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;
             }
