mmap engine: remove code duplication

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/engines/mmap.c b/engines/mmap.c
index 71abfb9..53fd358 100644
--- a/engines/mmap.c
+++ b/engines/mmap.c
@@ -75,12 +75,6 @@
 		return EIO;
 	}
 
-	if (f->mmap_ptr) {
-		if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
-			return errno;
-		f->mmap_ptr = NULL;
-	}
-
 	f->mmap_sz = mmap_map_size;
 	if (f->mmap_sz  > f->io_size)
 		f->mmap_sz = f->io_size;
@@ -101,12 +95,6 @@
 	if (fio_file_partial_mmap(f))
 		return EINVAL;
 
-	if (f->mmap_ptr) {
-		if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
-			return errno;
-		f->mmap_ptr = NULL;
-	}
-
 	f->mmap_sz = f->io_size;
 	f->mmap_off = 0;
 
@@ -122,10 +110,22 @@
 	struct fio_file *f = io_u->file;
 	int ret;
 
+	/*
+	 * It fits within existing mapping, use it
+	 */
 	if (io_u->offset >= f->mmap_off &&
 	    io_u->offset + io_u->buflen < f->mmap_off + f->mmap_sz)
 		goto done;
 
+	/*
+	 * unmap any existing mapping
+	 */
+	if (f->mmap_ptr) {
+		if (munmap(f->mmap_ptr, f->mmap_sz) < 0)
+			return errno;
+		f->mmap_ptr = NULL;
+	}
+
 	if (fio_mmapio_prep_full(td, io_u)) {
 		td_clear_error(td);
 		ret = fio_mmapio_prep_limited(td, io_u);