From patchwork Thu May 7 15:26:14 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 469717 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 74E42140281 for ; Fri, 8 May 2015 01:26:46 +1000 (AEST) Received: from localhost ([::1]:51744 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqNhE-0004rg-MQ for incoming@patchwork.ozlabs.org; Thu, 07 May 2015 11:26:44 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:57357) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqNgs-0004Hu-Lo for qemu-devel@nongnu.org; Thu, 07 May 2015 11:26:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1YqNgq-0002HX-Ae for qemu-devel@nongnu.org; Thu, 07 May 2015 11:26:22 -0400 Received: from mx1.redhat.com ([209.132.183.28]:38359) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1YqNgq-0002HR-5H for qemu-devel@nongnu.org; Thu, 07 May 2015 11:26:20 -0400 Received: from int-mx11.intmail.prod.int.phx2.redhat.com (int-mx11.intmail.prod.int.phx2.redhat.com [10.5.11.24]) by mx1.redhat.com (Postfix) with ESMTPS id D211B8EB3E for ; Thu, 7 May 2015 15:26:19 +0000 (UTC) Received: from donizetti.redhat.com (ovpn-112-76.ams2.redhat.com [10.36.112.76]) by int-mx11.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id t47FQFlU006231; Thu, 7 May 2015 11:26:18 -0400 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Thu, 7 May 2015 17:26:14 +0200 Message-Id: <1431012374-14113-2-git-send-email-pbonzini@redhat.com> In-Reply-To: <1431012374-14113-1-git-send-email-pbonzini@redhat.com> References: <1431012374-14113-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.24 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: kwolf@redhat.com, mreitz@redhat.com Subject: [Qemu-devel] [PATCH] qemu-nbd: only send a limited number of errno codes on the wire X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Right now, NBD includes potentially platform-specific error values in the wire protocol. Luckily, most common error values are more or less universal: in particular, of all errno values <= 34 (up to ERANGE), they are all the same on supported platforms except for 11 (which is EAGAIN on Windows and Linux, but EDEADLK on Darwin and the *BSDs). So, in order to guarantee some portability, only keep a dozen possible error codes and squash everything else to EINVAL. Signed-off-by: Paolo Bonzini --- nbd.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/nbd.c b/nbd.c index eea8c51..1ad5b66 100644 --- a/nbd.c +++ b/nbd.c @@ -86,6 +86,37 @@ #define NBD_OPT_ABORT (2) #define NBD_OPT_LIST (3) +/* NBD errors are based on errno numbers, so there is a 1:1 mapping, + * but only a limited set of errno values is specified in the protocol. + * Everything else is squashed to EINVAL. + */ +static int system_errno_to_nbd_errno(int err) +{ + switch (err) { + case EPERM: + return 1; + case EIO: + return 5; + case ENXIO: + return 6; + case E2BIG: + return 7; + case ENOMEM: + return 12; + case EACCES: + return 13; + case EFBIG: + return 27; + case ENOSPC: + return 28; + case EROFS: + return 30; + case EINVAL: + default: + return 22; + } +} + /* Definitions for opaque data types */ typedef struct NBDRequest NBDRequest; @@ -856,6 +887,20 @@ ssize_t nbd_receive_reply(int csock, struct nbd_reply *reply) reply->error = be32_to_cpup((uint32_t*)(buf + 4)); reply->handle = be64_to_cpup((uint64_t*)(buf + 8)); + /* NBD errors should be universally equal to the corresponding + * errno values, check it here. + */ + QEMU_BUILD_BUG_ON(EPERM != 1); + QEMU_BUILD_BUG_ON(EIO != 5); + QEMU_BUILD_BUG_ON(ENXIO != 6); + QEMU_BUILD_BUG_ON(E2BIG != 7); + QEMU_BUILD_BUG_ON(ENOMEM != 12); + QEMU_BUILD_BUG_ON(EACCES != 13); + QEMU_BUILD_BUG_ON(EINVAL != 22); + QEMU_BUILD_BUG_ON(EFBIG != 27); + QEMU_BUILD_BUG_ON(ENOSPC != 28); + QEMU_BUILD_BUG_ON(EROFS != 30); + TRACE("Got reply: " "{ magic = 0x%x, .error = %d, handle = %" PRIu64" }", magic, reply->error, reply->handle); @@ -872,6 +917,8 @@ static ssize_t nbd_send_reply(int csock, struct nbd_reply *reply) uint8_t buf[NBD_REPLY_SIZE]; ssize_t ret; + reply->error = system_errno_to_nbd_errno(reply->error); + /* Reply [ 0 .. 3] magic (NBD_REPLY_MAGIC) [ 4 .. 7] error (0 == no error)