From: David Edmondson <david.edmondson@oracle.com>
To: qemu-devel@nongnu.org
Cc: "Kevin Wolf" <kwolf@redhat.com>,
"Vladimir Sementsov-Ogievskiy" <vsementsov@virtuozzo.com>,
qemu-block@nongnu.org, "Philippe Mathieu-Daudé" <f4bug@amsat.org>,
"Max Reitz" <mreitz@redhat.com>,
"David Edmondson" <david.edmondson@oracle.com>
Subject: [PATCH v4] block: report errno when flock fcntl fails
Date: Tue, 12 Jan 2021 15:27:26 +0000 [thread overview]
Message-ID: <20210112152726.2217792-1-david.edmondson@oracle.com> (raw)
When a call to fcntl(2) for the purpose of manipulating file locks
fails with an error other than EAGAIN or EACCES, report the error
returned by fcntl.
EAGAIN or EACCES are elided as they are considered to be common
failures, indicating that a conflicting lock is held by another
process.
Signed-off-by: David Edmondson <david.edmondson@oracle.com>
---
v3:
- Remove the now unnecessary updates to the test framework (Max).
- Elide the error detail for EAGAIN or EACCES when locking (Kevin,
sort-of Max).
- Philippe and Vladimir sent Reviewed-by, but things have changed
noticeably, so I didn't add them (dme).
v4:
- Really, really remove the unnecessary updates to the test framework.
block/file-posix.c | 52 +++++++++++++++++++++++++++++++++++++---------
1 file changed, 42 insertions(+), 10 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index 00cdaaa2d4..c5142f7ffa 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -836,7 +836,13 @@ static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
if ((perm_lock_bits & bit) && !(locked_perm & bit)) {
ret = qemu_lock_fd(fd, off, 1, false);
if (ret) {
- error_setg(errp, "Failed to lock byte %d", off);
+ int err = -ret;
+
+ if (err == EAGAIN || err == EACCES) {
+ error_setg(errp, "Failed to lock byte %d", off);
+ } else {
+ error_setg_errno(errp, err, "Failed to lock byte %d", off);
+ }
return ret;
} else if (s) {
s->locked_perm |= bit;
@@ -844,7 +850,13 @@ static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
} else if (unlock && (locked_perm & bit) && !(perm_lock_bits & bit)) {
ret = qemu_unlock_fd(fd, off, 1);
if (ret) {
- error_setg(errp, "Failed to unlock byte %d", off);
+ int err = -ret;
+
+ if (err == EAGAIN || err == EACCES) {
+ error_setg(errp, "Failed to lock byte %d", off);
+ } else {
+ error_setg_errno(errp, err, "Failed to lock byte %d", off);
+ }
return ret;
} else if (s) {
s->locked_perm &= ~bit;
@@ -857,7 +869,13 @@ static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
if ((shared_perm_lock_bits & bit) && !(locked_shared_perm & bit)) {
ret = qemu_lock_fd(fd, off, 1, false);
if (ret) {
- error_setg(errp, "Failed to lock byte %d", off);
+ int err = -ret;
+
+ if (err == EAGAIN || err == EACCES) {
+ error_setg(errp, "Failed to lock byte %d", off);
+ } else {
+ error_setg_errno(errp, err, "Failed to lock byte %d", off);
+ }
return ret;
} else if (s) {
s->locked_shared_perm |= bit;
@@ -866,7 +884,7 @@ static int raw_apply_lock_bytes(BDRVRawState *s, int fd,
!(shared_perm_lock_bits & bit)) {
ret = qemu_unlock_fd(fd, off, 1);
if (ret) {
- error_setg(errp, "Failed to unlock byte %d", off);
+ error_setg_errno(errp, -ret, "Failed to unlock byte %d", off);
return ret;
} else if (s) {
s->locked_shared_perm &= ~bit;
@@ -890,9 +908,16 @@ static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm,
ret = qemu_lock_fd_test(fd, off, 1, true);
if (ret) {
char *perm_name = bdrv_perm_names(p);
- error_setg(errp,
- "Failed to get \"%s\" lock",
- perm_name);
+ int err = -ret;
+
+ if (err == EAGAIN || err == EACCES) {
+ error_setg(errp, "Failed to get \"%s\" lock",
+ perm_name);
+ } else {
+ error_setg_errno(errp, err,
+ "Failed to get \"%s\" lock",
+ perm_name);
+ }
g_free(perm_name);
return ret;
}
@@ -905,9 +930,16 @@ static int raw_check_lock_bytes(int fd, uint64_t perm, uint64_t shared_perm,
ret = qemu_lock_fd_test(fd, off, 1, true);
if (ret) {
char *perm_name = bdrv_perm_names(p);
- error_setg(errp,
- "Failed to get shared \"%s\" lock",
- perm_name);
+ int err = -ret;
+
+ if (err == EAGAIN || err == EACCES) {
+ error_setg(errp, "Failed to get shared \"%s\" lock",
+ perm_name);
+ } else {
+ error_setg_errno(errp, err,
+ "Failed to get shared \"%s\" lock",
+ perm_name);
+ }
g_free(perm_name);
return ret;
}
--
2.29.2
next reply other threads:[~2021-01-12 15:30 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-12 15:27 David Edmondson [this message]
2021-01-13 10:26 ` [PATCH v4] block: report errno when flock fcntl fails Vladimir Sementsov-Ogievskiy
2021-01-13 10:41 ` David Edmondson
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210112152726.2217792-1-david.edmondson@oracle.com \
--to=david.edmondson@oracle.com \
--cc=f4bug@amsat.org \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
--cc=vsementsov@virtuozzo.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).