[OnePlus 3 & 3T Cross Device Development] anupritaisno1:
[MENTION=9843830]2Tweak[/MENTION] I built e4defrag to defragment android devices with ext4

https://glassrom.pw/e4defrag

Use with caution and definitely do not defragment a non rotational storage periodically

diff --git a/misc/Android.bp b/misc/Android.bp
index 8881ebf2..88140799 100644
--- a/misc/Android.bp
+++ b/misc/Android.bp
@@ -280,3 +280,18 @@ cc_binary {
],
system_shared_libs: ["libc", "libdl"],
}
+
+//##########################################################################+// Build e4defrag
+
+cc_binary {
+ name: "e4defrag",
+ defaults: ["e2fsprogs-defaults"],
+
+ srcs: ["e4defrag.c"],
+ shared_libs: [
+ "libext2fs",
+ "libext2_com_err",
+ ],
+ system_shared_libs: ["libc", "libdl"],
+}
diff --git a/misc/e4defrag.c b/misc/e4defrag.c
index 1d1b4f41..d04f52f7 100644
--- a/misc/e4defrag.c
+++ b/misc/e4defrag.c
@@ -185,17 +185,8 @@ static struct frag_statistic_ino frag_rank[SHOW_FRAG_FILES];
* We prefer posix_fadvise64 when available, as it allows 64bit offset on
* 32bit systems
*/
-#if defined(HAVE_POSIX_FADVISE64)
#define posix_fadvise posix_fadvise64
-#elif defined(HAVE_FADVISE64)
-#define posix_fadvise fadvise64
-#elif !defined(HAVE_POSIX_FADVISE)
-#error posix_fadvise not available!
-#endif

-#ifndef HAVE_FALLOCATE64
-#error fallocate64 not available!
-#endif /* ! HAVE_FALLOCATE64 */

/*
* get_mount_point() - Get device's mount point.
@@ -424,12 +415,10 @@ static int defrag_fadvise(int fd, struct move_extent defrag_data,
offset = (loff_t)defrag_data.orig_start * block_size;
offset = (offset / pagesize) * pagesize;

-#ifdef HAVE_SYNC_FILE_RANGE
/* Sync file for fadvise process */
if (sync_file_range(fd, offset,
(loff_t)pagesize * page_num, sync_flag) < 0)
return -1;
-#endif

/* Try to release buffer cache which this process used,
* then other process can use the released buffer

To build it yourself use this patch

Why did I remove the ifdefs? The build system uses bionic to provide these functions but their existence is unknown to the compiler. Thankfully these calls are implemented in asm so we are simply moving data into the correct registers, clearing the return register and calling the function. After making sure bionic provided these functions I removed the ifdefs

How did I find the shared libs?

I looked at a few other examples in the script and then ran ldd

$ ldd $(which chattr)

linux-vdso.so.1 (0x000066310ca37000)
libe2p.so.2 => /usr/lib/libe2p.so.2 (0x000066310ca08000)
libcom_err.so.2 => /usr/lib/libcom_err.so.2 (0x000066310ca02000)
libc.so.6 => /usr/lib/libc.so.6 (0x000066310c83f000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x000066310c81e000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x000066310ca38000)
suzumiya@parabola ~/mount/external/e2fsprogs (58d48a20*) $ ldd $(which e4defrag)

linux-vdso.so.1 (0x00006c298ce5c000)
libext2fs.so.2 => /usr/lib/libext2fs.so.2 (0x00006c298cdc8000)
libcom_err.so.2 => /usr/lib/libcom_err.so.2 (0x00006c298cdc2000)
libc.so.6 => /usr/lib/libc.so.6 (0x00006c298cbff000)
libpthread.so.0 => /usr/lib/libpthread.so.0 (0x00006c298cbde000)
/lib64/ld-linux-x86-64.so.2 => /usr/lib64/ld-linux-x86-64.so.2 (0x00006c298ce5d000)