9
0
Fork 0

fs: add pread and pwrite functions

Add pread and pwrite functions.

Split read and write functions to save some space.
The functions pread and pwrite saves and sets the file
position to a given offset and restore them afterwards.

This also makes the nandtest command use these function
which is necessary to not break compilation for the nandtest
command.

Signed-off-by: Alexander Aring <alex.aring@gmail.com>
Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
This commit is contained in:
Alexander Aring 2013-02-17 22:05:01 +01:00 committed by Sascha Hauer
parent e48b144071
commit 9d8a20592f
3 changed files with 82 additions and 44 deletions

View File

@ -42,42 +42,18 @@ static unsigned int ecc_stats[MAX_ECC_BITS];
static unsigned int ecc_stats_over;
static unsigned int ecc_failed_cnt;
/*
* Implementation of pread with lseek and read.
*/
static ssize_t pread(int fd, void *buf, size_t count, loff_t offset)
{
int ret;
/* Seek to offset */
ret = lseek(fd, offset, SEEK_SET);
if (ret < 0)
perror("lseek");
/* Read from flash and put it into buf */
ret = read(fd, buf, count);
if (ret < 0)
perror("read");
return 0;
}
/*
* Implementation of pwrite with lseek and write.
*/
static ssize_t pwrite(int fd, const void *buf,
static ssize_t __pwrite(int fd, const void *buf,
size_t count, loff_t offset, loff_t length)
{
int ret;
ret = lseek(fd, offset, SEEK_SET);
if (ret < 0)
perror("lseek");
ssize_t ret;
/* Write buf to flash */
ret = write(fd, buf, count);
ret = pwrite(fd, buf, count, offset);
if (ret < 0) {
perror("write");
perror("pwrite");
if (markbad) {
printf("\nMark block bad at 0x%08llx\n",
offset + memregion.offset);
@ -88,7 +64,7 @@ static ssize_t pwrite(int fd, const void *buf,
}
flush(fd);
return 0;
return ret;
}
/*
@ -119,7 +95,7 @@ static int erase_and_write(loff_t ofs, unsigned char *data,
for (i = 0; i < meminfo.erasesize;
i += meminfo.writesize) {
/* Write data to given offset */
pwrite(fd, data + i, meminfo.writesize,
__pwrite(fd, data + i, meminfo.writesize,
ofs + i, length);
/* Read data from offset */

88
fs/fs.c
View File

@ -754,17 +754,12 @@ int ioctl(int fd, int request, void *buf)
return ret;
}
ssize_t read(int fd, void *buf, size_t count)
static ssize_t __read(FILE *f, void *buf, size_t count)
{
struct device_d *dev;
struct fs_driver_d *fsdrv;
FILE *f;
int ret;
if (check_fd(fd))
return -errno;
f = &files[fd];
dev = f->dev;
fsdrv = dev_to_fs_driver(dev);
@ -777,18 +772,14 @@ ssize_t read(int fd, void *buf, size_t count)
ret = fsdrv->read(dev, f, buf, count);
if (ret > 0)
f->pos += ret;
if (ret < 0)
errno = -ret;
return ret;
}
EXPORT_SYMBOL(read);
ssize_t write(int fd, const void *buf, size_t count)
ssize_t pread(int fd, void *buf, size_t count, loff_t offset)
{
struct device_d *dev;
struct fs_driver_d *fsdrv;
loff_t pos;
FILE *f;
int ret;
@ -796,6 +787,40 @@ ssize_t write(int fd, const void *buf, size_t count)
return -errno;
f = &files[fd];
pos = f->pos;
f->pos = offset;
ret = __read(f, buf, count);
f->pos = pos;
return ret;
}
EXPORT_SYMBOL(pread);
ssize_t read(int fd, void *buf, size_t count)
{
FILE *f;
int ret;
if (check_fd(fd))
return -errno;
f = &files[fd];
ret = __read(f, buf, count);
if (ret > 0)
f->pos += ret;
return ret;
}
EXPORT_SYMBOL(read);
static ssize_t __write(FILE *f, const void *buf, size_t count)
{
struct device_d *dev;
struct fs_driver_d *fsdrv;
int ret;
dev = f->dev;
fsdrv = dev_to_fs_driver(dev);
@ -812,13 +837,48 @@ ssize_t write(int fd, const void *buf, size_t count)
}
}
ret = fsdrv->write(dev, f, buf, count);
if (ret > 0)
f->pos += ret;
out:
if (ret < 0)
errno = -ret;
return ret;
}
ssize_t pwrite(int fd, const void *buf, size_t count, loff_t offset)
{
loff_t pos;
FILE *f;
int ret;
if (check_fd(fd))
return -errno;
f = &files[fd];
pos = f->pos;
f->pos = offset;
ret = __write(f, buf, count);
f->pos = pos;
return ret;
}
EXPORT_SYMBOL(pwrite);
ssize_t write(int fd, const void *buf, size_t count)
{
FILE *f;
int ret;
if (check_fd(fd))
return -errno;
f = &files[fd];
ret = __write(f, buf, count);
if (ret > 0)
f->pos += ret;
return ret;
}
EXPORT_SYMBOL(write);
int flush(int fd)

View File

@ -114,8 +114,10 @@ int flush(int fd);
int lstat(const char *filename, struct stat *s);
int stat(const char *filename, struct stat *s);
ssize_t read(int fd, void *buf, size_t count);
ssize_t pread(int fd, void *buf, size_t count, loff_t offset);
int ioctl(int fd, int request, void *buf);
ssize_t write(int fd, const void *buf, size_t count);
ssize_t pwrite(int fd, const void *buf, size_t count, loff_t offset);
#define SEEK_SET 1
#define SEEK_CUR 2