Skip to content

Added compiling target Android support #982

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
4676c5a
btrfs-progs: mkfs: add --inode-flags option
adam900710 May 21, 2025
e3823c6
btrfs-progs: tests: new test case for mkfs.btrfs --inode-flags
adam900710 May 21, 2025
deb9200
btrfs-progs: zoned: create a data block-group for relocation
morbidrsa May 30, 2025
45a237b
btrfs-progs: print csum values on superblock mismatch
maharmstone May 14, 2025
c534971
btrfs-progs: docs: fix kernel version to set defrag compress level
ozraru Apr 29, 2025
24b64c1
btrfs-progs: convert: add feature dependency checks for bgt
adam900710 May 24, 2025
62fd046
btrfs-progs: convert: replace the bytenrs check with a UASSERT()
adam900710 May 24, 2025
e27f4a5
btrfs-progs: convert: simplify insert_temp_root_item()
adam900710 May 24, 2025
9657aff
btrfs-progs: convert: simplify insert_temp_dev_item() and insert_temp…
adam900710 May 24, 2025
3a8d004
btrfs-progs: convert: simplify insert_temp_dev_extent()
adam900710 May 24, 2025
fb030a7
btrfs-progs: convert: simplify insert_temp_extent_item() and insert_t…
adam900710 May 24, 2025
a26657d
btrfs-progs: convert: merge setup_temp_fs_tree() and setup_temp_csum_…
adam900710 May 24, 2025
15f0b6a
btrfs-progs: convert: implement the block group tree support properly
adam900710 May 24, 2025
9a29f0c
btrfs-progs: tests: tests: add a test case for convert with bgt feature
adam900710 May 24, 2025
932205b
btrfs-progs: docs: add 6.15 kernel development statistics
kdave Mar 20, 2025
96d5e91
btrfs-progs: docs: update 6.15 contribution graphs
kdave Mar 20, 2025
eda8ec8
btrfs-progs: Add `pthread_cancel` implementation for bionic
shadichy Apr 24, 2025
0ffc07e
btrfs-progs: rescue-chunk-recover: Added `atomic` patch
shadichy Apr 24, 2025
6f9271f
btrfs-progs: list-chunks: Workarounds for replacing `qsort_r` when ta…
shadichy Apr 24, 2025
a0de648
btrfs-progs: pthread: Removed pthread.sym as it is not used anywhere
shadichy Jun 11, 2025
50c75df
btrfs-progs: pthread: Moved to android-compat
shadichy Jun 11, 2025
7416516
btrfs-progs: android-compat: Added `qsort_r` implementation
shadichy Jun 11, 2025
f33f4ba
btrfs-progs: android-compat: rename pthread library name and qsort he…
shadichy Jun 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Documentation/Contributors.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ Statistics for 6.x series
"6.12", "20", "111881", "163548", "148", "+1868 -1804"
"6.13", "25", "112756", "164722", "156", "+2780 -1579"
"6.14", "20", "114592", "167178", "147", "+3847 -1391"
"6.15", "25", "114616", "167485", "190", "+2346 -2039"


Legend:
Expand Down
2 changes: 1 addition & 1 deletion Documentation/btrfs-filesystem.rst
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ defragment [options] <file>|<dir> [<file>|<dir>...]
compression. See also section *EXAMPLES*.

-L|--level <level>
Since kernel 6.14 the compresison can also take the level parameter which will be used
Since kernel 6.15 the compresison can also take the level parameter which will be used
only for the defragmentation and overrides the eventual mount option compression level.
Valid levels depend on the compression algorithms: *zlib*
1..9, *lzo* does not have any levels, *zstd* the standard levels 1..15 and also the
Expand Down
35 changes: 35 additions & 0 deletions Documentation/mkfs.btrfs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,41 @@ OPTIONS
:file:`hardlink1` and :file:`hardlink2` because :file:`hardlink3` will
be inside a new subvolume.

--inode-flags <flags>:<path>
Specify that *path* to have inode *flags*, other than the default one (which
implies data COW and data checksum). The option *--rootdir* must also be
specified. This option can be specified multiple times.

The supported flag(s) are:

* *nodatacow*: disable data COW, implies *nodatasum* for regular files.
* *nodatasum*: disable data checksum only.

*flags* can be separated by comma (*,*).

Children inodes will inherit the flags from their parent inodes, like the
following case:

.. code-block:: none

rootdir/
|- file1
|- file2
|- dir/
|- file3

In that case, if *--inode-flags nodatacow:dir* is specified, both
:file:`dir` and :file:`file3` will have the *nodatacow* flag.

And this option also works with *--subvol* option, but the inode flag of
each subvolume is independent and will not inherit from the parent directory.
(The same as the kernel behavior.)

