summaryrefslogtreecommitdiffstats
path: root/fs/fs.c
diff options
context:
space:
mode:
authorAndrey Smirnov <andrew.smirnov@gmail.com>2019-02-26 11:53:03 -0800
committerSascha Hauer <s.hauer@pengutronix.de>2019-02-27 08:35:08 +0100
commite48c54902cc81652280cb50c4ddb8bad1cb6632f (patch)
treef129be80db135249d4d597d6c04d76010f56aa2e /fs/fs.c
parent27099b5255b478fd221013f0b868f0d754574228 (diff)
downloadbarebox-e48c54902cc81652280cb50c4ddb8bad1cb6632f.tar.gz
barebox-e48c54902cc81652280cb50c4ddb8bad1cb6632f.tar.xz
fs: Simplify fd to FILE lookup code
Avoid a bit of repeating code by merging checking fd for correctness and fd to FILE lookup into a single routine and converting the rest of the code to use it. Signed-off-by: Andrey Smirnov <andrew.smirnov@gmail.com> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
Diffstat (limited to 'fs/fs.c')
-rw-r--r--fs/fs.c83
1 files changed, 30 insertions, 53 deletions
diff --git a/fs/fs.c b/fs/fs.c
index 878a18e178..c6cb49996e 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -171,14 +171,14 @@ static void put_file(FILE *f)
dput(f->dentry);
}
-static int check_fd(int fd)
+static FILE *fd_to_file(int fd)
{
if (fd < 0 || fd >= MAX_FILES || !files[fd].in_use) {
errno = EBADF;
- return -errno;
+ return ERR_PTR(-errno);
}
- return 0;
+ return &files[fd];
}
static int create(struct dentry *dir, struct dentry *dentry)
@@ -205,14 +205,12 @@ EXPORT_SYMBOL(creat);
int ftruncate(int fd, loff_t length)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
if (f->size == FILE_SIZE_STREAM)
return 0;
@@ -232,14 +230,12 @@ int ftruncate(int fd, loff_t length)
int ioctl(int fd, int request, void *buf)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
fsdrv = f->fsdev->driver;
if (fsdrv->ioctl)
@@ -279,14 +275,12 @@ out:
ssize_t pread(int fd, void *buf, size_t count, loff_t offset)
{
loff_t pos;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
pos = f->pos;
f->pos = offset;
ret = __read(f, buf, count);
@@ -298,14 +292,12 @@ EXPORT_SYMBOL(pread);
ssize_t read(int fd, void *buf, size_t count)
{
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
ret = __read(f, buf, count);
if (ret > 0)
@@ -348,14 +340,12 @@ out:
ssize_t pwrite(int fd, const void *buf, size_t count, loff_t offset)
{
loff_t pos;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
pos = f->pos;
f->pos = offset;
ret = __write(f, buf, count);
@@ -367,14 +357,12 @@ EXPORT_SYMBOL(pwrite);
ssize_t write(int fd, const void *buf, size_t count)
{
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
ret = __write(f, buf, count);
if (ret > 0)
@@ -386,14 +374,12 @@ EXPORT_SYMBOL(write);
int flush(int fd)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
fsdrv = f->fsdev->driver;
if (fsdrv->flush)
ret = fsdrv->flush(&f->fsdev->dev, f);
@@ -406,17 +392,16 @@ int flush(int fd)
return ret;
}
-loff_t lseek(int fildes, loff_t offset, int whence)
+loff_t lseek(int fd, loff_t offset, int whence)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
loff_t pos;
int ret;
- if (check_fd(fildes))
+ if (IS_ERR(f))
return -1;
- f = &files[fildes];
fsdrv = f->fsdev->driver;
ret = -EINVAL;
@@ -461,12 +446,11 @@ EXPORT_SYMBOL(lseek);
int erase(int fd, loff_t count, loff_t offset)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
if (offset >= f->size)
return 0;
if (count == ERASE_SIZE_ALL || count > f->size - offset)
@@ -490,12 +474,11 @@ EXPORT_SYMBOL(erase);
int protect(int fd, size_t count, loff_t offset, int prot)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
if (offset >= f->size)
return 0;
if (count > f->size - offset)
@@ -532,15 +515,13 @@ int protect_file(const char *file, int prot)
void *memmap(int fd, int flags)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
void *retp = MAP_FAILED;
int ret;
- if (check_fd(fd))
+ if (IS_ERR(f))
return retp;
- f = &files[fd];
-
fsdrv = f->fsdev->driver;
if (fsdrv->memmap)
@@ -558,14 +539,12 @@ EXPORT_SYMBOL(memmap);
int close(int fd)
{
struct fs_driver_d *fsdrv;
- FILE *f;
+ FILE *f = fd_to_file(fd);
int ret = 0;
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
fsdrv = f->fsdev->driver;
if (fsdrv->close)
@@ -847,13 +826,11 @@ static void stat_inode(struct inode *inode, struct stat *s)
int fstat(int fd, struct stat *s)
{
- FILE *f;
+ FILE *f = fd_to_file(fd);
- if (check_fd(fd))
+ if (IS_ERR(f))
return -errno;
- f = &files[fd];
-
stat_inode(f->f_inode, s);
return 0;