>From 66bae04ded4feb8f218f0a6d30f360792430be7c Mon Sep 17 00:00:00 2001
From: "kenneth.kang" <kenneth.kang@lge.com>
Date: Wed, 5 Aug 2015 18:08:25 +0900
Subject: [PATCH] btrfs-progs: Add Android build mk file

Add Android.mk to use btrfs on android device.

To using this funtion, there are some limitation.
1) phread_cancel was changed to pthread_kill due to android didn't support
that.
2) This module needs lzo library on android/external folder.
3) blkid library doesn't support fully. So have to use -f option to
work.
---
 Android.mk      | 111 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 chunk-recover.c |   8 ++++
 cmds-receive.c  |   4 ++
 cmds-restore.c  |   1 +
 cmds-scrub.c    |  22 ++++++++++-
 kerncompat.h    |   3 ++
 mkfs.c          |   9 +++++
 utils.c         |   9 +++--
 8 files changed, 162 insertions(+), 5 deletions(-)
 create mode 100644 Android.mk

diff --git a/Android.mk b/Android.mk
new file mode 100644
index 0000000..f6f3e28
--- /dev/null
+++ b/Android.mk
@@ -0,0 +1,111 @@
+LOCAL_PATH:= $(call my-dir)
+
+#include $(call all-subdir-makefiles)
+
+CFLAGS := -g -O1 -Wall -D_FORTIFY_SOURCE=2 -include config.h -DBTRFS_FLAT_INCLUDES -D_XOPEN_SOURCE=700 -fno-strict-aliasing -fPIC
+
+LDFLAGS := -static -rdynamic
+
+LIBS := -luuid   -lblkid   -lz   -llzo2 -L. -lpthread
+LIBBTRFS_LIBS := $(LIBS)
+
+STATIC_CFLAGS := $(CFLAGS) -ffunction-sections -fdata-sections
+STATIC_LDFLAGS := -static -Wl,--gc-sections
+STATIC_LIBS := -luuid   -lblkid -luuid \
+              -lz   -llzo2 -L. -pthread
+
+btrfs_shared_libraries := libext2_uuid \
+			libext2_blkid
+
+objects := ctree.c disk-io.c radix-tree.c extent-tree.c print-tree.c \
+          root-tree.c dir-item.c file-item.c inode-item.c inode-map.c \
+          extent-cache.c extent_io.c volumes.c utils.c repair.c \
+          qgroup.c raid6.c free-space-cache.c list_sort.c props.c \
+          ulist.c qgroup-verify.c backref.c string-table.c task-utils.c \
+          inode.c file.c find-root.c
+cmds_objects := cmds-subvolume.c cmds-filesystem.c cmds-device.c cmds-scrub.c \
+               cmds-inspect.c cmds-balance.c cmds-send.c cmds-receive.c \
+               cmds-quota.c cmds-qgroup.c cmds-replace.c cmds-check.c \
+               cmds-restore.c cmds-rescue.c chunk-recover.c super-recover.c \
+               cmds-property.c cmds-fi-usage.c
+libbtrfs_objects := send-stream.c send-utils.c rbtree.c btrfs-list.c crc32c.c \
+                   uuid-tree.c utils-lib.c rbtree-utils.c
+libbtrfs_headers := send-stream.h send-utils.h send.h rbtree.h btrfs-list.h \
+                   crc32c.h list.h kerncompat.h radix-tree.h extent-cache.h \
+                   extent_io.h ioctl.h ctree.h btrfsck.h version.h
+TESTS := fsck-tests.sh convert-tests.sh
+blkid_objects := partition/ superblocks/ topology/
+
+
+# external/e2fsprogs/lib is needed for uuid/uuid.h
+common_C_INCLUDES := $(LOCAL_PATH) external/e2fsprogs/lib/ external/lzo/include/ external/zlib/
+
+#----------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_SRC_FILES := $(libbtrfs_objects)
+LOCAL_CFLAGS := $(STATIC_CFLAGS)
+LOCAL_MODULE := libbtrfs
+LOCAL_C_INCLUDES := $(common_C_INCLUDES)
+include $(BUILD_STATIC_LIBRARY)
+
+#----------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := btrfs
+#LOCAL_FORCE_STATIC_EXECUTABLE := true
+LOCAL_SRC_FILES := \
+		$(objects) \
+		$(cmds_objects) \
+		btrfs.c \
+		help.c \
+
+LOCAL_C_INCLUDES := $(common_C_INCLUDES)
+LOCAL_CFLAGS := $(STATIC_CFLAGS)
+#LOCAL_LDLIBS := $(LIBBTRFS_LIBS)
+#LOCAL_LDFLAGS := $(STATIC_LDFLAGS)
+LOCAL_SHARED_LIBRARIES := $(btrfs_shared_libraries)
+LOCAL_STATIC_LIBRARIES := libbtrfs liblzo-static libz
+LOCAL_SYSTEM_SHARED_LIBRARIES := libc libcutils
+
+LOCAL_EXPORT_C_INCLUDES := $(common_C_INCLUDES)
+#LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
+
+#----------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := mkfs.btrfs
+LOCAL_SRC_FILES := \
+                $(objects) \
+                mkfs.c
+
+LOCAL_C_INCLUDES := $(common_C_INCLUDES)
+LOCAL_CFLAGS := $(STATIC_CFLAGS)
+#LOCAL_LDLIBS := $(LIBBTRFS_LIBS)
+#LOCAL_LDFLAGS := $(STATIC_LDFLAGS)
+LOCAL_SHARED_LIBRARIES := $(btrfs_shared_libraries)
+LOCAL_STATIC_LIBRARIES := libbtrfs liblzo-static
+LOCAL_SYSTEM_SHARED_LIBRARIES := libc libcutils
+
+LOCAL_EXPORT_C_INCLUDES := $(common_C_INCLUDES)
+#LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
+
+#---------------------------------------------------------------
+include $(CLEAR_VARS)
+LOCAL_MODULE := btrfstune
+LOCAL_SRC_FILES := \
+                $(objects) \
+                btrfstune.c
+
+LOCAL_C_INCLUDES := $(common_C_INCLUDES)
+LOCAL_CFLAGS := $(STATIC_CFLAGS)
+LOCAL_SHARED_LIBRARIES := $(btrfs_shared_libraries)
+#LOCAL_LDLIBS := $(LIBBTRFS_LIBS)
+#LOCAL_LDFLAGS := $(STATIC_LDFLAGS)
+LOCAL_SHARED_LIBRARIES := $(btrfs_shared_libraries)
+LOCAL_STATIC_LIBRARIES := libbtrfs liblzo-static
+LOCAL_SYSTEM_SHARED_LIBRARIES := libc libcutils
+
+LOCAL_EXPORT_C_INCLUDES := $(common_C_INCLUDES)
+LOCAL_MODULE_TAGS := optional
+include $(BUILD_EXECUTABLE)
+#--------------------------------------------------------------
diff --git a/chunk-recover.c b/chunk-recover.c
index 832b3b1..7f793af 100644
--- a/chunk-recover.c
+++ b/chunk-recover.c
@@ -17,7 +17,9 @@
  */
 
 #include <stdio.h>