.. note::
Both *--inode-flags* and *--subvol* options are memory hungry,
will consume at least 8KiB for each option. Please keep the
usage of both options to minimum.

--shrink
Shrink the filesystem to its minimal size, only works with *--rootdir* option.

Expand Down
291 changes: 146 additions & 145 deletions Documentation/plot-contribs.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
314 changes: 157 additions & 157 deletions Documentation/plot-patches.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
564 changes: 283 additions & 281 deletions Documentation/plot-sloc-lines.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ objects = \
kernel-shared/uuid-tree.o \
kernel-shared/volumes.o \
kernel-shared/zoned.o \
android-compat/pthread.o \
android-compat/qsort.o \
common/array.o \
common/cpu-utils.o \
common/device-scan.o \
Expand Down Expand Up @@ -292,8 +294,8 @@ tune_objects = tune/main.o tune/seeding.o tune/change-uuid.o tune/change-metadat
all_objects = $(objects) $(cmds_objects) $(libbtrfs_objects) $(convert_objects) \
$(mkfs_objects) $(image_objects) $(tune_objects) $(libbtrfsutil_objects)

tags_files = $(addsuffix /*.[ch], . check cmds common convert crypto image include mkfs tune \
kernel-lib kernel-shared kernel-shared/uapi \
tags_files = $(addsuffix /*.[ch], . android-compat check cmds common convert crypto \
image include mkfs tune kernel-lib kernel-shared kernel-shared/uapi \
libbtrfs libbtrfsutil libbtrfsutil/python tests)

udev_rules = 64-btrfs-dm.rules 64-btrfs-zoned.rules
Expand Down Expand Up @@ -917,6 +919,7 @@ clean: $(CLEANDIRS)
libbtrfs.a libbtrfsutil.a $(libs_shared) $(lib_links)
@echo "Cleaning object files and dependencies"
$(Q)$(RM) -f -- *.o .deps/*.o.d \
android-compat/*.o android-compat/.deps/*.o.d \
check/*.o check/.deps/*.o.d \
cmds/*.o cmds/.deps/*.o.d \
common/*.o common/.deps/*.o.d \
Expand Down
57 changes: 57 additions & 0 deletions android-compat/pthread.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/

#include "include/config.h"

#ifdef __ANDROID__

/* Workaround for `pthread_cancel()` in Android, using `pthread_kill()` instead,
* as Android NDK does not support `pthread_cancel()`.
*/

#include <string.h>
#include <signal.h>
#include "android-compat/pthread.h"

int pthread_setcanceltype(int type, int *oldtype) { return 0; }
int pthread_setcancelstate(int state, int *oldstate) { return 0; }
int pthread_cancel(pthread_t thread_id) {
int status;
if ((status = btrfs_set_thread_exit_handler()) == 0) {
status = pthread_kill(thread_id, SIGUSR1);
}
return status;
}

void btrfs_thread_exit_handler(int sig) {
pthread_exit(0);
}

int btrfs_set_thread_exit_handler() {
int rc;
struct sigaction actions;

memset(&actions, 0, sizeof(actions));
sigemptyset(&actions.sa_mask);
actions.sa_flags = 0;
actions.sa_handler = btrfs_thread_exit_handler;

rc = sigaction(SIGUSR1, &actions, NULL);
return rc;
}

#endif
45 changes: 45 additions & 0 deletions android-compat/pthread.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/

#ifndef __ANDROID_COMPAT_PTHREAD_H__
#define __ANDROID_COMPAT_PTHREAD_H__

#include "include/config.h"

#include <pthread.h>

#ifdef __ANDROID__

/* Adding missing `pthread` related definitions in Android.
*/

#define PTHREAD_CANCELED ((void *) -1)

#define PTHREAD_CANCEL_DEFERRED 0
#define PTHREAD_CANCEL_ASYNCHRONOUS 0
#define PTHREAD_CANCEL_ENABLE 0
#define PTHREAD_CANCEL_DISABLE 0

int pthread_setcanceltype(int type, int *oldtype);
int pthread_setcancelstate(int state, int *oldstate);
int pthread_cancel(pthread_t thread_id);

int btrfs_set_thread_exit_handler();
void btrfs_thread_exit_handler(int sig);

#endif

#endif
50 changes: 50 additions & 0 deletions android-compat/qsort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/

#include "android-compat/qsort.h"
#include <stdlib.h>

struct qsort_r_context {
int (*compar)(const void *, const void *, void *);
void *arg;
};

#if __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
static _Thread_local struct qsort_r_context *qsort_r_ctx = NULL;
#else
static __thread struct qsort_r_context *qsort_r_ctx = NULL;
#endif

static int qsort_r_stub_compare(const void *a, const void *b)
{
return qsort_r_ctx->compar(a, b, qsort_r_ctx->arg);
}

void qsort_r(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *, void *), void *arg)
{
if (nel == 0) return;

struct qsort_r_context ctx;
ctx.compar = compar;
ctx.arg = arg;
struct qsort_r_context *old_ctx = qsort_r_ctx;
qsort_r_ctx = &ctx;

Check warning

Code scanning / CodeQL

Local variable address stored in non-local memory Warning

A stack address (
source
) may be assigned to a non-local variable.
qsort(base, nel, width, qsort_r_stub_compare);

// Restore the old context after qsort is finished.
qsort_r_ctx = old_ctx;
}
50 changes: 50 additions & 0 deletions android-compat/qsort.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License v2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 021110-1307, USA.
*/

