1*3934e8ebSDarrick J. Wong /* SPDX-License-Identifier: GPL-2.0-or-later */ 2*3934e8ebSDarrick J. Wong /* 3*3934e8ebSDarrick J. Wong * Copyright (C) 2021-2023 Oracle. All Rights Reserved. 4*3934e8ebSDarrick J. Wong * Author: Darrick J. Wong <djwong@kernel.org> 5*3934e8ebSDarrick J. Wong */ 6*3934e8ebSDarrick J. Wong #ifndef __XFS_SCRUB_XFARRAY_H__ 7*3934e8ebSDarrick J. Wong #define __XFS_SCRUB_XFARRAY_H__ 8*3934e8ebSDarrick J. Wong 9*3934e8ebSDarrick J. Wong /* xfile array index type, along with cursor initialization */ 10*3934e8ebSDarrick J. Wong typedef uint64_t xfarray_idx_t; 11*3934e8ebSDarrick J. Wong #define XFARRAY_CURSOR_INIT ((__force xfarray_idx_t)0) 12*3934e8ebSDarrick J. Wong 13*3934e8ebSDarrick J. Wong /* Iterate each index of an xfile array. */ 14*3934e8ebSDarrick J. Wong #define foreach_xfarray_idx(array, idx) \ 15*3934e8ebSDarrick J. Wong for ((idx) = XFARRAY_CURSOR_INIT; \ 16*3934e8ebSDarrick J. Wong (idx) < xfarray_length(array); \ 17*3934e8ebSDarrick J. Wong (idx)++) 18*3934e8ebSDarrick J. Wong 19*3934e8ebSDarrick J. Wong struct xfarray { 20*3934e8ebSDarrick J. Wong /* Underlying file that backs the array. */ 21*3934e8ebSDarrick J. Wong struct xfile *xfile; 22*3934e8ebSDarrick J. Wong 23*3934e8ebSDarrick J. Wong /* Number of array elements. */ 24*3934e8ebSDarrick J. Wong xfarray_idx_t nr; 25*3934e8ebSDarrick J. Wong 26*3934e8ebSDarrick J. Wong /* Maximum possible array size. */ 27*3934e8ebSDarrick J. Wong xfarray_idx_t max_nr; 28*3934e8ebSDarrick J. Wong 29*3934e8ebSDarrick J. Wong /* Number of unset slots in the array below @nr. */ 30*3934e8ebSDarrick J. Wong uint64_t unset_slots; 31*3934e8ebSDarrick J. Wong 32*3934e8ebSDarrick J. Wong /* Size of an array element. */ 33*3934e8ebSDarrick J. Wong size_t obj_size; 34*3934e8ebSDarrick J. Wong 35*3934e8ebSDarrick J. Wong /* log2 of array element size, if possible. */ 36*3934e8ebSDarrick J. Wong int obj_size_log; 37*3934e8ebSDarrick J. Wong }; 38*3934e8ebSDarrick J. Wong 39*3934e8ebSDarrick J. Wong int xfarray_create(const char *descr, unsigned long long required_capacity, 40*3934e8ebSDarrick J. Wong size_t obj_size, struct xfarray **arrayp); 41*3934e8ebSDarrick J. Wong void xfarray_destroy(struct xfarray *array); 42*3934e8ebSDarrick J. Wong int xfarray_load(struct xfarray *array, xfarray_idx_t idx, void *ptr); 43*3934e8ebSDarrick J. Wong int xfarray_unset(struct xfarray *array, xfarray_idx_t idx); 44*3934e8ebSDarrick J. Wong int xfarray_store(struct xfarray *array, xfarray_idx_t idx, const void *ptr); 45*3934e8ebSDarrick J. Wong int xfarray_store_anywhere(struct xfarray *array, const void *ptr); 46*3934e8ebSDarrick J. Wong bool xfarray_element_is_null(struct xfarray *array, const void *ptr); 47*3934e8ebSDarrick J. Wong 48*3934e8ebSDarrick J. Wong /* Append an element to the array. */ 49*3934e8ebSDarrick J. Wong static inline int xfarray_append(struct xfarray *array, const void *ptr) 50*3934e8ebSDarrick J. Wong { 51*3934e8ebSDarrick J. Wong return xfarray_store(array, array->nr, ptr); 52*3934e8ebSDarrick J. Wong } 53*3934e8ebSDarrick J. Wong 54*3934e8ebSDarrick J. Wong uint64_t xfarray_length(struct xfarray *array); 55*3934e8ebSDarrick J. Wong int xfarray_load_next(struct xfarray *array, xfarray_idx_t *idx, void *rec); 56*3934e8ebSDarrick J. Wong 57*3934e8ebSDarrick J. Wong #endif /* __XFS_SCRUB_XFARRAY_H__ */ 58