From 73ab9e03861c22ec6723b7b110d62ba60f37c164 Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Wed, 8 Nov 2023 17:11:44 -0500 Subject: [PATCH] bcachefs: Factor out darray resize slowpath Move the slowpath (actually growing the darray) to an out-of-line function; also, add some helpers for the upcoming btree write buffer rewrite. Signed-off-by: Kent Overstreet --- fs/bcachefs/Makefile | 1 + fs/bcachefs/darray.c | 21 +++++++++++++++++++++ fs/bcachefs/darray.h | 32 +++++++++++++++++++------------- 3 files changed, 41 insertions(+), 13 deletions(-) create mode 100644 fs/bcachefs/darray.c diff --git a/fs/bcachefs/Makefile b/fs/bcachefs/Makefile index 45b64f89258c..ed550a40463e 100644 --- a/fs/bcachefs/Makefile +++ b/fs/bcachefs/Makefile @@ -28,6 +28,7 @@ bcachefs-y := \ clock.o \ compress.o \ counters.o \ + darray.o \ debug.o \ dirent.o \ disk_groups.o \ diff --git a/fs/bcachefs/darray.c b/fs/bcachefs/darray.c new file mode 100644 index 000000000000..aae07be1d911 --- /dev/null +++ b/fs/bcachefs/darray.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include +#include +#include "darray.h" + +int __bch2_darray_resize(darray_void *d, size_t element_size, size_t new_size, gfp_t gfp) +{ + if (new_size > d->size) { + new_size = roundup_pow_of_two(new_size); + + void *data = krealloc_array(d->data, new_size, element_size, gfp); + if (!data) + return -ENOMEM; + + d->data = data; + d->size = new_size; + } + + return 0; +} diff --git a/fs/bcachefs/darray.h b/fs/bcachefs/darray.h index 87b4b2d1ec76..43ea21ad9ea3 100644 --- a/fs/bcachefs/darray.h +++ b/fs/bcachefs/darray.h @@ -8,7 +8,6 @@ * Inspired by CCAN's darray */ -#include "util.h" #include #define DARRAY(type) \ @@ -19,20 +18,25 @@ struct { \ typedef DARRAY(void) darray_void; +int __bch2_darray_resize(darray_void *, size_t, size_t, gfp_t); + +static inline int __darray_resize(darray_void *d, size_t element_size, + size_t new_size, gfp_t gfp) +{ + return unlikely(new_size > d->size) + ? __bch2_darray_resize(d, element_size, new_size, gfp) + : 0; +} + +#define darray_resize_gfp(_d, _new_size, _gfp) \ + __darray_resize((darray_void *) (_d), sizeof((_d)->data[0]), (_new_size), _gfp) + +#define darray_resize(_d, _new_size) \ + darray_resize_gfp(_d, _new_size, GFP_KERNEL) + static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more, gfp_t gfp) { - if (d->nr + more > d->size) { - size_t new_size = roundup_pow_of_two(d->nr + more); - void *data = krealloc_array(d->data, new_size, t_size, gfp); - - if (!data) - return -ENOMEM; - - d->data = data; - d->size = new_size; - } - - return 0; + return __darray_resize(d, t_size, d->nr + more, gfp); } #define darray_make_room_gfp(_d, _more, _gfp) \ @@ -41,6 +45,8 @@ static inline int __darray_make_room(darray_void *d, size_t t_size, size_t more, #define darray_make_room(_d, _more) \ darray_make_room_gfp(_d, _more, GFP_KERNEL) +#define darray_room(_d) ((_d).size - (_d).nr) + #define darray_top(_d) ((_d).data[(_d).nr]) #define darray_push_gfp(_d, _item, _gfp) \