libf2fs, fsck, mkfs: call f2fs_finalize_device before exit

The fsck tool should call f2fs_finalize_device before exit to close the device
file.

Signed-off-by: Jaegeuk Kim <jaegeuk@kernel.org>
diff --git a/fsck/main.c b/fsck/main.c
index 5aa3956..46f5d04 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -203,6 +203,9 @@
 	}
 
 	f2fs_do_umount(sbi);
+
+	f2fs_finalize_device(&config);
+
 	printf("\nDone.\n");
 	return ret;
 }
diff --git a/include/f2fs_fs.h b/include/f2fs_fs.h
index 4f44866..53b8cb9 100644
--- a/include/f2fs_fs.h
+++ b/include/f2fs_fs.h
@@ -659,6 +659,7 @@
 extern void f2fs_init_configuration(struct f2fs_configuration *);
 extern int f2fs_dev_is_umounted(struct f2fs_configuration *);
 extern int f2fs_get_device_info(struct f2fs_configuration *);
+extern void f2fs_finalize_device(struct f2fs_configuration *);
 
 extern int dev_read(void *, __u64, size_t);
 extern int dev_write(void *, __u64, size_t);
diff --git a/lib/libf2fs.c b/lib/libf2fs.c
index 6168c5c..8d6c670 100644
--- a/lib/libf2fs.c
+++ b/lib/libf2fs.c
@@ -493,3 +493,15 @@
 	return 0;
 }
 
+void f2fs_finalize_device(struct f2fs_configuration *c)
+{
+	/*
+	 * We should call fsync() to flush out all the dirty pages
+	 * in the block device page cache.
+	 */
+	if (fsync(c->fd) < 0)
+		MSG(0, "\tError: Could not conduct fsync!!!\n");
+
+	if (close(c->fd) < 0)
+		MSG(0, "\tError: Failed to close device file!!!\n");
+}
diff --git a/mkfs/f2fs_format.c b/mkfs/f2fs_format.c
index c7c6971..1568545 100644
--- a/mkfs/f2fs_format.c
+++ b/mkfs/f2fs_format.c
@@ -880,7 +880,5 @@
 	if (err)
 		MSG(0, "\tError: Could not format the device!!!\n");
 
-	f2fs_finalize_device();
-
 	return err;
 }
diff --git a/mkfs/f2fs_format_main.c b/mkfs/f2fs_format_main.c
index 2b6c195..19c52e4 100644
--- a/mkfs/f2fs_format_main.c
+++ b/mkfs/f2fs_format_main.c
@@ -129,6 +129,8 @@
 	if (f2fs_format_device() < 0)
 		return -1;
 
+	f2fs_finalize_device(&config);
+
 	MSG(0, "Info: format successful\n");
 
 	return 0;
diff --git a/mkfs/f2fs_format_utils.c b/mkfs/f2fs_format_utils.c
index f4c3767..5cc2a32 100644
--- a/mkfs/f2fs_format_utils.c
+++ b/mkfs/f2fs_format_utils.c
@@ -15,20 +15,6 @@
 
 #include "f2fs_fs.h"
 
-void f2fs_finalize_device()
-{
-	/*
-	 * We should call fsync() to flush out all the dirty pages
-	 * in the block device page cache.
-	 */
-	if (fsync(config.fd) < 0)
-		MSG(0, "\tError: Could not conduct fsync!!!\n");
-
-	if (close(config.fd) < 0)
-		MSG(0, "\tError: Failed to close device file!!!\n");
-
-}
-
 int f2fs_trim_device()
 {
 	unsigned long long range[2];
diff --git a/mkfs/f2fs_format_utils.h b/mkfs/f2fs_format_utils.h
index fb731fc..9eb2cea 100644
--- a/mkfs/f2fs_format_utils.h
+++ b/mkfs/f2fs_format_utils.h
@@ -12,6 +12,5 @@
 
 extern struct f2fs_configuration config;
 
-void f2fs_finalize_device(void);
 int f2fs_trim_device(void);
 int f2fs_format_device(void);