[PATCH] Close files on error

The error path doesn't close already opened files. Also set
O_SYNC regardless of file type.

Signed-off-by: Jens Axboe <jens.axboe@oracle.com>
diff --git a/filesetup.c b/filesetup.c
index 4fa04ec..50434e6 100644
--- a/filesetup.c
+++ b/filesetup.c
@@ -317,18 +317,18 @@
 
 	if (td->odirect)
 		flags |= OS_O_DIRECT;
+	if (td->sync_io)
+		flags |= O_SYNC;
 
 	if (td_write(td) || td_rw(td)) {
+		flags |= O_RDWR;
+
 		if (td->filetype == FIO_TYPE_FILE) {
 			if (!td->overwrite)
 				flags |= O_TRUNC;
 
 			flags |= O_CREAT;
 		}
-		if (td->sync_io)
-			flags |= O_SYNC;
-
-		flags |= O_RDWR;
 
 		f->fd = open(f->file_name, flags, 0600);
 	} else {
@@ -362,6 +362,16 @@
 			break;
 	}
 
+	if (!err)
+		return 0;
+
+	for_each_file(td, f, i) {
+		if (f->fd != -1) {
+			close(f->fd);
+			f->fd = -1;
+		}
+	}
+
 	return err;
 }