locking: Add support for sanlock_strerror

The recently added sanlock_strerror function can be used to translate
sanlock's numeric errors into human readable strings.

https://bugzilla.redhat.com/show_bug.cgi?id=1409511

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
This commit is contained in:
Jiri Denemark 2017-03-31 21:42:22 +02:00
parent ac58c03606
commit 23377c539b
2 changed files with 115 additions and 46 deletions

View File

@ -61,6 +61,13 @@ AC_DEFUN([LIBVIRT_CHECK_SANLOCK],[
[whether sanlock supports sanlock_write_lockspace]) [whether sanlock supports sanlock_write_lockspace])
fi fi
AC_CHECK_LIB([sanlock_client], [sanlock_strerror],
[sanlock_strerror=yes], [sanlock_strerror=no])
if test "x$sanlock_strerror" = "xyes" ; then
AC_DEFINE_UNQUOTED([HAVE_SANLOCK_STRERROR], 1,
[whether sanlock supports sanlock_strerror])
fi
CPPFLAGS="$old_cppflags" CPPFLAGS="$old_cppflags"
LIBS="$old_libs" LIBS="$old_libs"
fi fi

View File

@ -97,6 +97,25 @@ struct _virLockManagerSanlockPrivate {
bool registered; bool registered;
}; };
static bool
ATTRIBUTE_NONNULL(2)
virLockManagerSanlockError(int err,
char **message)
{
if (err <= -200) {
#if HAVE_SANLOCK_STRERROR
ignore_value(VIR_STRDUP_QUIET(*message, sanlock_strerror(err)));
#else
ignore_value(virAsprintfQuiet(message, _("sanlock error %d"), err));
#endif
return true;
} else {
return false;
}
}
/* /*
* sanlock plugin for the libvirt virLockManager API * sanlock plugin for the libvirt virLockManager API
*/ */
@ -263,14 +282,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
} }
if ((rv = sanlock_align(&ls.host_id_disk)) < 0) { if ((rv = sanlock_align(&ls.host_id_disk)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to query sector size %s: error %d"), _("Unable to query sector size %s: %s"),
path, rv); path, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to query sector size %s"), _("Unable to query sector size %s"),
path); path);
}
goto error_unlink; goto error_unlink;
} }
@ -292,14 +314,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
} }
if ((rv = virLockManagerSanlockInitLockspace(driver, &ls) < 0)) { if ((rv = virLockManagerSanlockInitLockspace(driver, &ls) < 0)) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to initialize lockspace %s: error %d"), _("Unable to initialize lockspace %s: %s"),
path, rv); path, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to initialize lockspace %s"), _("Unable to initialize lockspace %s"),
path); path);
}
goto error_unlink; goto error_unlink;
} }
VIR_DEBUG("Lockspace %s has been initialized", path); VIR_DEBUG("Lockspace %s has been initialized", path);
@ -362,14 +387,17 @@ virLockManagerSanlockSetupLockspace(virLockManagerSanlockDriverPtr driver)
goto retry; goto retry;
} }
if (-rv != EEXIST) { if (-rv != EEXIST) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to add lockspace %s: error %d"), _("Unable to add lockspace %s: %s"),
path, rv); path, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to add lockspace %s"), _("Unable to add lockspace %s"),
path); path);
}
goto error; goto error;
} else { } else {
VIR_DEBUG("Lockspace %s is already registered", path); VIR_DEBUG("Lockspace %s is already registered", path);
@ -694,14 +722,17 @@ virLockManagerSanlockCreateLease(virLockManagerSanlockDriverPtr driver,
} }
if ((rv = sanlock_align(&res->disks[0])) < 0) { if ((rv = sanlock_align(&res->disks[0])) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to query sector size %s: error %d"), _("Unable to query sector size %s: %s"),
res->disks[0].path, rv); res->disks[0].path, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to query sector size %s"), _("Unable to query sector size %s"),
res->disks[0].path); res->disks[0].path);
}
goto error_unlink; goto error_unlink;
} }
@ -723,14 +754,17 @@ virLockManagerSanlockCreateLease(virLockManagerSanlockDriverPtr driver,
} }
if ((rv = sanlock_init(NULL, res, 0, 0)) < 0) { if ((rv = sanlock_init(NULL, res, 0, 0)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to initialize lease %s: error %d"), _("Unable to initialize lease %s: %s"),
res->disks[0].path, rv); res->disks[0].path, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to initialize lease %s"), _("Unable to initialize lease %s"),
res->disks[0].path); res->disks[0].path);
}
goto error_unlink; goto error_unlink;
} }
VIR_DEBUG("Lease %s has been initialized", res->disks[0].path); VIR_DEBUG("Lease %s has been initialized", res->disks[0].path);
@ -867,10 +901,12 @@ virLockManagerSanlockRegisterKillscript(int sock,
} }
if ((rv = sanlock_killpath(sock, 0, path, args)) < 0) { if ((rv = sanlock_killpath(sock, 0, path, args)) < 0) {
if (rv <= -200) { char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to register lock failure action:" _("Failed to register lock failure action: %s"),
" error %d"), rv); NULLSTR(err));
VIR_FREE(err);
} else { } else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to register lock failure" _("Failed to register lock failure"
@ -934,13 +970,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
if (priv->vm_pid == getpid()) { if (priv->vm_pid == getpid()) {
VIR_DEBUG("Register sanlock %d", flags); VIR_DEBUG("Register sanlock %d", flags);
if ((sock = sanlock_register()) < 0) { if ((sock = sanlock_register()) < 0) {
if (sock <= -200) char *err = NULL;
if (virLockManagerSanlockError(sock, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to open socket to sanlock daemon: error %d"), _("Failed to open socket to sanlock daemon: %s"),
sock); NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-sock, "%s", virReportSystemError(-sock, "%s",
_("Failed to open socket to sanlock daemon")); _("Failed to open socket to sanlock daemon"));
}
goto error; goto error;
} }
@ -971,14 +1010,17 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
if ((rv = sanlock_state_to_args((char *)state, if ((rv = sanlock_state_to_args((char *)state,
&res_count, &res_count,
&res_args)) < 0) { &res_args)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Unable to parse lock state %s: error %d"), _("Unable to parse lock state %s: %s"),
state, rv); state, NULLSTR(err));
else VIR_FREE(err);
} else {
virReportSystemError(-rv, virReportSystemError(-rv,
_("Unable to parse lock state %s"), _("Unable to parse lock state %s"),
state); state);
}
goto error; goto error;
} }
res_free = true; res_free = true;
@ -992,12 +1034,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
if ((rv = sanlock_acquire(sock, priv->vm_pid, 0, if ((rv = sanlock_acquire(sock, priv->vm_pid, 0,
priv->res_count, priv->res_args, priv->res_count, priv->res_args,
opt)) < 0) { opt)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_RESOURCE_BUSY, virReportError(VIR_ERR_RESOURCE_BUSY,
_("Failed to acquire lock: error %d"), rv); _("Failed to acquire lock: %s"),
else NULLSTR(err));
VIR_FREE(err);
} else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to acquire lock")); _("Failed to acquire lock"));
}
goto error; goto error;
} }
} }
@ -1016,12 +1062,16 @@ static int virLockManagerSanlockAcquire(virLockManagerPtr lock,
if (flags & VIR_LOCK_MANAGER_ACQUIRE_RESTRICT) { if (flags & VIR_LOCK_MANAGER_ACQUIRE_RESTRICT) {
if ((rv = sanlock_restrict(sock, SANLK_RESTRICT_ALL)) < 0) { if ((rv = sanlock_restrict(sock, SANLK_RESTRICT_ALL)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to restrict process: error %d"), rv); _("Failed to restrict process: %s"),
else NULLSTR(err));
VIR_FREE(err);
} else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to restrict process")); _("Failed to restrict process"));
}
goto error; goto error;
} }
} }
@ -1068,12 +1118,16 @@ static int virLockManagerSanlockRelease(virLockManagerPtr lock,
if (state) { if (state) {
if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) { if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to inquire lock: error %d"), rv); _("Failed to inquire lock: %s"),
else NULLSTR(err));
VIR_FREE(err);
} else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to inquire lock")); _("Failed to inquire lock"));
}
return -1; return -1;
} }
@ -1083,12 +1137,16 @@ static int virLockManagerSanlockRelease(virLockManagerPtr lock,
if ((rv = sanlock_release(-1, priv->vm_pid, 0, res_count, if ((rv = sanlock_release(-1, priv->vm_pid, 0, res_count,
priv->res_args)) < 0) { priv->res_args)) < 0) {
if (rv <= -200) char *err = NULL;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to release lock: error %d"), rv); _("Failed to release lock: %s"),
else NULLSTR(err));
VIR_FREE(err);
} else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to release lock")); _("Failed to release lock"));
}
return -1; return -1;
} }
@ -1118,12 +1176,16 @@ static int virLockManagerSanlockInquire(virLockManagerPtr lock,
} }
if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) { if ((rv = sanlock_inquire(-1, priv->vm_pid, 0, &res_count, state)) < 0) {
if (rv <= -200) char *err;
if (virLockManagerSanlockError(rv, &err)) {
virReportError(VIR_ERR_INTERNAL_ERROR, virReportError(VIR_ERR_INTERNAL_ERROR,
_("Failed to inquire lock: error %d"), rv); _("Failed to inquire lock: %s"),
else NULLSTR(err));
VIR_FREE(err);
} else {
virReportSystemError(-rv, "%s", virReportSystemError(-rv, "%s",
_("Failed to inquire lock")); _("Failed to inquire lock"));
}
return -1; return -1;
} }