• V
    nbd: read_sync and friends: return 0 on success · f5d406fe
    Vladimir Sementsov-Ogievskiy 提交于
    functions read_sync, drop_sync, write_sync, and also
    nbd_negotiate_write, nbd_negotiate_read, nbd_negotiate_drop_sync
    returns number of processed bytes. But what this number can be,
    except requested number of bytes?
    
    Actually, underlying nbd_wr_syncv function returns a value >= 0 and
    != requested_bytes only on eof on read operation. So, firstly, it is
    impossible on write (let's add an assert) and on read it actually
    means, that communication is broken (except nbd_receive_reply, see
    below).
    
    Most of callers operate like this:
       if (func(..., size) != size) {
           /* error path */
       }
    , i.e.:
      1. They are not interested in partial success
      2. Extra duplications in code (especially bad are duplications of
         magic numbers)
      3. User doesn't see actual error message, as return code is lost.
         (this patch doesn't fix this point, but it makes fixing easier)
    
    Several callers handles ret >= 0 and != requested-size separately, by
    just returning EINVAL in this case. This patch makes read_sync and
    friends return EINVAL in this case, so final behavior is the same.
    
    And only one caller - nbd_receive_reply() does something not so
    obvious. It returns EINVAL for ret > 0 and != requested-size, like
    previous group, but for ret == 0 it returns 0. The only caller of
    nbd_receive_reply() - nbd_read_reply_entry() handles ret == 0 in the
    same way as ret < 0, so for now it doesn't matter. However, in
    following commits error path handling will be improved and we'll need
    to distinguish success from fail in this case too. So, this patch adds
    separate helper for this case - read_sync_eof.
    Signed-off-by: NVladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
    Message-Id: <20170516094533.6160-3-vsementsov@virtuozzo.com>
    Signed-off-by: NPaolo Bonzini <pbonzini@redhat.com>
    f5d406fe
server.c 38.7 KB