[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PULL 50/53] block: report errno when flock fcntl fails
From: |
Max Reitz |
Subject: |
[PULL 50/53] block: report errno when flock fcntl fails |
Date: |
Tue, 26 Jan 2021 15:20:13 +0100 |
From: David Edmondson <david.edmondson@oracle.com>
When a call to fcntl(2) for the purpose of adding 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.
No errors are elided when removing file locks.
Signed-off-by: David Edmondson <david.edmondson@oracle.com>
Message-Id: <20210113164447.2545785-1-david.edmondson@oracle.com>
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
---
block/file-posix.c | 38 ++++++++++++++++++++++++++++----------
1 file changed, 28 insertions(+), 10 deletions(-)
diff --git a/block/file-posix.c b/block/file-posix.c
index 00cdaaa2d4..11aafa9d82 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -216,6 +216,20 @@ typedef struct RawPosixAIOData {
static int cdrom_reopen(BlockDriverState *bs);
#endif
+/*
+ * Elide EAGAIN and EACCES details when failing to lock, as this
+ * indicates that the specified file region is already locked by
+ * another process, which is considered a common scenario.
+ */
+#define raw_lock_error_setg_errno(errp, err, fmt, ...) \
+ do { \
+ if ((err) == EAGAIN || (err) == EACCES) { \
+ error_setg((errp), (fmt), ## __VA_ARGS__); \
+ } else { \
+ error_setg_errno((errp), (err), (fmt), ## __VA_ARGS__); \
+ } \
+ } while (0)
+
#if defined(__NetBSD__)
static int raw_normalize_devicepath(const char **filename, Error **errp)
{
@@ -836,7 +850,8 @@ 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);
+ raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d",
+ off);
return ret;
} else if (s) {
s->locked_perm |= bit;
@@ -844,7 +859,7 @@ 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);
+ error_setg_errno(errp, -ret, "Failed to unlock byte %d", off);
return ret;
} else if (s) {
s->locked_perm &= ~bit;
@@ -857,7 +872,8 @@ 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);
+ raw_lock_error_setg_errno(errp, -ret, "Failed to lock byte %d",
+ off);
return ret;
} else if (s) {
s->locked_shared_perm |= bit;
@@ -866,7 +882,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 +906,10 @@ 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);
+
+ raw_lock_error_setg_errno(errp, -ret,
+ "Failed to get \"%s\" lock",
+ perm_name);
g_free(perm_name);
return ret;
}
@@ -905,9 +922,10 @@ 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);
+
+ raw_lock_error_setg_errno(errp, -ret,
+ "Failed to get shared \"%s\" lock",
+ perm_name);
g_free(perm_name);
return ret;
}
--
2.29.2
- [PULL 40/53] iotests: 257: prepare for backup over block-copy, (continued)
- [PULL 40/53] iotests: 257: prepare for backup over block-copy, Max Reitz, 2021/01/26
- [PULL 41/53] block/block-copy: make progress_bytes_callback optional, Max Reitz, 2021/01/26
- [PULL 42/53] block/backup: drop extra gotos from backup_run(), Max Reitz, 2021/01/26
- [PULL 43/53] backup: move to block-copy, Max Reitz, 2021/01/26
- [PULL 44/53] qapi: backup: disable copy_range by default, Max Reitz, 2021/01/26
- [PULL 48/53] simplebench: bench_block_job: add cmd_options argument, Max Reitz, 2021/01/26
- [PULL 45/53] block/block-copy: drop unused block_copy_set_progress_callback(), Max Reitz, 2021/01/26
- [PULL 49/53] simplebench: add bench-backup.py, Max Reitz, 2021/01/26
- [PULL 47/53] simplebench/bench_block_job: use correct shebang line with python3, Max Reitz, 2021/01/26
- [PULL 46/53] block/block-copy: drop unused argument of block_copy(), Max Reitz, 2021/01/26
- [PULL 50/53] block: report errno when flock fcntl fails,
Max Reitz <=
- [PULL 52/53] iotests/118: Drop 'change' test, Max Reitz, 2021/01/26
- [PULL 51/53] iotests: Add test for the regression fixed in c8bf9a9169, Max Reitz, 2021/01/26
- [PULL 53/53] iotests/178: Pass value to invalid option, Max Reitz, 2021/01/26
- Re: [PULL 00/53] Block patches, Peter Maydell, 2021/01/27