+#ifndef ANDROID
 #include <stdio_ext.h>
+#endif
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -755,9 +757,11 @@ static int scan_one_device(void *dev_scan_struct)
 	int fd = dev_scan->fd;
 	int oldtype;
 
+#ifndef ANDROID
 	ret = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
 	if (ret)
 		return 1;
+#endif
 
 	buf = malloc(sizeof(*buf) + rc->leafsize);
 	if (!buf)
@@ -884,7 +888,11 @@ static int scan_devices(struct recover_control *rc)
 	}
 out1:
 	while (ret && (cancel_from <= cancel_to)) {
+#ifdef ANDROID
+		pthread_kill(t_scans[cancel_from], SIGUSR1);
+#else
 		pthread_cancel(t_scans[cancel_from]);
+#endif
 		cancel_from++;
 	}
 out2:
diff --git a/cmds-receive.c b/cmds-receive.c
index 071bea9..3e9d912 100644
--- a/cmds-receive.c
+++ b/cmds-receive.c
@@ -25,7 +25,11 @@
 #include <pthread.h>
 #include <math.h>
 #include <ftw.h>
+#ifndef ANDROID
 #include <wait.h>
+#else
+#include <sys/wait.h>
+#endif
 #include <assert.h>
 #include <getopt.h>
 
diff --git a/cmds-restore.c b/cmds-restore.c
index 8fc8b2a..9a53058 100644
--- a/cmds-restore.c
+++ b/cmds-restore.c
@@ -86,6 +86,7 @@ static int decompress_zlib(char *inbuf, char *outbuf, u64 compress_len,
 	(void)inflateEnd(&strm);
 	return 0;
 }
+
 static inline size_t read_compress_length(unsigned char *buf)
 {
 	__le32 dlen;
diff --git a/cmds-scrub.c b/cmds-scrub.c
index b7aa809..2b03f71 100644
--- a/cmds-scrub.c
+++ b/cmds-scrub.c
@@ -780,12 +780,13 @@ static int scrub_write_progress(pthread_mutex_t *m, const char *fsid,
 	int fd = -1;
 	int old;
 
+#ifndef ANDROID
 	ret = pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &old);
 	if (ret) {
 		err = -ret;
 		goto out3;
 	}
-
+#endif
 	ret = pthread_mutex_lock(m);
 	if (ret) {
 		err = -ret;
@@ -816,10 +817,11 @@ out1:
 		err = -ret;
 
 out2:
+#ifndef ANDROID
 	ret = pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, &old);
 	if (ret && !err)
 		err = -ret;
-
+#endif
 out3:
 	return err;
 }
