qemu with hax to log dma reads & writes jcs.org/2018/11/12/vfio

nbd: Always call "close_fn" in nbd_client_new

Rename the parameter "close" to "close_fn" to disambiguous with
close(2).

This unifies error handling paths of NBDClient allocation:
nbd_client_new will shutdown the socket and call the "close_fn" callback
if negotiation failed, so the caller don't need a different path than
the normal close.

The returned pointer is never used, make it void in preparation for the
next patch.

Signed-off-by: Fam Zheng <famz@redhat.com>
Message-Id: <1452760863-25350-2-git-send-email-famz@redhat.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

authored by

Fam Zheng and committed by
Paolo Bonzini
ee7d7aab e1dc6815

+11 -18
+2 -3
blockdev-nbd.c
··· 27 27 socklen_t addr_len = sizeof(addr); 28 28 29 29 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); 30 - if (fd >= 0 && !nbd_client_new(NULL, fd, nbd_client_put)) { 31 - shutdown(fd, 2); 32 - close(fd); 30 + if (fd >= 0) { 31 + nbd_client_new(NULL, fd, nbd_client_put); 33 32 } 34 33 } 35 34
+1 -2
include/block/nbd.h
··· 98 98 void nbd_export_set_name(NBDExport *exp, const char *name); 99 99 void nbd_export_close_all(void); 100 100 101 - NBDClient *nbd_client_new(NBDExport *exp, int csock, 102 - void (*close)(NBDClient *)); 101 + void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *)); 103 102 void nbd_client_get(NBDClient *client); 104 103 void nbd_client_put(NBDClient *client); 105 104
+5 -6
nbd.c
··· 1475 1475 } 1476 1476 } 1477 1477 1478 - NBDClient *nbd_client_new(NBDExport *exp, int csock, 1479 - void (*close)(NBDClient *)) 1478 + void nbd_client_new(NBDExport *exp, int csock, void (*close_fn)(NBDClient *)) 1480 1479 { 1481 1480 NBDClient *client; 1482 1481 client = g_malloc0(sizeof(NBDClient)); ··· 1485 1484 client->sock = csock; 1486 1485 client->can_read = true; 1487 1486 if (nbd_send_negotiate(client)) { 1488 - g_free(client); 1489 - return NULL; 1487 + shutdown(client->sock, 2); 1488 + close_fn(client); 1489 + return; 1490 1490 } 1491 - client->close = close; 1491 + client->close = close_fn; 1492 1492 qemu_co_mutex_init(&client->send_lock); 1493 1493 nbd_set_handlers(client); 1494 1494 ··· 1496 1496 QTAILQ_INSERT_TAIL(&exp->clients, client, next); 1497 1497 nbd_export_get(exp); 1498 1498 } 1499 - return client; 1500 1499 }
+3 -7
qemu-nbd.c
··· 333 333 return; 334 334 } 335 335 336 - if (nbd_client_new(exp, fd, nbd_client_closed)) { 337 - nb_fds++; 338 - nbd_update_server_fd_handler(server_fd); 339 - } else { 340 - shutdown(fd, 2); 341 - close(fd); 342 - } 336 + nb_fds++; 337 + nbd_update_server_fd_handler(server_fd); 338 + nbd_client_new(exp, fd, nbd_client_closed); 343 339 } 344 340 345 341 static void nbd_update_server_fd_handler(int fd)