[PATCH 1/6] libxfs: provide a kernel-compatible kasprintf

Darrick J. Wong
2024-06-03T19:24:56Z

From: Christoph Hellwig <hch@lst.de>

The kernel-like kasprintf will be used by the new metadir code, as well
as the rmap data structures in xfs_repair.

Signed-off-by: Christoph Hellwig <hch@lst.de>
Reviewed-by: Darrick J. Wong <djwong@kernel.org>
[djwong: tweak commit message]
Signed-off-by: Darrick J. Wong <djwong@kernel.org>
---
include/kmem.h | 3 +++
libxfs/kmem.c | 13 +++++++++++++
2 files changed, 16 insertions(+)

diff --git a/include/kmem.h b/include/kmem.h
index 386b4a6be..8dfb2fb0b 100644
--- a/include/kmem.h
+++ b/include/kmem.h
@@ -65,4 +65,7 @@ static inline void kfree(const void *ptr)
free((void *)ptr);
}

+__attribute__((format(printf,2,3)))
+char *kasprintf(gfp_t gfp, const char *fmt, ...);
+
#endif
diff --git a/libxfs/kmem.c b/libxfs/kmem.c
index a2a3935d0..2e2935180 100644
--- a/libxfs/kmem.c
+++ b/libxfs/kmem.c
@@ -104,3 +104,16 @@ krealloc(void *ptr, size_t new_size, int flags)
}
return ptr;
}
+
+char *kasprintf(gfp_t gfp, const char *fmt, ...)
+{
+ va_list ap;
+ char *p;
+
+ va_start(ap, fmt);
+ if (vasprintf(&p, fmt, ap) < 0)
+ p = NULL;
+ va_end(ap);
+
+ return p;
+}