Fix race on io_u->flags

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/io_u.c b/io_u.c
index 232f6a6..4be958d 100644
--- a/io_u.c
+++ b/io_u.c
@@ -413,7 +413,6 @@
 {
 	td_io_u_lock(td);
 
-	assert((io_u->flags & IO_U_F_FREE) == 0);
 	io_u->flags |= IO_U_F_FREE;
 	io_u->flags &= ~IO_U_F_FREE_DEF;
 
@@ -861,8 +860,7 @@
 
 	if (io_u) {
 		assert(io_u->flags & IO_U_F_FREE);
-		io_u->flags &= ~IO_U_F_FREE;
-		io_u->flags &= ~IO_U_F_FREE_DEF;
+		io_u->flags &= ~(IO_U_F_FREE | IO_U_F_FREE_DEF);
 
 		io_u->error = 0;
 		flist_del(&io_u->list);
@@ -971,8 +969,10 @@
 
 	dprint_io_u(io_u, "io complete");
 
+	td_io_u_lock(td);
 	assert(io_u->flags & IO_U_F_FLIGHT);
 	io_u->flags &= ~IO_U_F_FLIGHT;
+	td_io_u_unlock(td);
 
 	if (ddir_sync(io_u->ddir)) {
 		td->last_was_sync = 1;