We have this sequence at multiple places: Check a return value for being below zero and if so, turn it positive and store into errno. Instead of opencoding it everywhere, add a helper to encapsulate this. Signed-off-by: Ahmad Fatoum <a.fatoum@xxxxxxxxxxxxxx> --- fs/fs.c | 112 ++++++++++++------------------------------------ include/errno.h | 7 +++ lib/libfile.c | 15 +++---- lib/parameter.c | 22 ++++------ 4 files changed, 48 insertions(+), 108 deletions(-) diff --git a/fs/fs.c b/fs/fs.c index 68e7873e9c54..4b64f6fcf1df 100644 --- a/fs/fs.c +++ b/fs/fs.c @@ -365,10 +365,8 @@ int ftruncate(int fd, loff_t length) return 0; ret = fsdev_truncate(&f->fsdev->dev, f, length); - if (ret) { - errno = -ret; - return ret; - } + if (ret) + return errno_set(ret); f->size = length; f->f_inode->i_size = f->size; @@ -391,9 +389,8 @@ int ioctl(int fd, int request, void *buf) ret = fsdrv->ioctl(&f->fsdev->dev, f, request, buf); else ret = -ENOSYS; - if (ret) - errno = -ret; - return ret; + + return errno_set(ret); } static ssize_t __read(FILE *f, void *buf, size_t count) @@ -419,9 +416,7 @@ static ssize_t __read(FILE *f, void *buf, size_t count) ret = fsdrv->read(&f->fsdev->dev, f, buf, count); out: - if (ret < 0) - errno = -ret; - return ret; + return errno_set(ret); } ssize_t pread(int fd, void *buf, size_t count, loff_t offset) @@ -490,9 +485,7 @@ static ssize_t __write(FILE *f, const void *buf, size_t count) } ret = fsdrv->write(&f->fsdev->dev, f, buf, count); out: - if (ret < 0) - errno = -ret; - return ret; + return errno_set(ret); } ssize_t pwrite(int fd, const void *buf, size_t count, loff_t offset) @@ -544,10 +537,7 @@ int flush(int fd) else ret = 0; - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } loff_t lseek(int fd, loff_t offset, int whence) @@ -597,8 +587,7 @@ loff_t lseek(int fd, loff_t offset, int whence) return pos; out: - if (ret) - errno = -ret; + errno_set(ret); return -1; } @@ -629,10 +618,7 @@ int erase(int fd, loff_t count, loff_t offset) else ret = -ENOSYS; - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(erase); @@ -659,10 +645,7 @@ int protect(int fd, size_t count, loff_t offset, int prot) else ret = -ENOSYS; - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(protect); @@ -689,10 +672,7 @@ int discard_range(int fd, loff_t count, loff_t offset) else ret = -ENOSYS; - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } int protect_file(const char *file, int prot) @@ -730,9 +710,7 @@ void *memmap(int fd, int flags) else ret = -EINVAL; - if (ret) - errno = -ret; - + errno_set(ret); return retp; } EXPORT_SYMBOL(memmap); @@ -756,10 +734,7 @@ int close(int fd) put_file(f); - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(close); @@ -2464,10 +2439,7 @@ int mkdir (const char *pathname, mode_t mode) dput(dentry); path_put(&path); out: - if (error) - errno = -error; - - return error; + return errno_set(error); } EXPORT_SYMBOL(mkdir); @@ -2519,10 +2491,7 @@ int rmdir (const char *pathname) path_put(&path); putname(name); - if (error) - errno = -error; - - return error; + return errno_set(error); } EXPORT_SYMBOL(rmdir); @@ -2676,10 +2645,7 @@ int open(const char *pathname, int flags, ...) out: put_file(f); out1: - - if (error) - errno = -error; - return error; + return errno_set(error); } EXPORT_SYMBOL(open); @@ -2717,9 +2683,7 @@ int unlink(const char *pathname) out_put: path_put(&path); out: - if (ret) - errno = -ret; - return ret; + return errno_set(ret); } EXPORT_SYMBOL(unlink); @@ -2746,10 +2710,7 @@ int symlink(const char *pathname, const char *newpath) error = vfs_symlink(path.dentry->d_inode, dentry, pathname); out: - if (error) - errno = -error; - - return error; + return errno_set(error); } EXPORT_SYMBOL(symlink); @@ -2819,7 +2780,7 @@ DIR *opendir(const char *pathname) out_put: path_put(&path); out: - errno = -ret; + errno_set(ret); return NULL; } @@ -2827,10 +2788,8 @@ EXPORT_SYMBOL(opendir); int closedir(DIR *dir) { - if (!dir) { - errno = EBADF; - return -EBADF; - } + if (!dir) + return errno_set(-EBADF); release_dir(dir); @@ -2876,10 +2835,7 @@ int readlink(const char *pathname, char *buf, size_t bufsiz) out_put: path_put(&path); out: - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(readlink); @@ -2909,10 +2865,7 @@ static int stat_filename(const char *filename, struct stat *s, unsigned int flag out_put: path_put(&path); out: - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } int stat(const char *filename, struct stat *s) @@ -3000,9 +2953,7 @@ char *canonicalize_path(const char *pathname) res = dpath(path.dentry, d_root); out: - if (ret) - errno = -ret; - + errno_set(ret); return res; } @@ -3036,10 +2987,7 @@ int chdir(const char *pathname) ret = 0; out: - if (ret) - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(chdir); @@ -3234,9 +3182,7 @@ int mount(const char *device, const char *fsname, const char *pathname, out: path_put(&path); - errno = -ret; - - return ret; + return errno_set(ret); } EXPORT_SYMBOL(mount); @@ -3273,10 +3219,8 @@ int umount(const char *pathname) } } - if (!fsdev) { - errno = EFAULT; - return -EFAULT; - } + if (!fsdev) + return errno_set(-EFAULT); return fsdev_umount(fsdev); } diff --git a/include/errno.h b/include/errno.h index 6ec7af4d7e9f..12e526a0d7ed 100644 --- a/include/errno.h +++ b/include/errno.h @@ -10,4 +10,11 @@ extern int errno; void perror(const char *s); const char *strerror(int errnum); +static inline int errno_set(int err) +{ + if (err < 0) + errno = -err; + return err; +} + #endif /* __ERRNO_H */ diff --git a/lib/libfile.c b/lib/libfile.c index 72a2fc79c721..185c7af721b5 100644 --- a/lib/libfile.c +++ b/lib/libfile.c @@ -33,10 +33,8 @@ int pwrite_full(int fd, const void *buf, size_t size, loff_t offset) while (size) { now = pwrite(fd, buf, size, offset); - if (now == 0) { - errno = ENOSPC; - return -errno; - } + if (now == 0) + return errno_set(-ENOSPC); if (now < 0) return now; size -= now; @@ -61,10 +59,8 @@ int write_full(int fd, const void *buf, size_t size) while (size) { now = write(fd, buf, size); - if (now == 0) { - errno = ENOSPC; - return -errno; - } + if (now == 0) + return errno_set(-ENOSPC); if (now < 0) return now; size -= now; @@ -240,8 +236,7 @@ int read_file_2(const char *filename, size_t *size, void **outbuf, /* ensure wchar_t nul termination */ buf = calloc(ALIGN(read_size, 2) + 2, 1); if (!buf) { - ret = -ENOMEM; - errno = ENOMEM; + ret = errno_set(-ENOMEM); goto err_out; } diff --git a/lib/parameter.c b/lib/parameter.c index aac33e3a1940..c2b8536ee93f 100644 --- a/lib/parameter.c +++ b/lib/parameter.c @@ -90,28 +90,22 @@ int dev_set_param(struct device *dev, const char *name, const char *val) struct param_d *param; int ret; - if (!dev) { - errno = ENODEV; - return -ENODEV; - } + if (!dev) + return errno_set(-ENODEV); param = get_param_by_name(dev, name); - if (!param) { - errno = EINVAL; - return -EINVAL; - } + if (!param) + return errno_set(-EINVAL); - if (param->flags & PARAM_FLAG_RO) { - errno = EACCES; - return -EACCES; - } + if (param->flags & PARAM_FLAG_RO) + return errno_set(-EACCES); ret = param->set(dev, param, val); if (ret) - errno = -ret; + return errno_set(ret); - return ret; + return 0; } /** -- 2.39.2