Skip to content

Commit f4f3529

Browse files
codyprimemdroth
authored andcommitted
block/nfs: fix mutex assertion in nfs_file_close()
Commit c096358 introduced assertion checks for when qemu_mutex() functions are called without the corresponding qemu_mutex_init() having initialized the mutex. This uncovered a latent bug in qemu's nfs driver - in nfs_client_close(), the NFSClient structure is overwritten with zeros, prior to the mutex being destroyed. Go ahead and destroy the mutex in nfs_client_close(), and change where we call qemu_mutex_init() so that it is correctly balanced. There are also a couple of memory leaks obscured by the memset, so this fixes those as well. Finally, we should be able to get rid of the memset(), as it isn't necessary. Cc: [email protected] Signed-off-by: Jeff Cody <[email protected]> Reviewed-by: Peter Lieven <[email protected]> Reviewed-by: Stefan Hajnoczi <[email protected]> Reviewed-by: John Snow <[email protected]> Signed-off-by: Kevin Wolf <[email protected]> (cherry picked from commit 113fe79) Signed-off-by: Michael Roth <[email protected]>
1 parent 5f7f7e4 commit f4f3529

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

block/nfs.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,19 +434,23 @@ static void nfs_client_close(NFSClient *client)
434434
if (client->context) {
435435
if (client->fh) {
436436
nfs_close(client->context, client->fh);
437+
client->fh = NULL;
437438
}
438439
aio_set_fd_handler(client->aio_context, nfs_get_fd(client->context),
439440
false, NULL, NULL, NULL, NULL);
440441
nfs_destroy_context(client->context);
442+
client->context = NULL;
441443
}
442-
memset(client, 0, sizeof(NFSClient));
444+
g_free(client->path);
445+
qemu_mutex_destroy(&client->mutex);
446+
qapi_free_NFSServer(client->server);
447+
client->server = NULL;
443448
}
444449

445450
static void nfs_file_close(BlockDriverState *bs)
446451
{
447452
NFSClient *client = bs->opaque;
448453
nfs_client_close(client);
449-
qemu_mutex_destroy(&client->mutex);
450454
}
451455

452456
static NFSServer *nfs_config(QDict *options, Error **errp)
@@ -499,6 +503,7 @@ static int64_t nfs_client_open(NFSClient *client, QDict *options,
499503
struct stat st;
500504
char *file = NULL, *strp = NULL;
501505

506+
qemu_mutex_init(&client->mutex);
502507
opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
503508
qemu_opts_absorb_qdict(opts, options, &local_err);
504509
if (local_err) {
@@ -661,7 +666,7 @@ static int nfs_file_open(BlockDriverState *bs, QDict *options, int flags,
661666
if (ret < 0) {
662667
return ret;
663668
}
664-
qemu_mutex_init(&client->mutex);
669+
665670
bs->total_sectors = ret;
666671
ret = 0;
667672
return ret;

0 commit comments

Comments
 (0)