@@ -898,9 +900,11 @@ static void *scrub_progress_cycle(void *ctx)
 	struct sockaddr_un peer;
 	socklen_t peer_size = sizeof(peer);
 
+#ifndef ANDROID
 	perr = pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &old);
 	if (perr)
 		goto out;
+#endif
 
 	uuid_unparse(spc->fi->fsid, fsid);
 
@@ -952,10 +956,12 @@ static void *scrub_progress_cycle(void *ctx)
 			 * result we got for the current write and go
 			 * on. flag should be set on next cycle, then.
 			 */
+#ifndef ANDROID
 			perr = pthread_setcancelstate(
 					PTHREAD_CANCEL_DISABLE, &old);
 			if (perr)
 				goto out;
+#endif
 			perr = pthread_mutex_lock(&sp_shared->progress_mutex);
 			if (perr)
 				goto out;
@@ -964,20 +970,24 @@ static void *scrub_progress_cycle(void *ctx)
 						&sp_shared->progress_mutex);
 				if (perr)
 					goto out;
+#ifndef ANDROID
 				perr = pthread_setcancelstate(
 						PTHREAD_CANCEL_ENABLE, &old);
 				if (perr)
 					goto out;
+#endif
 				memcpy(sp, sp_last, sizeof(*sp));
 				continue;
 			}
 			perr = pthread_mutex_unlock(&sp_shared->progress_mutex);
 			if (perr)
 				goto out;
+#ifndef ANDROID
 			perr = pthread_setcancelstate(
 					PTHREAD_CANCEL_ENABLE, &old);
 			if (perr)
 				goto out;
+#endif
 			memcpy(sp, sp_shared, sizeof(*sp));
 			memcpy(sp_last, sp_shared, sizeof(*sp));
 		}
@@ -1506,7 +1516,11 @@ static int scrub_start(int argc, char **argv, int resume)
 		}
 	}
 
+#ifdef ANDROID
+	ret = pthread_kill(t_prog, SIGUSR1);
+#else
 	ret = pthread_cancel(t_prog);
+#endif
 	if (!ret)
 		ret = pthread_join(t_prog, &terr);
 
@@ -1517,7 +1531,11 @@ static int scrub_start(int argc, char **argv, int resume)
 	}
 
 	/* check for errors returned from the progress thread itself */
+#ifdef ANDROID
+	if (do_print && terr) {
+#else
 	if (do_print && terr && terr != PTHREAD_CANCELED) {
+#endif
 		fprintf(stderr, "ERROR: recording progress "
 			"failed: %s\n", strerror(-PTR_ERR(terr)));
 	}
diff --git a/kerncompat.h b/kerncompat.h
index 5d92856..ae5b724 100644
--- a/kerncompat.h
+++ b/kerncompat.h
@@ -134,6 +134,9 @@ typedef long long s64;
 typedef int s32;
 #endif
 
+#ifdef ANDROID
+typedef struct blkid_struct_probe *blkid_probe;
+#endif
 
 struct vma_shared { int prio_tree_node; };
 struct vm_area_struct {
diff --git a/mkfs.c b/mkfs.c
index dafd500..face183 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -25,7 +25,12 @@
 #include <stdlib.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#ifndef ANDROID
 #include <sys/dir.h>
+#else
+#include <dirent.h>
+#define direct dirent
+#endif
 #include <fcntl.h>
 #include <unistd.h>
 #include <getopt.h>
@@ -1359,7 +1364,11 @@ int main(int ac, char **av)
 	dev_cnt = ac - optind;
 
 	file = av[optind++];
+#ifndef ANDROID
 	ssd = is_ssd(file);
+#else
+	ssd = true;
+#endif
 
 	if (is_vol_small(file) || mixed) {
 		if (verbose)
diff --git a/utils.c b/utils.c
index 39b295a..24deb65 100644
--- a/utils.c
+++ b/utils.c
@@ -878,9 +878,9 @@ int btrfs_prepare_device(int fd, char *file, int zero_end, u64 *block_count_ret,
 			file, strerror(-ret));
 		return 1;
 	}
-
+#ifndef ANDROID
 	btrfs_wipe_existing_sb(fd);
-
+#endif
 	*block_count_ret = block_count;
 	return 0;
 }
@@ -2267,7 +2267,6 @@ check_overwrite(
 		return 0;
 
 	ret = -1; /* will reset on success of all setup calls */
-
 	pr = blkid_new_probe_from_filename(device);
 	if (!pr)
 		goto out;
@@ -2436,7 +2435,11 @@ int test_dev_for_mkfs(char *file, int force_overwrite)
 		return 1;
 	}
 	if (!force_overwrite) {
+#ifndef ANDROID
 		if (check_overwrite(file)) {
+#else
+		{
+#endif
 			fprintf(stderr, "Use the -f option to force overwrite.\n");
 			return 1;
 		}
-- 
1.8.2

