mkfs: completely abstract the dev IO during format
authorJP Abgrall <jpa@google.com>
Sat, 11 Jan 2014 05:16:10 +0000 (21:16 -0800)
committerJaegeuk Kim <jaegeuk.kim@samsung.com>
Mon, 13 Jan 2014 01:21:08 +0000 (10:21 +0900)
This change allows for f2fs_format to work on a non-standard device (e.g.
sparse-memory backed file).
Removes direct access to config.fd from within f2fs_format.c.

Now, f2fs_format_device() can be made to work against any device by
providing
 f2fs_finalize_device()
 f2fs_trim_device()
and the lib2fs.c functions
  ASCIIToUNICODE
  dev_write
  f2fs_cal_crc32
  f2fs_set_bit
  log_base_2

This will allow Android's fastboot to use f2fs against a libsparse.

Signed-off-by: JP Abgrall <jpa@google.com>
Signed-off-by: Jaegeuk Kim <jaegeuk.kim@samsung.com>
mkfs/Makefile.am
mkfs/f2fs_format.c
mkfs/f2fs_format_utils.c [new file with mode: 0644]
mkfs/f2fs_format_utils.h [new file with mode: 0644]

index 08432da..79cbec2 100644 (file)
@@ -3,5 +3,5 @@
 AM_CPPFLAGS = ${libuuid_CFLAGS} -I$(top_srcdir)/include
 AM_CFLAGS = -Wall
 sbin_PROGRAMS = mkfs.f2fs
-mkfs_f2fs_SOURCES = f2fs_format.c
+mkfs_f2fs_SOURCES = f2fs_format.c f2fs_format_utils.c
 mkfs_f2fs_LDADD = ${libuuid_LIBS} $(top_builddir)/lib/libf2fs.la
index 18ded79..60c8231 100644 (file)
@@ -22,6 +22,7 @@
 #include <uuid/uuid.h>
 
 #include "f2fs_fs.h"
+#include "f2fs_format_utils.h"
 
 extern struct f2fs_configuration config;
 struct f2fs_super_block super_block;
@@ -908,33 +909,6 @@ exit:
        return err;
 }
 
-int f2fs_trim_device()
-{
-       unsigned long long range[2];
-       struct stat stat_buf;
-
-       if (!config.trim)
-               return 0;
-
-       range[0] = 0;
-       range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE;
-
-       if (fstat(config.fd, &stat_buf) < 0 ) {
-               MSG(1, "\tError: Failed to get the device stat!!!\n");
-               return -1;
-       }
-
-       MSG(0, "Info: Discarding device\n");
-       if (S_ISREG(stat_buf.st_mode))
-               return 0;
-       else if (S_ISBLK(stat_buf.st_mode)) {
-               if (ioctl(config.fd, BLKDISCARD, &range) < 0)
-                       MSG(0, "Info: This device doesn't support TRIM\n");
-       } else
-               return -1;
-       return 0;
-}
-
 static int f2fs_format_device(void)
 {
        int err = 0;
@@ -984,15 +958,7 @@ exit:
        if (err)
                MSG(0, "\tError: Could not format the device!!!\n");
 
-       /*
-        * 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");
+       f2fs_finalize_device();
 
        return err;
 }
diff --git a/mkfs/f2fs_format_utils.c b/mkfs/f2fs_format_utils.c
new file mode 100644 (file)
index 0000000..4a43405
--- /dev/null
@@ -0,0 +1,60 @@
+/**
+ * f2fs_format_utils.c
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *             http://www.samsung.com/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <linux/fs.h>
+
+#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];
+       struct stat stat_buf;
+
+       if (!config.trim)
+               return 0;
+
+       range[0] = 0;
+       range[1] = config.total_sectors * DEFAULT_SECTOR_SIZE;
+
+       if (fstat(config.fd, &stat_buf) < 0 ) {
+               MSG(1, "\tError: Failed to get the device stat!!!\n");
+               return -1;
+       }
+
+       MSG(0, "Info: Discarding device\n");
+       if (S_ISREG(stat_buf.st_mode))
+               return 0;
+       else if (S_ISBLK(stat_buf.st_mode)) {
+               if (ioctl(config.fd, BLKDISCARD, &range) < 0)
+                       MSG(0, "Info: This device doesn't support TRIM\n");
+       } else
+               return -1;
+       return 0;
+}
+
diff --git a/mkfs/f2fs_format_utils.h b/mkfs/f2fs_format_utils.h
new file mode 100644 (file)
index 0000000..745be64
--- /dev/null
@@ -0,0 +1,18 @@
+/**
+ * f2fs_format_utils.c
+ *
+ * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ *             http://www.samsung.com/
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2 as
+ * published by the Free Software Foundation.
+ */
+#define _LARGEFILE64_SOURCE
+
+#include "f2fs_fs.h"
+
+extern struct f2fs_configuration config;
+
+void f2fs_finalize_device();
+int f2fs_trim_device();