Commit bcb1d238 authored by Artem Bityutskiy's avatar Artem Bityutskiy Committed by David Woodhouse

mtd: move zero length verification to MTD API functions

In many places in drivers we verify for the zero length, but this is very
inconsistent across drivers. This is obviously the right thing to do, though.
This patch moves the check to the MTD API functions instead and removes a lot
of duplication.
Signed-off-by: default avatarArtem Bityutskiy <artem.bityutskiy@linux.intel.com>
Reviewed-by: default avatarShmulik Ladkani <shmulik.ladkani@gmail.com>
Signed-off-by: default avatarDavid Woodhouse <David.Woodhouse@intel.com>
parent 0dd5235f
...@@ -1566,9 +1566,6 @@ static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t le ...@@ -1566,9 +1566,6 @@ static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t le
int chipnum; int chipnum;
unsigned long ofs; unsigned long ofs;
if (!len)
return 0;
chipnum = to >> cfi->chipshift; chipnum = to >> cfi->chipshift;
ofs = to - (chipnum << cfi->chipshift); ofs = to - (chipnum << cfi->chipshift);
......
...@@ -1246,9 +1246,6 @@ static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1246,9 +1246,6 @@ static int cfi_amdstd_write_words(struct mtd_info *mtd, loff_t to, size_t len,
unsigned long ofs, chipstart; unsigned long ofs, chipstart;
DECLARE_WAITQUEUE(wait, current); DECLARE_WAITQUEUE(wait, current);
if (!len)
return 0;
chipnum = to >> cfi->chipshift; chipnum = to >> cfi->chipshift;
ofs = to - (chipnum << cfi->chipshift); ofs = to - (chipnum << cfi->chipshift);
chipstart = cfi->chips[chipnum].start; chipstart = cfi->chips[chipnum].start;
...@@ -1487,9 +1484,6 @@ static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1487,9 +1484,6 @@ static int cfi_amdstd_write_buffers(struct mtd_info *mtd, loff_t to, size_t len,
int chipnum; int chipnum;
unsigned long ofs; unsigned long ofs;
if (!len)
return 0;
chipnum = to >> cfi->chipshift; chipnum = to >> cfi->chipshift;
ofs = to - (chipnum << cfi->chipshift); ofs = to - (chipnum << cfi->chipshift);
...@@ -1697,9 +1691,6 @@ static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -1697,9 +1691,6 @@ static int cfi_amdstd_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
int ret = 0; int ret = 0;
int chipnum; int chipnum;
if (!len)
return 0;
chipnum = to >> cfi->chipshift; chipnum = to >> cfi->chipshift;
ofs = to - (chipnum << cfi->chipshift); ofs = to - (chipnum << cfi->chipshift);
chipstart = cfi->chips[chipnum].start; chipstart = cfi->chips[chipnum].start;
......
...@@ -615,9 +615,6 @@ static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to, ...@@ -615,9 +615,6 @@ static int cfi_staa_write_buffers (struct mtd_info *mtd, loff_t to,
int chipnum; int chipnum;
unsigned long ofs; unsigned long ofs;
if (!len)
return 0;
chipnum = to >> cfi->chipshift; chipnum = to >> cfi->chipshift;
ofs = to - (chipnum << cfi->chipshift); ofs = to - (chipnum << cfi->chipshift);
......
...@@ -178,9 +178,6 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -178,9 +178,6 @@ static int block2mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
struct block2mtd_dev *dev = mtd->priv; struct block2mtd_dev *dev = mtd->priv;
int err; int err;
if (!len)
return 0;
mutex_lock(&dev->write_mutex); mutex_lock(&dev->write_mutex);
err = _block2mtd_write(dev, buf, to, len, retlen); err = _block2mtd_write(dev, buf, to, len, retlen);
mutex_unlock(&dev->write_mutex); mutex_unlock(&dev->write_mutex);
......
...@@ -437,9 +437,6 @@ static int flash_read (struct mtd_info *mtd,loff_t from,size_t len,size_t *retle ...@@ -437,9 +437,6 @@ static int flash_read (struct mtd_info *mtd,loff_t from,size_t len,size_t *retle
printk (KERN_DEBUG "%s(from = 0x%.8x, len = %d)\n", __func__, (__u32)from, len); printk (KERN_DEBUG "%s(from = 0x%.8x, len = %d)\n", __func__, (__u32)from, len);
#endif #endif
/* sanity checks */
if (!len) return (0);
/* we always read len bytes */ /* we always read len bytes */
*retlen = len; *retlen = len;
......
...@@ -346,10 +346,6 @@ static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -346,10 +346,6 @@ static int m25p80_read(struct mtd_info *mtd, loff_t from, size_t len,
pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev), pr_debug("%s: %s from 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
__func__, (u32)from, len); __func__, (u32)from, len);
/* sanity checks */
if (!len)
return 0;
spi_message_init(&m); spi_message_init(&m);
memset(t, 0, (sizeof t)); memset(t, 0, (sizeof t));
...@@ -408,10 +404,6 @@ static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -408,10 +404,6 @@ static int m25p80_write(struct mtd_info *mtd, loff_t to, size_t len,
pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev), pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
__func__, (u32)to, len); __func__, (u32)to, len);
/* sanity checks */
if (!len)
return(0);
spi_message_init(&m); spi_message_init(&m);
memset(t, 0, (sizeof t)); memset(t, 0, (sizeof t));
...@@ -495,10 +487,6 @@ static int sst_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -495,10 +487,6 @@ static int sst_write(struct mtd_info *mtd, loff_t to, size_t len,
pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev), pr_debug("%s: %s to 0x%08x, len %zd\n", dev_name(&flash->spi->dev),
__func__, (u32)to, len); __func__, (u32)to, len);
/* sanity checks */
if (!len)
return 0;
spi_message_init(&m); spi_message_init(&m);
memset(t, 0, (sizeof t)); memset(t, 0, (sizeof t));
......
...@@ -249,10 +249,6 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -249,10 +249,6 @@ static int dataflash_read(struct mtd_info *mtd, loff_t from, size_t len,
pr_debug("%s: read 0x%x..0x%x\n", dev_name(&priv->spi->dev), pr_debug("%s: read 0x%x..0x%x\n", dev_name(&priv->spi->dev),
(unsigned)from, (unsigned)(from + len)); (unsigned)from, (unsigned)(from + len));
/* Sanity checks */
if (!len)
return 0;
/* Calculate flash page/byte address */ /* Calculate flash page/byte address */
addr = (((unsigned)from / priv->page_size) << priv->page_offset) addr = (((unsigned)from / priv->page_size) << priv->page_offset)
+ ((unsigned)from % priv->page_size); + ((unsigned)from % priv->page_size);
...@@ -321,10 +317,6 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -321,10 +317,6 @@ static int dataflash_write(struct mtd_info *mtd, loff_t to, size_t len,
pr_debug("%s: write 0x%x..0x%x\n", pr_debug("%s: write 0x%x..0x%x\n",
dev_name(&spi->dev), (unsigned)to, (unsigned)(to + len)); dev_name(&spi->dev), (unsigned)to, (unsigned)(to + len));
/* Sanity checks */
if (!len)
return 0;
spi_message_init(&msg); spi_message_init(&msg);
x[0].tx_buf = command = priv->command; x[0].tx_buf = command = priv->command;
...@@ -479,8 +471,6 @@ static ssize_t otp_read(struct spi_device *spi, unsigned base, ...@@ -479,8 +471,6 @@ static ssize_t otp_read(struct spi_device *spi, unsigned base,
if ((off + len) > 64) if ((off + len) > 64)
len = 64 - off; len = 64 - off;
if (len == 0)
return len;
spi_message_init(&m); spi_message_init(&m);
......
...@@ -563,9 +563,6 @@ static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -563,9 +563,6 @@ static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
u32 ctrlreg1, val; u32 ctrlreg1, val;
int ret; int ret;
if (!len)
return 0;
if (!flash || !dev) if (!flash || !dev)
return -ENODEV; return -ENODEV;
...@@ -662,9 +659,6 @@ static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -662,9 +659,6 @@ static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
if (!flash || !dev) if (!flash || !dev)
return -ENODEV; return -ENODEV;
if (!len)
return 0;
if (flash->bank > dev->num_flashes - 1) { if (flash->bank > dev->num_flashes - 1) {
dev_err(&dev->pdev->dev, "Invalid Bank Num"); dev_err(&dev->pdev->dev, "Invalid Bank Num");
return -EINVAL; return -EINVAL;
......
...@@ -220,10 +220,6 @@ static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -220,10 +220,6 @@ static int sst25l_read(struct mtd_info *mtd, loff_t from, size_t len,
unsigned char command[4]; unsigned char command[4];
int ret; int ret;
/* Sanity checking */
if (len == 0)
return 0;
spi_message_init(&message); spi_message_init(&message);
memset(&transfer, 0, sizeof(transfer)); memset(&transfer, 0, sizeof(transfer));
...@@ -265,10 +261,6 @@ static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -265,10 +261,6 @@ static int sst25l_write(struct mtd_info *mtd, loff_t to, size_t len,
int i, j, ret, bytes, copied = 0; int i, j, ret, bytes, copied = 0;
unsigned char command[5]; unsigned char command[5];
/* Sanity checks */
if (!len)
return 0;
if ((uint32_t)to % mtd->writesize) if ((uint32_t)to % mtd->writesize)
return -EINVAL; return -EINVAL;
......
...@@ -639,7 +639,6 @@ static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs, ...@@ -639,7 +639,6 @@ static int lpddr_writev(struct mtd_info *mtd, const struct kvec *vecs,
int chipnum; int chipnum;
unsigned long ofs, vec_seek, i; unsigned long ofs, vec_seek, i;
int wbufsize = 1 << lpddr->qinfo->BufSizeShift; int wbufsize = 1 << lpddr->qinfo->BufSizeShift;
size_t len = 0; size_t len = 0;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
......
...@@ -360,9 +360,6 @@ static int vmu_flash_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -360,9 +360,6 @@ static int vmu_flash_read(struct mtd_info *mtd, loff_t from, size_t len,
int index = 0, retval, partition, leftover, numblocks; int index = 0, retval, partition, leftover, numblocks;
unsigned char cx; unsigned char cx;
if (len < 1)
return -EIO;
mpart = mtd->priv; mpart = mtd->priv;
mdev = mpart->mdev; mdev = mpart->mdev;
partition = mpart->partition; partition = mpart->partition;
...@@ -434,11 +431,6 @@ static int vmu_flash_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -434,11 +431,6 @@ static int vmu_flash_write(struct mtd_info *mtd, loff_t to, size_t len,
partition = mpart->partition; partition = mpart->partition;
card = maple_get_drvdata(mdev); card = maple_get_drvdata(mdev);
/* simple sanity checks */
if (len < 1) {
error = -EIO;
goto failed;
}
numblocks = card->parts[partition].numblocks; numblocks = card->parts[partition].numblocks;
if (to + len > numblocks * card->blocklen) if (to + len > numblocks * card->blocklen)
len = numblocks * card->blocklen - to; len = numblocks * card->blocklen - to;
......
...@@ -695,6 +695,11 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr) ...@@ -695,6 +695,11 @@ int mtd_erase(struct mtd_info *mtd, struct erase_info *instr)
return -EINVAL; return -EINVAL;
if (!(mtd->flags & MTD_WRITEABLE)) if (!(mtd->flags & MTD_WRITEABLE))
return -EROFS; return -EROFS;
if (!instr->len) {
instr->state = MTD_ERASE_DONE;
mtd_erase_callback(instr);
return 0;
}
return mtd->_erase(mtd, instr); return mtd->_erase(mtd, instr);
} }
EXPORT_SYMBOL_GPL(mtd_erase); EXPORT_SYMBOL_GPL(mtd_erase);
...@@ -713,6 +718,8 @@ int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, ...@@ -713,6 +718,8 @@ int mtd_point(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (from < 0 || from > mtd->size || len > mtd->size - from) if (from < 0 || from > mtd->size || len > mtd->size - from)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_point(mtd, from, len, retlen, virt, phys); return mtd->_point(mtd, from, len, retlen, virt, phys);
} }
EXPORT_SYMBOL_GPL(mtd_point); EXPORT_SYMBOL_GPL(mtd_point);
...@@ -724,6 +731,8 @@ int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len) ...@@ -724,6 +731,8 @@ int mtd_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (from < 0 || from > mtd->size || len > mtd->size - from) if (from < 0 || from > mtd->size || len > mtd->size - from)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_unpoint(mtd, from, len); return mtd->_unpoint(mtd, from, len);
} }
EXPORT_SYMBOL_GPL(mtd_unpoint); EXPORT_SYMBOL_GPL(mtd_unpoint);
...@@ -750,6 +759,8 @@ int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, ...@@ -750,6 +759,8 @@ int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
*retlen = 0; *retlen = 0;
if (from < 0 || from > mtd->size || len > mtd->size - from) if (from < 0 || from > mtd->size || len > mtd->size - from)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_read(mtd, from, len, retlen, buf); return mtd->_read(mtd, from, len, retlen, buf);
} }
EXPORT_SYMBOL_GPL(mtd_read); EXPORT_SYMBOL_GPL(mtd_read);
...@@ -762,6 +773,8 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, ...@@ -762,6 +773,8 @@ int mtd_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
return -EINVAL; return -EINVAL;
if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE)) if (!mtd->_write || !(mtd->flags & MTD_WRITEABLE))
return -EROFS; return -EROFS;
if (!len)
return 0;
return mtd->_write(mtd, to, len, retlen, buf); return mtd->_write(mtd, to, len, retlen, buf);
} }
EXPORT_SYMBOL_GPL(mtd_write); EXPORT_SYMBOL_GPL(mtd_write);
...@@ -783,6 +796,8 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen, ...@@ -783,6 +796,8 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
return -EINVAL; return -EINVAL;
if (!(mtd->flags & MTD_WRITEABLE)) if (!(mtd->flags & MTD_WRITEABLE))
return -EROFS; return -EROFS;
if (!len)
return 0;
return mtd->_panic_write(mtd, to, len, retlen, buf); return mtd->_panic_write(mtd, to, len, retlen, buf);
} }
EXPORT_SYMBOL_GPL(mtd_panic_write); EXPORT_SYMBOL_GPL(mtd_panic_write);
...@@ -794,6 +809,8 @@ int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) ...@@ -794,6 +809,8 @@ int mtd_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_lock(mtd, ofs, len); return mtd->_lock(mtd, ofs, len);
} }
EXPORT_SYMBOL_GPL(mtd_lock); EXPORT_SYMBOL_GPL(mtd_lock);
...@@ -804,6 +821,8 @@ int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) ...@@ -804,6 +821,8 @@ int mtd_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_unlock(mtd, ofs, len); return mtd->_unlock(mtd, ofs, len);
} }
EXPORT_SYMBOL_GPL(mtd_unlock); EXPORT_SYMBOL_GPL(mtd_unlock);
...@@ -814,6 +833,8 @@ int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) ...@@ -814,6 +833,8 @@ int mtd_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
return -EOPNOTSUPP; return -EOPNOTSUPP;
if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs) if (ofs < 0 || ofs > mtd->size || len > mtd->size - ofs)
return -EINVAL; return -EINVAL;
if (!len)
return 0;
return mtd->_is_locked(mtd, ofs, len); return mtd->_is_locked(mtd, ofs, len);
} }
EXPORT_SYMBOL_GPL(mtd_is_locked); EXPORT_SYMBOL_GPL(mtd_is_locked);
......
...@@ -1614,9 +1614,6 @@ static int nand_read(struct mtd_info *mtd, loff_t from, size_t len, ...@@ -1614,9 +1614,6 @@ static int nand_read(struct mtd_info *mtd, loff_t from, size_t len,
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int ret; int ret;
if (!len)
return 0;
nand_get_device(chip, mtd, FL_READING); nand_get_device(chip, mtd, FL_READING);
ops.len = len; ops.len = len;
ops.datbuf = buf; ops.datbuf = buf;
...@@ -2313,10 +2310,6 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -2313,10 +2310,6 @@ static int panic_nand_write(struct mtd_info *mtd, loff_t to, size_t len,
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int ret; int ret;
/* Do not allow reads past end of device */
if (!len)
return 0;
/* Wait for the device to get ready */ /* Wait for the device to get ready */
panic_nand_wait(mtd, chip, 400); panic_nand_wait(mtd, chip, 400);
...@@ -2351,9 +2344,6 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len, ...@@ -2351,9 +2344,6 @@ static int nand_write(struct mtd_info *mtd, loff_t to, size_t len,
struct mtd_oob_ops ops; struct mtd_oob_ops ops;
int ret; int ret;
if (!len)
return 0;
nand_get_device(chip, mtd, FL_WRITING); nand_get_device(chip, mtd, FL_WRITING);
ops.len = len; ops.len = len;
ops.datbuf = (uint8_t *)buf; ops.datbuf = (uint8_t *)buf;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment