[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling
From: |
Michael Tokarev |
Subject: |
[Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling |
Date: |
Sat, 7 Mar 2015 00:43:02 +0300 |
All filesystem methods that call common v9fs_request() function
also convert return value to errno. Move this conversion to the
common function and remove redundand error handling in methods.
I didn't remove local `retval' variable in simple functions to
keep the code consistent.
Also, proxy_truncate() seem to prefer zero successful return
instead of returning whatever the helper returned, maybe this
should be changed.
This also removes (harmless) double call to v9fs_string_free()
in proxy_mkdir(), and renames local variables in some functions
for consistency.
Signed-off-by: Michael Tokarev <address@hidden>
---
hw/9pfs/virtio-9p-proxy.c | 142 ++++++++++++----------------------------------
1 file changed, 35 insertions(+), 107 deletions(-)
diff --git a/hw/9pfs/virtio-9p-proxy.c b/hw/9pfs/virtio-9p-proxy.c
index 0904130..13064b6 100644
--- a/hw/9pfs/virtio-9p-proxy.c
+++ b/hw/9pfs/virtio-9p-proxy.c
@@ -291,7 +291,7 @@ static int v9fs_receive_status(V9fsProxy *proxy,
/*
* Proxy->header and proxy->request written to socket by QEMU process.
* This request read by proxy helper process
- * returns 0 on success and -errno on error
+ * returns 0 on success and -1 (setting errno) on error
*/
static int v9fs_request(V9fsProxy *proxy, int type,
void *response, const char *fmt, ...)
@@ -299,7 +299,7 @@ static int v9fs_request(V9fsProxy *proxy, int type,
dev_t rdev;
va_list ap;
int size = 0;
- int retval = 0;
+ int retval, err;
uint64_t offset;
ProxyHeader header = { 0, 0};
struct timespec spec[2];
@@ -310,10 +310,11 @@ static int v9fs_request(V9fsProxy *proxy, int type,
qemu_mutex_lock(&proxy->mutex);
- if (proxy->sockfd == -1) {
+ if (proxy->sockfd < 0) {
retval = -EIO;
- goto err_out;
+ goto out;
}
+
iovec = &proxy->out_iovec;
reply = &proxy->in_iovec;
va_start(ap, fmt);
@@ -529,15 +530,15 @@ static int v9fs_request(V9fsProxy *proxy, int type,
va_end(ap);
if (retval < 0) {
- goto err_out;
+ goto out;
}
/* marshal the header details */
proxy_marshal(iovec, 0, "dd", header.type, header.size);
header.size += PROXY_HDR_SZ;
- retval = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
- if (retval != header.size) {
+ err = qemu_write_full(proxy->sockfd, iovec->iov_base, header.size);
+ if (err != header.size) {
goto close_error;
}
@@ -548,9 +549,7 @@ static int v9fs_request(V9fsProxy *proxy, int type,
* A file descriptor is returned as response for
* T_OPEN,T_CREATE on success
*/
- if (v9fs_receivefd(proxy->sockfd, &retval) < 0) {
- goto close_error;
- }
+ err = v9fs_receivefd(proxy->sockfd, &retval);
break;
case T_MKNOD:
case T_MKDIR:
@@ -564,51 +563,44 @@ static int v9fs_request(V9fsProxy *proxy, int type,
case T_REMOVE:
case T_LSETXATTR:
case T_LREMOVEXATTR:
- if (v9fs_receive_status(proxy, reply, &retval) < 0) {
- goto close_error;
- }
+ err = v9fs_receive_status(proxy, reply, &retval);
break;
case T_LSTAT:
case T_READLINK:
case T_STATFS:
case T_GETVERSION:
- if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
- goto close_error;
- }
+ err = v9fs_receive_response(proxy, type, &retval, response);
break;
case T_LGETXATTR:
case T_LLISTXATTR:
if (!size) {
- if (v9fs_receive_status(proxy, reply, &retval) < 0) {
- goto close_error;
- }
+ err = v9fs_receive_status(proxy, reply, &retval);
} else {
- if (v9fs_receive_response(proxy, type, &retval, response) < 0) {
- goto close_error;
- }
+ err = v9fs_receive_response(proxy, type, &retval, response);
}
break;
}
-err_out:
- qemu_mutex_unlock(&proxy->mutex);
- return retval;
-
+ if (err < 0) {
close_error:
- close(proxy->sockfd);
- proxy->sockfd = -1;
+ close(proxy->sockfd);
+ proxy->sockfd = -1;
+ retval = -EIO;
+ }
+
+out:
+ if (retval < 0) {
+ errno = -retval;
+ retval = -1;
+ }
qemu_mutex_unlock(&proxy->mutex);
- return -EIO;
+ return retval;
}
static int proxy_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat
*stbuf)
{
int retval;
retval = v9fs_request(fs_ctx->private, T_LSTAT, stbuf, "s", fs_path);
- if (retval < 0) {
- errno = -retval;
- return -1;
- }
return retval;
}
@@ -619,7 +611,6 @@ static ssize_t proxy_readlink(FsContext *fs_ctx, V9fsPath
*fs_path,
retval = v9fs_request(fs_ctx->private, T_READLINK, buf, "sd",
fs_path, bufsz);
if (retval < 0) {
- errno = -retval;
return -1;
}
return strlen(buf);
@@ -639,10 +630,6 @@ static int proxy_open(FsContext *ctx, V9fsPath *fs_path,
int flags, V9fsFidOpenState *fs)
{
fs->fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, flags);
- if (fs->fd < 0) {
- errno = -fs->fd;
- fs->fd = -1;
- }
return fs->fd;
}
@@ -654,7 +641,6 @@ static int proxy_opendir(FsContext *ctx,
fs->dir = NULL;
fd = v9fs_request(ctx->private, T_OPEN, NULL, "sd", fs_path, O_DIRECTORY);
if (fd < 0) {
- errno = -fd;
return -1;
}
fs->dir = fdopendir(fd);
@@ -738,9 +724,6 @@ static int proxy_chmod(FsContext *fs_ctx, V9fsPath
*fs_path, FsCred *credp)
int retval;
retval = v9fs_request(fs_ctx->private, T_CHMOD, NULL, "sd",
fs_path, credp->fc_mode);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -757,10 +740,6 @@ static int proxy_mknod(FsContext *fs_ctx, V9fsPath
*dir_path,
&fullname, credp->fc_mode, credp->fc_rdev,
credp->fc_uid, credp->fc_gid);
v9fs_string_free(&fullname);
- if (retval < 0) {
- errno = -retval;
- retval = -1;
- }
return retval;
}
@@ -776,11 +755,6 @@ static int proxy_mkdir(FsContext *fs_ctx, V9fsPath
*dir_path,
retval = v9fs_request(fs_ctx->private, T_MKDIR, NULL, "sddd", &fullname,
credp->fc_mode, credp->fc_uid, credp->fc_gid);
v9fs_string_free(&fullname);
- if (retval < 0) {
- errno = -retval;
- retval = -1;
- }
- v9fs_string_free(&fullname);
return retval;
}
@@ -809,10 +783,6 @@ static int proxy_open2(FsContext *fs_ctx, V9fsPath
*dir_path, const char *name,
&fullname, flags, credp->fc_mode,
credp->fc_uid, credp->fc_gid);
v9fs_string_free(&fullname);
- if (fs->fd < 0) {
- errno = -fs->fd;
- fs->fd = -1;
- }
return fs->fd;
}
@@ -832,10 +802,6 @@ static int proxy_symlink(FsContext *fs_ctx, const char
*oldpath,
&target, &fullname, credp->fc_uid, credp->fc_gid);
v9fs_string_free(&fullname);
v9fs_string_free(&target);
- if (retval < 0) {
- errno = -retval;
- retval = -1;
- }
return retval;
}
@@ -850,20 +816,14 @@ static int proxy_link(FsContext *ctx, V9fsPath *oldpath,
retval = v9fs_request(ctx->private, T_LINK, NULL, "ss", oldpath, &newpath);
v9fs_string_free(&newpath);
- if (retval < 0) {
- errno = -retval;
- retval = -1;
- }
return retval;
}
static int proxy_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
{
int retval;
-
retval = v9fs_request(ctx->private, T_TRUNCATE, NULL, "sq", fs_path, size);
if (retval < 0) {
- errno = -retval;
return -1;
}
return 0;
@@ -884,9 +844,6 @@ static int proxy_rename(FsContext *ctx, const char *oldpath,
&oldname, &newname);
v9fs_string_free(&oldname);
v9fs_string_free(&newname);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -895,9 +852,6 @@ static int proxy_chown(FsContext *fs_ctx, V9fsPath
*fs_path, FsCred *credp)
int retval;
retval = v9fs_request(fs_ctx->private, T_CHOWN, NULL, "sdd",
fs_path, credp->fc_uid, credp->fc_gid);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -909,9 +863,6 @@ static int proxy_utimensat(FsContext *s, V9fsPath *fs_path,
fs_path,
buf[0].tv_sec, buf[0].tv_nsec,
buf[1].tv_sec, buf[1].tv_nsec);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -923,9 +874,6 @@ static int proxy_remove(FsContext *ctx, const char *path)
v9fs_string_sprintf(&name, "%s", path);
retval = v9fs_request(ctx->private, T_REMOVE, NULL, "s", &name);
v9fs_string_free(&name);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -951,10 +899,6 @@ static int proxy_statfs(FsContext *s, V9fsPath *fs_path,
struct statfs *stbuf)
{
int retval;
retval = v9fs_request(s->private, T_STATFS, stbuf, "s", fs_path);
- if (retval < 0) {
- errno = -retval;
- return -1;
- }
return retval;
}
@@ -969,9 +913,6 @@ static ssize_t proxy_lgetxattr(FsContext *ctx, V9fsPath
*fs_path,
retval = v9fs_request(ctx->private, T_LGETXATTR, value, "dss", size,
fs_path, &xname);
v9fs_string_free(&xname);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -980,10 +921,7 @@ static ssize_t proxy_llistxattr(FsContext *ctx, V9fsPath
*fs_path,
{
int retval;
retval = v9fs_request(ctx->private, T_LLISTXATTR, value, "ds", size,
- fs_path);
- if (retval < 0) {
- errno = -retval;
- }
+ fs_path);
return retval;
}
@@ -1005,9 +943,6 @@ static int proxy_lsetxattr(FsContext *ctx, V9fsPath
*fs_path, const char *name,
fs_path, &xname, &xvalue, size, flags);
v9fs_string_free(&xname);
v9fs_string_free(&xvalue);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -1022,9 +957,6 @@ static int proxy_lremovexattr(FsContext *ctx, V9fsPath
*fs_path,
retval = v9fs_request(ctx->private, T_LREMOVEXATTR, NULL, "ss",
fs_path, &xname);
v9fs_string_free(&xname);
- if (retval < 0) {
- errno = -retval;
- }
return retval;
}
@@ -1046,7 +978,7 @@ static int proxy_renameat(FsContext *ctx, V9fsPath *olddir,
const char *old_name, V9fsPath *newdir,
const char *new_name)
{
- int ret;
+ int retval;
V9fsString old_full_name, new_full_name;
v9fs_string_init(&old_full_name);
@@ -1055,30 +987,30 @@ static int proxy_renameat(FsContext *ctx, V9fsPath
*olddir,
v9fs_string_sprintf(&old_full_name, "%s/%s", olddir->data, old_name);
v9fs_string_sprintf(&new_full_name, "%s/%s", newdir->data, new_name);
- ret = proxy_rename(ctx, old_full_name.data, new_full_name.data);
+ retval = proxy_rename(ctx, old_full_name.data, new_full_name.data);
v9fs_string_free(&old_full_name);
v9fs_string_free(&new_full_name);
- return ret;
+ return retval;
}
static int proxy_unlinkat(FsContext *ctx, V9fsPath *dir,
const char *name, int flags)
{
- int ret;
+ int retval;
V9fsString fullname;
v9fs_string_init(&fullname);
v9fs_string_sprintf(&fullname, "%s/%s", dir->data, name);
- ret = proxy_remove(ctx, fullname.data);
+ retval = proxy_remove(ctx, fullname.data);
v9fs_string_free(&fullname);
- return ret;
+ return retval;
}
static int proxy_ioc_getversion(FsContext *fs_ctx, V9fsPath *path,
mode_t st_mode, uint64_t *st_gen)
{
- int err;
+ int retval;
/* Do not try to open special files like device nodes, fifos etc
* we can get fd for regular files and directories only
@@ -1087,12 +1019,8 @@ static int proxy_ioc_getversion(FsContext *fs_ctx,
V9fsPath *path,
errno = ENOTTY;
return -1;
}
- err = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, "s", path);
- if (err < 0) {
- errno = -err;
- err = -1;
- }
- return err;
+ retval = v9fs_request(fs_ctx->private, T_GETVERSION, st_gen, "s", path);
+ return retval;
}
static int connect_namedsocket(const char *path)
--
2.1.4
- [Qemu-devel] [PATCH v3 0/3] RFC: 9pfs-proxy: simplify/cleanup, Michael Tokarev, 2015/03/06
- [Qemu-devel] [PATCH 2/3] fsdev: introduce v9fs_vmarshal() and v9fs_vunmarshal(), Michael Tokarev, 2015/03/06
- [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling,
Michael Tokarev <=
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Eric Blake, 2015/03/07
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/07
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/07
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Aneesh Kumar K.V, 2015/03/08
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/10
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Aneesh Kumar K.V, 2015/03/10
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/11
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Aneesh Kumar K.V, 2015/03/11
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/11
- Re: [Qemu-devel] [PATCH 1/3] 9pfs-proxy: simplify error handling, Michael Tokarev, 2015/03/11