#ifndef __ANDROID_COMPAT_QSORT_H__
#define __ANDROID_COMPAT_QSORT_H__

#include <sys/types.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
* @brief A compatible implementation of the GNU C Library (Glibc) qsort_r.
*
* Sorts an array using the quicksort algorithm. This function is thread-safe
* by allowing a custom context pointer (arg) to be passed to the comparison
* function.
*
* @param base A pointer to the first element of the array to be sorted.
* @param nel The number of elements in the array.
* @param width The size in bytes of each element in the array.
* @param compar The comparison function, which takes two elements and a context
* pointer. The function must return:
* - A negative integer if the first element is less than the second.
* - Zero if the elements are equal.
* - A positive integer if the first element is greater than the second.
* @param arg The custom pointer passed to the comparison function.
*/
void qsort_r(void *base, size_t nel, size_t width,
int (*compar)(const void *, const void *, void *), void *arg);

#ifdef __cplusplus
}
#endif

#endif
4 changes: 3 additions & 1 deletion cmds/inspect.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* Boston, MA 021110-1307, USA.
*/

#include "android-compat/qsort.h"
#include "kerncompat.h"
#include <sys/ioctl.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -878,9 +879,10 @@ static int print_list_chunks(struct list_chunks_ctx *ctx, const char *sortmode,
}

/* Skip additional sort if nothing defined by user. */
if (comp.count > 0)
if (comp.count > 0) {
qsort_r(ctx->stats, ctx->length, sizeof(ctx->stats[0]),
(sort_r_cmp_t)compare_cmp_multi, &comp);
}

col_count = 9;
/* Two rows for header and separator. */
Expand Down
27 changes: 25 additions & 2 deletions cmds/rescue-chunk-recover.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include "android-compat/pthread.h"
#include <errno.h>
#include <stddef.h>
#include <string.h>
#ifdef __ANDROID__
#include <stdatomic.h>
#endif
#include "kernel-lib/list.h"
#include "kernel-shared/accessors.h"
#include "kernel-shared/extent-io-tree.h"
Expand Down Expand Up @@ -82,6 +85,9 @@ struct device_scan {
struct btrfs_device *dev;
int fd;
u64 bytenr;
#ifdef __ANDROID__
atomic_flag thread_running;
#endif
};

static struct extent_record *btrfs_new_extent_record(struct extent_buffer *eb)
Expand Down Expand Up @@ -761,8 +767,12 @@ static int scan_one_device(void *dev_scan_struct)
return 1;

buf = malloc(sizeof(*buf) + rc->nodesize);
if (!buf)
if (!buf) {
#ifdef __ANDROID__
atomic_flag_clear(&dev_scan->thread_running);
#endif
return -ENOMEM;
}
buf->len = rc->nodesize;

bytenr = 0;
Expand Down Expand Up @@ -823,6 +833,9 @@ static int scan_one_device(void *dev_scan_struct)
out:
close(fd);
free(buf);
#ifdef __ANDROID__
atomic_flag_clear(&dev_scan->thread_running);
#endif
return ret;
}

Expand Down Expand Up @@ -869,6 +882,9 @@ static int scan_devices(struct recover_control *rc)
dev_scans[devidx].dev = dev;
dev_scans[devidx].fd = fd;
dev_scans[devidx].bytenr = -1;
#ifdef __ANDROID__
atomic_flag_test_and_set(&dev_scans[devidx].thread_running);
#endif
devidx++;
}

Expand All @@ -887,8 +903,15 @@ static int scan_devices(struct recover_control *rc)
for (i = 0; i < devidx; i++) {
if (dev_scans[i].bytenr == -1)
continue;
#ifdef __ANDROID__
if (atomic_flag_test_and_set(&dev_scans[i].thread_running))
ret = EBUSY;
else
ret = pthread_join(t_scans[i], (void **)&t_rets[i]);
#else
ret = pthread_tryjoin_np(t_scans[i],
(void **)&t_rets[i]);
#endif
if (ret == EBUSY) {
all_done = false;
continue;
Expand Down
2 changes: 1 addition & 1 deletion cmds/scrub.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
#include <poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <pthread.h>
#include "android-compat/pthread.h"
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
Expand Down
Loading