Project import
diff --git a/f2fs_utils/Android.mk b/f2fs_utils/Android.mk
new file mode 100644
index 0000000..82c3ee0
--- /dev/null
+++ b/f2fs_utils/Android.mk
@@ -0,0 +1,102 @@
+# Copyright 2014 The Android Open Source Project
+
+LOCAL_PATH:= $(call my-dir)
+
+ifeq ($(HOST_OS),linux)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libf2fs_utils_host
+LOCAL_SRC_FILES := f2fs_utils.c
+LOCAL_STATIC_LIBRARIES := \
+ libsparse_host \
+ libz
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+LOCAL_CFLAGS := -Wno-unused-parameter
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := f2fs_ioutils.c
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_STATIC_LIBRARIES := \
+ libselinux \
+ libsparse_host \
+ libext2_uuid-host \
+ libz
+LOCAL_MODULE := libf2fs_ioutils_host
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := f2fs_dlutils.c
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+# Will attempt to dlopen("libf2fs_fmt_host_dyn")
+LOCAL_LDLIBS := -ldl
+LOCAL_MODULE := libf2fs_dlutils_host
+include $(BUILD_HOST_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := make_f2fs_main.c
+LOCAL_MODULE := make_f2fs
+# libf2fs_dlutils_host will dlopen("libf2fs_fmt_host_dyn")
+LOCAL_LDFLAGS := -ldl -rdynamic
+# The following libf2fs_* are from system/extras/f2fs_utils,
+# and do not use code in external/f2fs-tools.
+LOCAL_STATIC_LIBRARIES := libf2fs_utils_host libf2fs_ioutils_host libf2fs_dlutils_host
+LOCAL_REQUIRED_MODULES := libf2fs_fmt_host_dyn
+LOCAL_STATIC_LIBRARIES += \
+ libsparse_host \
+ libz
+include $(BUILD_HOST_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libf2fs_dlutils
+LOCAL_SRC_FILES := f2fs_dlutils.c
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+LOCAL_SHARED_LIBRARIES := libdl
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libf2fs_dlutils_static
+LOCAL_SRC_FILES := f2fs_dlutils.c
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+LOCAL_SHARED_LIBRARIES := libdl
+include $(BUILD_STATIC_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libf2fs_utils_static
+LOCAL_SRC_FILES := f2fs_utils.c
+LOCAL_C_INCLUDES := external/f2fs-tools/include external/f2fs-tools/mkfs
+LOCAL_CFLAGS := -Wno-unused-parameter
+LOCAL_STATIC_LIBRARIES := \
+ libsparse_static
+include $(BUILD_STATIC_LIBRARY)
+
+endif
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := libf2fs_sparseblock
+LOCAL_SRC_FILES := f2fs_sparseblock.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_C_INCLUDES := external/f2fs-tools/include \
+ system/core/include/log
+include $(BUILD_SHARED_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := f2fs_sparseblock
+LOCAL_SRC_FILES := f2fs_sparseblock.c
+LOCAL_SHARED_LIBRARIES := liblog libcutils
+LOCAL_C_INCLUDES := external/f2fs-tools/include \
+ system/core/include/log
+include $(BUILD_EXECUTABLE)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := mkf2fsuserimg.sh
+LOCAL_SRC_FILES := mkf2fsuserimg.sh
+LOCAL_MODULE_CLASS := EXECUTABLES
+# We don't need any additional suffix.
+LOCAL_MODULE_SUFFIX :=
+LOCAL_BUILT_MODULE_STEM := $(notdir $(LOCAL_SRC_FILES))
+LOCAL_IS_HOST_MODULE := true
+include $(BUILD_PREBUILT)
+
+
diff --git a/f2fs_utils/MODULE_LICENSE_APACHE2 b/f2fs_utils/MODULE_LICENSE_APACHE2
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/f2fs_utils/MODULE_LICENSE_APACHE2
diff --git a/f2fs_utils/NOTICE b/f2fs_utils/NOTICE
new file mode 100644
index 0000000..5d14293
--- /dev/null
+++ b/f2fs_utils/NOTICE
@@ -0,0 +1,190 @@
+
+ Copyright (c) 2010, The Android Open Source Project
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
diff --git a/f2fs_utils/f2fs_dlutils.c b/f2fs_utils/f2fs_dlutils.c
new file mode 100644
index 0000000..40be416
--- /dev/null
+++ b/f2fs_utils/f2fs_dlutils.c
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define _LARGEFILE64_SOURCE
+
+#include <stdio.h>
+
+#include <dlfcn.h>
+#include <assert.h>
+
+#include <f2fs_fs.h>
+#include <f2fs_format_utils.h>
+#define F2FS_DYN_LIB "libf2fs_fmt_host_dyn.so"
+
+int (*f2fs_format_device_dl)(void);
+void (*f2fs_init_configuration_dl)(struct f2fs_configuration *);
+
+int f2fs_format_device(void) {
+ assert(f2fs_format_device_dl);
+ return f2fs_format_device_dl();
+}
+void f2fs_init_configuration(struct f2fs_configuration *config) {
+ assert(f2fs_init_configuration_dl);
+ f2fs_init_configuration_dl(config);
+}
+
+int dlopenf2fs() {
+ void* f2fs_lib;
+
+ f2fs_lib = dlopen(F2FS_DYN_LIB, RTLD_NOW);
+ if (!f2fs_lib) {
+ return -1;
+ }
+ f2fs_format_device_dl = dlsym(f2fs_lib, "f2fs_format_device");
+ f2fs_init_configuration_dl = dlsym(f2fs_lib, "f2fs_init_configuration");
+ if (!f2fs_format_device_dl || !f2fs_init_configuration_dl) {
+ return -1;
+ }
+ return 0;
+}
diff --git a/f2fs_utils/f2fs_ioutils.c b/f2fs_utils/f2fs_ioutils.c
new file mode 100644
index 0000000..01efd53
--- /dev/null
+++ b/f2fs_utils/f2fs_ioutils.c
@@ -0,0 +1,195 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define _LARGEFILE64_SOURCE
+
+#include <assert.h>
+#include <asm/types.h>
+#include <dlfcn.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <linux/fs.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h> /* memset() */
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <f2fs_fs.h>
+#include <f2fs_format_utils.h>
+
+#include <sparse/sparse.h>
+
+struct selabel_handle;
+
+#include "make_f2fs.h"
+
+#ifdef USE_MINGW
+
+#include <winsock2.h>
+
+/* These match the Linux definitions of these flags.
+ L_xx is defined to avoid conflicting with the win32 versions.
+*/
+#define L_S_IRUSR 00400
+#define L_S_IWUSR 00200
+#define L_S_IXUSR 00100
+#define S_IRWXU (L_S_IRUSR | L_S_IWUSR | L_S_IXUSR)
+#define S_IRGRP 00040
+#define S_IWGRP 00020
+#define S_IXGRP 00010
+#define S_IRWXG (S_IRGRP | S_IWGRP | S_IXGRP)
+#define S_IROTH 00004
+#define S_IWOTH 00002
+#define S_IXOTH 00001
+#define S_IRWXO (S_IROTH | S_IWOTH | S_IXOTH)
+#define S_ISUID 0004000
+#define S_ISGID 0002000
+#define S_ISVTX 0001000
+
+#else
+
+#include <selinux/selinux.h>
+#include <selinux/label.h>
+
+#define O_BINARY 0
+
+#endif
+
+struct f2fs_configuration config;
+struct sparse_file *f2fs_sparse_file;
+
+struct buf_item {
+ void *buf;
+ size_t len;
+ struct buf_item *next;
+};
+
+struct buf_item *buf_list;
+
+static int dev_write_fd(void *buf, __u64 offset, size_t len)
+{
+ if (lseek64(config.fd, (off64_t)offset, SEEK_SET) < 0)
+ return -1;
+ ssize_t written = write(config.fd, buf, len);
+ if (written == -1)
+ return -1;
+ if ((size_t)written != len)
+ return -1;
+ return 0;
+}
+
+void flush_sparse_buffs()
+{
+ while (buf_list) {
+ struct buf_item *bi = buf_list;
+ buf_list = buf_list->next;
+ free(bi->buf);
+ free(bi);
+ }
+}
+
+static int dev_write_sparse(void *buf, __u64 byte_offset, size_t byte_len)
+{
+ struct buf_item *bi = calloc(1, sizeof(struct buf_item));
+
+ if (bi == NULL) {
+ return -ENOMEM;
+ }
+ bi->buf = malloc(byte_len);
+ if (bi->buf == NULL) {
+ free(bi);
+ return -ENOMEM;
+ }
+
+ bi->len = byte_len;
+ memcpy(bi->buf, buf, byte_len);
+ bi->next = buf_list;
+ buf_list = bi;
+
+ sparse_file_add_data(f2fs_sparse_file, bi->buf, byte_len, byte_offset/F2FS_BLKSIZE);
+ return 0;
+}
+
+void f2fs_finalize_device(struct f2fs_configuration *c)
+{
+}
+
+int f2fs_trim_device()
+{
+ return 0;
+}
+
+/*
+ * IO interfaces
+ */
+int dev_read_version(void *buf, __u64 offset, size_t len)
+{
+ return 0;
+}
+
+int dev_read(void *buf, __u64 offset, size_t len)
+{
+ return 0;
+}
+
+int dev_write(void *buf, __u64 offset, size_t len)
+{
+ if (config.fd >= 0) {
+ return dev_write_fd(buf, offset, len);
+ } else {
+ return dev_write_sparse(buf, offset, len);
+ }
+}
+
+
+int dev_fill(void *buf, __u64 offset, size_t len)
+{
+ int ret;
+ if (config.fd >= 0) {
+ return dev_write_fd(buf, offset, len);
+ }
+ // sparse file fills with zero by default.
+ // return sparse_file_add_fill(f2fs_sparse_file, ((__u8*)(bi->buf))[0], byte_len, byte_offset/F2FS_BLKSIZE);
+ return 0;
+}
+
+int dev_read_block(void *buf, __u64 blk_addr)
+{
+ assert(false); // Must not be invoked.
+ return 0;
+}
+
+int dev_read_blocks(void *buf, __u64 addr, __u32 nr_blks)
+{
+ assert(false); // Must not be invoked.
+ return 0;
+}
+
diff --git a/f2fs_utils/f2fs_sparseblock.c b/f2fs_utils/f2fs_sparseblock.c
new file mode 100644
index 0000000..e968ce0
--- /dev/null
+++ b/f2fs_utils/f2fs_sparseblock.c
@@ -0,0 +1,633 @@
+#define _LARGEFILE64_SOURCE
+
+#define LOG_TAG "f2fs_sparseblock"
+
+
+#include <cutils/log.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <f2fs_fs.h>
+#include <linux/types.h>
+#include <malloc.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "f2fs_sparseblock.h"
+
+
+#define D_DISP_u32(ptr, member) \
+ do { \
+ SLOGD("%-30s" "\t\t[0x%#08x : %u]\n", \
+ #member, le32_to_cpu((ptr)->member), le32_to_cpu((ptr)->member) ); \
+ } while (0);
+
+#define D_DISP_u64(ptr, member) \
+ do { \
+ SLOGD("%-30s" "\t\t[0x%#016llx : %llu]\n", \
+ #member, le64_to_cpu((ptr)->member), le64_to_cpu((ptr)->member) ); \
+ } while (0);
+
+#define segno_in_journal(sum, i) ((sum)->sit_j.entries[i].segno)
+
+#define sit_in_journal(sum, i) ((sum)->sit_j.entries[i].se)
+
+static void dbg_print_raw_sb_info(struct f2fs_super_block *sb)
+{
+ SLOGD("\n");
+ SLOGD("+--------------------------------------------------------+\n");
+ SLOGD("| Super block |\n");
+ SLOGD("+--------------------------------------------------------+\n");
+
+ D_DISP_u32(sb, magic);
+ D_DISP_u32(sb, major_ver);
+ D_DISP_u32(sb, minor_ver);
+ D_DISP_u32(sb, log_sectorsize);
+ D_DISP_u32(sb, log_sectors_per_block);
+
+ D_DISP_u32(sb, log_blocksize);
+ D_DISP_u32(sb, log_blocks_per_seg);
+ D_DISP_u32(sb, segs_per_sec);
+ D_DISP_u32(sb, secs_per_zone);
+ D_DISP_u32(sb, checksum_offset);
+ D_DISP_u64(sb, block_count);
+
+ D_DISP_u32(sb, section_count);
+ D_DISP_u32(sb, segment_count);
+ D_DISP_u32(sb, segment_count_ckpt);
+ D_DISP_u32(sb, segment_count_sit);
+ D_DISP_u32(sb, segment_count_nat);
+
+ D_DISP_u32(sb, segment_count_ssa);
+ D_DISP_u32(sb, segment_count_main);
+ D_DISP_u32(sb, segment0_blkaddr);
+
+ D_DISP_u32(sb, cp_blkaddr);
+ D_DISP_u32(sb, sit_blkaddr);
+ D_DISP_u32(sb, nat_blkaddr);
+ D_DISP_u32(sb, ssa_blkaddr);
+ D_DISP_u32(sb, main_blkaddr);
+
+ D_DISP_u32(sb, root_ino);
+ D_DISP_u32(sb, node_ino);
+ D_DISP_u32(sb, meta_ino);
+ D_DISP_u32(sb, cp_payload);
+ SLOGD("\n");
+}
+static void dbg_print_raw_ckpt_struct(struct f2fs_checkpoint *cp)
+{
+ SLOGD("\n");
+ SLOGD("+--------------------------------------------------------+\n");
+ SLOGD("| Checkpoint |\n");
+ SLOGD("+--------------------------------------------------------+\n");
+
+ D_DISP_u64(cp, checkpoint_ver);
+ D_DISP_u64(cp, user_block_count);
+ D_DISP_u64(cp, valid_block_count);
+ D_DISP_u32(cp, rsvd_segment_count);
+ D_DISP_u32(cp, overprov_segment_count);
+ D_DISP_u32(cp, free_segment_count);
+
+ D_DISP_u32(cp, alloc_type[CURSEG_HOT_NODE]);
+ D_DISP_u32(cp, alloc_type[CURSEG_WARM_NODE]);
+ D_DISP_u32(cp, alloc_type[CURSEG_COLD_NODE]);
+ D_DISP_u32(cp, cur_node_segno[0]);
+ D_DISP_u32(cp, cur_node_segno[1]);
+ D_DISP_u32(cp, cur_node_segno[2]);
+
+ D_DISP_u32(cp, cur_node_blkoff[0]);
+ D_DISP_u32(cp, cur_node_blkoff[1]);
+ D_DISP_u32(cp, cur_node_blkoff[2]);
+
+
+ D_DISP_u32(cp, alloc_type[CURSEG_HOT_DATA]);
+ D_DISP_u32(cp, alloc_type[CURSEG_WARM_DATA]);
+ D_DISP_u32(cp, alloc_type[CURSEG_COLD_DATA]);
+ D_DISP_u32(cp, cur_data_segno[0]);
+ D_DISP_u32(cp, cur_data_segno[1]);
+ D_DISP_u32(cp, cur_data_segno[2]);
+
+ D_DISP_u32(cp, cur_data_blkoff[0]);
+ D_DISP_u32(cp, cur_data_blkoff[1]);
+ D_DISP_u32(cp, cur_data_blkoff[2]);
+
+ D_DISP_u32(cp, ckpt_flags);
+ D_DISP_u32(cp, cp_pack_total_block_count);
+ D_DISP_u32(cp, cp_pack_start_sum);
+ D_DISP_u32(cp, valid_node_count);
+ D_DISP_u32(cp, valid_inode_count);
+ D_DISP_u32(cp, next_free_nid);
+ D_DISP_u32(cp, sit_ver_bitmap_bytesize);
+ D_DISP_u32(cp, nat_ver_bitmap_bytesize);
+ D_DISP_u32(cp, checksum_offset);
+ D_DISP_u64(cp, elapsed_time);
+
+ D_DISP_u32(cp, sit_nat_version_bitmap[0]);
+ SLOGD("\n\n");
+}
+
+static void dbg_print_info_struct(struct f2fs_info *info)
+{
+ SLOGD("\n");
+ SLOGD("+--------------------------------------------------------+\n");
+ SLOGD("| F2FS_INFO |\n");
+ SLOGD("+--------------------------------------------------------+\n");
+ SLOGD("blocks_per_segment: %"PRIu64, info->blocks_per_segment);
+ SLOGD("block_size: %d", info->block_size);
+ SLOGD("sit_bmp loc: %p", info->sit_bmp);
+ SLOGD("sit_bmp_size: %d", info->sit_bmp_size);
+ SLOGD("blocks_per_sit: %"PRIu64, info->blocks_per_sit);
+ SLOGD("sit_blocks loc: %p", info->sit_blocks);
+ SLOGD("sit_sums loc: %p", info->sit_sums);
+ SLOGD("sit_sums num: %d", le16_to_cpu(info->sit_sums->n_sits));
+ unsigned int i;
+ for(i = 0; i < (le16_to_cpu(info->sit_sums->n_sits)); i++) {
+ SLOGD("entry %d in journal entries is for segment %d",i, le32_to_cpu(segno_in_journal(info->sit_sums, i)));
+ }
+
+ SLOGD("cp_blkaddr: %"PRIu64, info->cp_blkaddr);
+ SLOGD("cp_valid_cp_blkaddr: %"PRIu64, info->cp_valid_cp_blkaddr);
+ SLOGD("sit_blkaddr: %"PRIu64, info->sit_blkaddr);
+ SLOGD("nat_blkaddr: %"PRIu64, info->nat_blkaddr);
+ SLOGD("ssa_blkaddr: %"PRIu64, info->ssa_blkaddr);
+ SLOGD("main_blkaddr: %"PRIu64, info->main_blkaddr);
+ SLOGD("total_user_used: %"PRIu64, info->total_user_used);
+ SLOGD("total_blocks: %"PRIu64, info->total_blocks);
+ SLOGD("\n\n");
+}
+
+
+/* read blocks */
+static int read_structure(int fd, unsigned long long start, void *buf, ssize_t len)
+{
+ off64_t ret;
+
+ ret = lseek64(fd, start, SEEK_SET);
+ if (ret < 0) {
+ SLOGE("failed to seek\n");
+ return ret;
+ }
+
+ ret = read(fd, buf, len);
+ if (ret < 0) {
+ SLOGE("failed to read\n");
+ return ret;
+ }
+ if (ret != len) {
+ SLOGE("failed to read all\n");
+ return -1;
+ }
+ return 0;
+}
+
+static int read_structure_blk(int fd, unsigned long long start_blk, void *buf, size_t len)
+{
+ return read_structure(fd, F2FS_BLKSIZE*start_blk, buf, F2FS_BLKSIZE * len);
+}
+
+static int read_f2fs_sb(int fd, struct f2fs_super_block *sb)
+{
+ int rc;
+ rc = read_structure(fd, F2FS_SUPER_OFFSET, sb, sizeof(*sb));
+ if (le32_to_cpu(sb->magic) != F2FS_SUPER_MAGIC) {
+ SLOGE("Not a valid F2FS super block. Magic:%#08x != %#08x",
+ le32_to_cpu(sb->magic), F2FS_SUPER_MAGIC);
+ return -1;
+ }
+ return 0;
+}
+
+unsigned int get_f2fs_filesystem_size_sec(char *dev)
+{
+ int fd;
+ if ((fd = open(dev, O_RDONLY)) < 0) {
+ SLOGE("Cannot open device to get filesystem size ");
+ return 0;
+ }
+ struct f2fs_super_block sb;
+ if(read_f2fs_sb(fd, &sb))
+ return 0;
+ return (unsigned int)(le64_to_cpu(sb.block_count)*F2FS_BLKSIZE/DEFAULT_SECTOR_SIZE);
+}
+
+static struct f2fs_checkpoint *validate_checkpoint(block_t cp_addr,
+ unsigned long long *version, int fd)
+{
+ unsigned char *cp_block_1, *cp_block_2;
+ struct f2fs_checkpoint *cp_block, *cp_ret;
+ u64 cp1_version = 0, cp2_version = 0;
+
+ cp_block_1 = malloc(F2FS_BLKSIZE);
+ if (!cp_block_1)
+ return NULL;
+
+ /* Read the 1st cp block in this CP pack */
+ if (read_structure_blk(fd, cp_addr, cp_block_1, 1))
+ goto invalid_cp1;
+
+ /* get the version number */
+ cp_block = (struct f2fs_checkpoint *)cp_block_1;
+
+ cp1_version = le64_to_cpu(cp_block->checkpoint_ver);
+
+ cp_block_2 = malloc(F2FS_BLKSIZE);
+ if (!cp_block_2) {
+ goto invalid_cp1;
+ }
+ /* Read the 2nd cp block in this CP pack */
+ cp_addr += le32_to_cpu(cp_block->cp_pack_total_block_count) - 1;
+ if (read_structure_blk(fd, cp_addr, cp_block_2, 1)) {
+ goto invalid_cp2;
+ }
+
+ cp_block = (struct f2fs_checkpoint *)cp_block_2;
+
+ cp2_version = le64_to_cpu(cp_block->checkpoint_ver);
+
+ if (cp2_version == cp1_version) {
+ *version = cp2_version;
+ free(cp_block_2);
+ return (struct f2fs_checkpoint *)cp_block_1;
+ }
+
+ /* There must be something wrong with this checkpoint */
+invalid_cp2:
+ free(cp_block_2);
+invalid_cp1:
+ free(cp_block_1);
+ return NULL;
+}
+
+int get_valid_checkpoint_info(int fd, struct f2fs_super_block *sb, struct f2fs_checkpoint **cp, struct f2fs_info *info)
+{
+ struct f2fs_checkpoint *cp_block;
+
+ struct f2fs_checkpoint *cp1, *cp2, *cur_cp;
+ int cur_cp_no;
+ unsigned long blk_size;
+ unsigned long long cp1_version = 0, cp2_version = 0;
+ unsigned long long cp1_start_blk_no;
+ unsigned long long cp2_start_blk_no;
+ u32 bmp_size;
+
+ blk_size = 1U << le32_to_cpu(sb->log_blocksize);
+
+ /*
+ * Find valid cp by reading both packs and finding most recent one.
+ */
+ cp1_start_blk_no = le32_to_cpu(sb->cp_blkaddr);
+ cp1 = validate_checkpoint(cp1_start_blk_no, &cp1_version, fd);
+
+ /* The second checkpoint pack should start at the next segment */
+ cp2_start_blk_no = cp1_start_blk_no + (1 << le32_to_cpu(sb->log_blocks_per_seg));
+ cp2 = validate_checkpoint(cp2_start_blk_no, &cp2_version, fd);
+
+ if (cp1 && cp2) {
+ if (ver_after(cp2_version, cp1_version)) {
+ cur_cp = cp2;
+ info->cp_valid_cp_blkaddr = cp2_start_blk_no;
+ free(cp1);
+ } else {
+ cur_cp = cp1;
+ info->cp_valid_cp_blkaddr = cp1_start_blk_no;
+ free(cp2);
+ }
+ } else if (cp1) {
+ cur_cp = cp1;
+ info->cp_valid_cp_blkaddr = cp1_start_blk_no;
+ } else if (cp2) {
+ cur_cp = cp2;
+ info->cp_valid_cp_blkaddr = cp2_start_blk_no;
+ } else {
+ goto fail_no_cp;
+ }
+
+ *cp = cur_cp;
+
+ return 0;
+
+fail_no_cp:
+ SLOGE("Valid Checkpoint not found!!");
+ return -EINVAL;
+}
+
+static int gather_sit_info(int fd, struct f2fs_info *info)
+{
+ u64 num_segments = (info->total_blocks - info->main_blkaddr
+ + info->blocks_per_segment - 1) / info->blocks_per_segment;
+ u64 num_sit_blocks = (num_segments + SIT_ENTRY_PER_BLOCK - 1) / SIT_ENTRY_PER_BLOCK;
+ u64 sit_block;
+
+ info->sit_blocks = malloc(num_sit_blocks * sizeof(struct f2fs_sit_block));
+ if (!info->sit_blocks)
+ return -1;
+
+ for(sit_block = 0; sit_block<num_sit_blocks; sit_block++) {
+ off64_t address = info->sit_blkaddr + sit_block;
+
+ if (f2fs_test_bit(sit_block, info->sit_bmp))
+ address += info->blocks_per_sit;
+
+ SLOGD("Reading cache block starting at block %"PRIu64, address);
+ if (read_structure(fd, address * F2FS_BLKSIZE, &info->sit_blocks[sit_block], sizeof(struct f2fs_sit_block))) {
+ SLOGE("Could not read sit block at block %"PRIu64, address);
+ free(info->sit_blocks);
+ return -1;
+ }
+ }
+ return 0;
+}
+
+static inline int is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
+{
+ unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
+ return !!(ckpt_flags & f);
+}
+
+static inline u64 sum_blk_addr(struct f2fs_checkpoint *cp, struct f2fs_info *info, int base, int type)
+{
+ return info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_total_block_count)
+ - (base + 1) + type;
+}
+
+static int get_sit_summary(int fd, struct f2fs_info *info, struct f2fs_checkpoint *cp)
+{
+ char buffer[F2FS_BLKSIZE];
+
+ info->sit_sums = calloc(1, sizeof(struct f2fs_summary_block));
+ if (!info->sit_sums)
+ return -1;
+
+ /* CURSEG_COLD_DATA where the journaled SIT entries are. */
+ if (is_set_ckpt_flags(cp, CP_COMPACT_SUM_FLAG)) {
+ if (read_structure_blk(fd, info->cp_valid_cp_blkaddr + le32_to_cpu(cp->cp_pack_start_sum), buffer, 1))
+ return -1;
+ memcpy(&info->sit_sums->n_sits, &buffer[SUM_JOURNAL_SIZE], SUM_JOURNAL_SIZE);
+ } else {
+ u64 blk_addr;
+ if (is_set_ckpt_flags(cp, CP_UMOUNT_FLAG))
+ blk_addr = sum_blk_addr(cp, info, NR_CURSEG_TYPE, CURSEG_COLD_DATA);
+ else
+ blk_addr = sum_blk_addr(cp, info, NR_CURSEG_DATA_TYPE, CURSEG_COLD_DATA);
+
+ if (read_structure_blk(fd, blk_addr, buffer, 1))
+ return -1;
+
+ memcpy(info->sit_sums, buffer, sizeof(struct f2fs_summary_block));
+ }
+ return 0;
+}
+
+struct f2fs_info *generate_f2fs_info(int fd)
+{
+ struct f2fs_super_block *sb = NULL;
+ struct f2fs_checkpoint *cp = NULL;
+ struct f2fs_info *info;
+
+ info = calloc(1, sizeof(*info));
+ if (!info) {
+ SLOGE("Out of memory!");
+ return NULL;
+ }
+
+ sb = malloc(sizeof(*sb));
+ if(!sb) {
+ SLOGE("Out of memory!");
+ free(info);
+ return NULL;
+ }
+ if (read_f2fs_sb(fd, sb)) {
+ SLOGE("Failed to read superblock");
+ free(info);
+ free(sb);
+ return NULL;
+ }
+ dbg_print_raw_sb_info(sb);
+
+ info->cp_blkaddr = le32_to_cpu(sb->cp_blkaddr);
+ info->sit_blkaddr = le32_to_cpu(sb->sit_blkaddr);
+ info->nat_blkaddr = le32_to_cpu(sb->nat_blkaddr);
+ info->ssa_blkaddr = le32_to_cpu(sb->ssa_blkaddr);
+ info->main_blkaddr = le32_to_cpu(sb->main_blkaddr);
+ info->block_size = F2FS_BLKSIZE;
+ info->total_blocks = sb->block_count;
+ info->blocks_per_sit = (le32_to_cpu(sb->segment_count_sit) >> 1) << le32_to_cpu(sb->log_blocks_per_seg);
+ info->blocks_per_segment = 1U << le32_to_cpu(sb->log_blocks_per_seg);
+
+ if (get_valid_checkpoint_info(fd, sb, &cp, info))
+ goto error;
+ dbg_print_raw_ckpt_struct(cp);
+
+ info->total_user_used = le32_to_cpu(cp->valid_block_count);
+
+ u32 bmp_size = le32_to_cpu(cp->sit_ver_bitmap_bytesize);
+
+ /* get sit validity bitmap */
+ info->sit_bmp = malloc(bmp_size);
+ if(!info->sit_bmp) {
+ SLOGE("Out of memory!");
+ goto error;
+ }
+
+ info->sit_bmp_size = bmp_size;
+ if (read_structure(fd, info->cp_valid_cp_blkaddr * F2FS_BLKSIZE
+ + offsetof(struct f2fs_checkpoint, sit_nat_version_bitmap),
+ info->sit_bmp, bmp_size)) {
+ SLOGE("Error getting SIT validity bitmap");
+ goto error;
+ }
+
+ if (gather_sit_info(fd , info)) {
+ SLOGE("Error getting SIT information");
+ goto error;
+ }
+ if (get_sit_summary(fd, info, cp)) {
+ SLOGE("Error getting SIT entries in summary area");
+ goto error;
+ }
+ dbg_print_info_struct(info);
+ return info;
+error:
+ free(sb);
+ free(cp);
+ free_f2fs_info(info);
+ return NULL;
+}
+
+void free_f2fs_info(struct f2fs_info *info)
+{
+ if (info) {
+ free(info->sit_blocks);
+ info->sit_blocks = NULL;
+
+ free(info->sit_bmp);
+ info->sit_bmp = NULL;
+
+ free(info->sit_sums);
+ info->sit_sums = NULL;
+ }
+ free(info);
+}
+
+u64 get_num_blocks_used(struct f2fs_info *info)
+{
+ return info->main_blkaddr + info->total_user_used;
+}
+
+int f2fs_test_bit(unsigned int nr, const char *p)
+{
+ int mask;
+ char *addr = (char *)p;
+
+ addr += (nr >> 3);
+ mask = 1 << (7 - (nr & 0x07));
+ return (mask & *addr) != 0;
+}
+
+int run_on_used_blocks(u64 startblock, struct f2fs_info *info, int (*func)(u64 pos, void *data), void *data) {
+ struct f2fs_sit_block sit_block_cache;
+ struct f2fs_sit_entry * sit_entry;
+ u64 sit_block_num_cur = 0, segnum = 0, block_offset;
+ u64 block;
+ unsigned int used, found, started = 0, i;
+
+ block = startblock;
+ while (block < info->total_blocks) {
+ /* TODO: Save only relevant portions of metadata */
+ if (block < info->main_blkaddr) {
+ if (func(block, data)) {
+ SLOGI("func error");
+ return -1;
+ }
+ } else {
+ /* Main Section */
+ segnum = (block - info->main_blkaddr)/info->blocks_per_segment;
+
+ /* check the SIT entries in the journal */
+ found = 0;
+ for(i = 0; i < le16_to_cpu(info->sit_sums->n_sits); i++) {
+ if (le32_to_cpu(segno_in_journal(info->sit_sums, i)) == segnum) {
+ sit_entry = &sit_in_journal(info->sit_sums, i);
+ found = 1;
+ break;
+ }
+ }
+
+ /* get SIT entry from SIT section */
+ if (!found) {
+ sit_block_num_cur = segnum / SIT_ENTRY_PER_BLOCK;
+ sit_entry = &info->sit_blocks[sit_block_num_cur].entries[segnum % SIT_ENTRY_PER_BLOCK];
+ }
+
+ block_offset = (block - info->main_blkaddr) % info->blocks_per_segment;
+
+ if (block_offset == 0 && GET_SIT_VBLOCKS(sit_entry) == 0) {
+ block += info->blocks_per_segment;
+ continue;
+ }
+
+ used = f2fs_test_bit(block_offset, (char *)sit_entry->valid_map);
+ if(used)
+ if (func(block, data))
+ return -1;
+ }
+
+ block++;
+ }
+ return 0;
+}
+
+struct privdata
+{
+ int count;
+ int infd;
+ int outfd;
+ char* buf;
+ char *zbuf;
+ int done;
+ struct f2fs_info *info;
+};
+
+
+/*
+ * This is a simple test program. It performs a block to block copy of a
+ * filesystem, replacing blocks identified as unused with 0's.
+ */
+
+int copy_used(u64 pos, void *data)
+{
+ struct privdata *d = data;
+ char *buf;
+ int pdone = (pos * 100) / d->info->total_blocks;
+ if (pdone > d->done) {
+ d->done = pdone;
+ printf("Done with %d percent\n", d->done);
+ }
+
+ d->count++;
+ buf = d->buf;
+ if(read_structure_blk(d->infd, (unsigned long long)pos, d->buf, 1)) {
+ printf("Error reading!!!\n");
+ return -1;
+ }
+
+ off64_t ret;
+ ret = lseek64(d->outfd, pos * F2FS_BLKSIZE, SEEK_SET);
+ if (ret < 0) {
+ SLOGE("failed to seek\n");
+ return ret;
+ }
+
+ ret = write(d->outfd, d->buf, F2FS_BLKSIZE);
+ if (ret < 0) {
+ SLOGE("failed to write\n");
+ return ret;
+ }
+ if (ret != F2FS_BLKSIZE) {
+ SLOGE("failed to read all\n");
+ return -1;
+ }
+ return 0;
+}
+
+int main(int argc, char **argv)
+{
+ if (argc != 3)
+ printf("Usage: %s fs_file_in fs_file_out\n", argv[0]);
+ char *in = argv[1];
+ char *out = argv[2];
+ int infd, outfd;
+
+ if ((infd = open(in, O_RDONLY)) < 0) {
+ SLOGE("Cannot open device");
+ return 0;
+ }
+ if ((outfd = open(out, O_WRONLY|O_CREAT, S_IRUSR | S_IWUSR)) < 0) {
+ SLOGE("Cannot open output");
+ return 0;
+ }
+
+ struct privdata d;
+ d.infd = infd;
+ d.outfd = outfd;
+ d.count = 0;
+ struct f2fs_info *info = generate_f2fs_info(infd);
+ if (!info) {
+ printf("Failed to generate info!");
+ return -1;
+ }
+ char *buf = malloc(F2FS_BLKSIZE);
+ char *zbuf = calloc(1, F2FS_BLKSIZE);
+ d.buf = buf;
+ d.zbuf = zbuf;
+ d.done = 0;
+ d.info = info;
+ int expected_count = get_num_blocks_used(info);
+ run_on_used_blocks(0, info, ©_used, &d);
+ printf("Copied %d blocks. Expected to copy %d\n", d.count, expected_count);
+ ftruncate64(outfd, info->total_blocks * F2FS_BLKSIZE);
+ free_f2fs_info(info);
+ free(buf);
+ free(zbuf);
+ close(infd);
+ close(outfd);
+ return 0;
+}
diff --git a/f2fs_utils/f2fs_sparseblock.h b/f2fs_utils/f2fs_sparseblock.h
new file mode 100644
index 0000000..0a0dab4
--- /dev/null
+++ b/f2fs_utils/f2fs_sparseblock.h
@@ -0,0 +1,74 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef F2FS_UTILS_F2F2_UTILS_H_
+#define F2FS_UTILS_F2F2_UTILS_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+
+
+
+#define ver_after(a, b) (typecheck(unsigned long long, a) && \
+ typecheck(unsigned long long, b) && \
+ ((long long)((a) - (b)) > 0))
+
+#define ver_equal(a, b) (typecheck(unsigned long long, a) && \
+ typecheck(unsigned long long, b) && \
+ ((long long)((a) - (b)) == 0))
+
+struct f2fs_sit_block;
+struct f2fs_summary_block;
+
+struct f2fs_info {
+ u_int64_t blocks_per_segment;
+ u_int32_t block_size;
+
+ char *sit_bmp;
+ u_int32_t sit_bmp_size;
+ u_int64_t blocks_per_sit;
+ struct f2fs_sit_block *sit_blocks;
+ struct f2fs_summary_block *sit_sums;
+
+ u_int64_t cp_blkaddr;
+ u_int64_t cp_valid_cp_blkaddr;
+
+ u_int64_t sit_blkaddr;
+
+ u_int64_t nat_blkaddr;
+
+ u_int64_t ssa_blkaddr;
+
+ u_int64_t main_blkaddr;
+
+ u_int64_t total_user_used;
+ u_int64_t total_blocks;
+};
+
+u64 get_num_blocks_used(struct f2fs_info *info);
+struct f2fs_info *generate_f2fs_info(int fd);
+void free_f2fs_info(struct f2fs_info *info);
+unsigned int get_f2fs_filesystem_size_sec(char *dev);
+int run_on_used_blocks(u64 startblock, struct f2fs_info *info, int (*func)(u64 pos, void *data), void *data);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // F2FS_UTILS_F2F2_UTILS_H_
diff --git a/f2fs_utils/f2fs_utils.c b/f2fs_utils/f2fs_utils.c
new file mode 100644
index 0000000..6254c08
--- /dev/null
+++ b/f2fs_utils/f2fs_utils.c
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
+ * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#define _LARGEFILE64_SOURCE
+
+#include <fcntl.h>
+#include <string.h>
+
+#include <f2fs_fs.h>
+#include <f2fs_format_utils.h>
+
+#include <sparse/sparse.h>
+
+struct selabel_handle;
+
+#include "make_f2fs.h"
+
+extern void flush_sparse_buffs();
+
+struct f2fs_configuration config;
+struct sparse_file *f2fs_sparse_file;
+extern int dlopenf2fs();
+
+static void reset_f2fs_info() {
+ // Reset all the global data structures used by make_f2fs so it
+ // can be called again.
+ memset(&config, 0, sizeof(config));
+ config.fd = -1;
+ if (f2fs_sparse_file) {
+ sparse_file_destroy(f2fs_sparse_file);
+ f2fs_sparse_file = NULL;
+ }
+}
+
+int make_f2fs_sparse_fd(int fd, long long len,
+ const char *mountpoint, struct selabel_handle *sehnd)
+{
+ if (dlopenf2fs() < 0) {
+ return -1;
+ }
+ reset_f2fs_info();
+ f2fs_init_configuration(&config);
+ len &= ~((__u64)(F2FS_BLKSIZE - 1));
+ config.total_sectors = len / config.sector_size;
+ config.start_sector = 0;
+ f2fs_sparse_file = sparse_file_new(F2FS_BLKSIZE, len);
+ f2fs_format_device();
+ sparse_file_write(f2fs_sparse_file, fd, /*gzip*/0, /*sparse*/1, /*crc*/0);
+ sparse_file_destroy(f2fs_sparse_file);
+ flush_sparse_buffs();
+ f2fs_sparse_file = NULL;
+ return 0;
+}
diff --git a/f2fs_utils/make_f2fs.h b/f2fs_utils/make_f2fs.h
new file mode 100644
index 0000000..f02694a
--- /dev/null
+++ b/f2fs_utils/make_f2fs.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2010 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef _MAKE_F2FS_H_
+#define _MAKE_F2FS_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct selabel_handle;
+
+int make_f2fs_sparse_fd(int fd, long long len,
+ const char *mountpoint, struct selabel_handle *sehnd);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/f2fs_utils/make_f2fs_main.c b/f2fs_utils/make_f2fs_main.c
new file mode 100644
index 0000000..1e0dd87
--- /dev/null
+++ b/f2fs_utils/make_f2fs_main.c
@@ -0,0 +1,91 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <fcntl.h>
+#include <libgen.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#if defined(__linux__)
+#include <linux/fs.h>
+#elif defined(__APPLE__) && defined(__MACH__)
+#include <sys/disk.h>
+#endif
+
+#include "make_f2fs.h"
+
+#ifndef USE_MINGW /* O_BINARY is windows-specific flag */
+#define O_BINARY 0
+#endif
+
+static void usage(char *path)
+{
+ fprintf(stderr, "%s -l <len>\n", basename(path));
+ fprintf(stderr, " <filename>\n");
+}
+
+int main(int argc, char **argv)
+{
+ int opt;
+ const char *filename = NULL;
+ int fd;
+ int exitcode;
+ long long len;
+ while ((opt = getopt(argc, argv, "l:")) != -1) {
+ switch (opt) {
+ case 'l':
+ len = atoll(optarg);
+ break;
+ default: /* '?' */
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+ }
+
+
+ if (optind >= argc) {
+ fprintf(stderr, "Expected filename after options\n");
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ filename = argv[optind++];
+
+ if (optind < argc) {
+ fprintf(stderr, "Unexpected argument: %s\n", argv[optind]);
+ usage(argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ if (strcmp(filename, "-")) {
+ fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+ if (fd < 0) {
+ perror("open");
+ return EXIT_FAILURE;
+ }
+ } else {
+ fd = STDOUT_FILENO;
+ }
+
+ exitcode = make_f2fs_sparse_fd(fd, len, NULL, NULL);
+
+ close(fd);
+ if (exitcode && strcmp(filename, "-"))
+ unlink(filename);
+ return exitcode;
+}
diff --git a/f2fs_utils/mkf2fsuserimg.sh b/f2fs_utils/mkf2fsuserimg.sh
new file mode 100755
index 0000000..93ec743
--- /dev/null
+++ b/f2fs_utils/mkf2fsuserimg.sh
@@ -0,0 +1,34 @@
+#!/bin/bash
+#
+# To call this script, make sure make_f2fs is somewhere in PATH
+
+function usage() {
+cat<<EOT
+Usage:
+${0##*/} OUTPUT_FILE SIZE
+EOT
+}
+
+echo "in mkf2fsuserimg.sh PATH=$PATH"
+
+if [ $# -lt 2 ]; then
+ usage
+ exit 1
+fi
+
+OUTPUT_FILE=$1
+SIZE=$2
+shift; shift
+
+
+if [ -z $SIZE ]; then
+ echo "Need size of filesystem"
+ exit 2
+fi
+
+MAKE_F2FS_CMD="make_f2fs -l $SIZE $OUTPUT_FILE"
+echo $MAKE_F2FS_CMD
+$MAKE_F2FS_CMD
+if [ $? -ne 0 ]; then
+ exit 4
+fi