summaryrefslogtreecommitdiff
path: root/fs/xfs/scrub/array.c
blob: 7871e8dcad5cc21e17c897a8e5017bcdd7c65bd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (C) 2018 Oracle.  All Rights Reserved.
 * Author: Darrick J. Wong <darrick.wong@oracle.com>
 */
#include "xfs.h"
#include "xfs_fs.h"
#include "xfs_shared.h"
#include "scrub/array.h"

/*
 * XFS Fixed-Size Big Memory Array
 * ===============================
 * The big memory array uses a list to store large numbers of fixed-size
 * records in memory.  Access to the array is performed via indexed get and put
 * methods, and an append method is provided for convenience.  Array elements
 * can be set to all zeroes, which means that the entry is NULL and will be
 * skipped during iteration.
 */

struct xa_item {
	struct list_head	list;
	/* array item comes after here */
};

#define XA_ITEM_SIZE(sz)	(sizeof(struct xa_item) + (sz))

/* Initialize a big memory array. */
struct xfbma *
xfbma_init(
	size_t		obj_size)
{
	struct xfbma	*array;
	int		error;

	error = -ENOMEM;
	array = kmem_alloc(sizeof(struct xfbma) + obj_size,
			KM_NOFS | KM_MAYFAIL);
	if (!array)
		return ERR_PTR(error);

	array->obj_size = obj_size;
	array->nr = 0;
	INIT_LIST_HEAD(&array->list);
	memset(&array->cache, 0, sizeof(array->cache));

	return array;
}

void
xfbma_destroy(
	struct xfbma	*array)
{
	struct xa_item	*item, *n;

	list_for_each_entry_safe(item, n, &array->list, list) {
		list_del(&item->list);
		kmem_free(item);
	}
	kmem_free(array);
}

/* Find something in the cache. */
static struct xa_item *
xfbma_cache_lookup(
	struct xfbma	*array,
	uint64_t	nr)
{
	uint64_t	i;

	for (i = 0; i < XMA_CACHE_SIZE; i++)
		if (array->cache[i].nr == nr && array->cache[i].item)
			return array->cache[i].item;
	return NULL;
}

/* Invalidate the lookup cache. */
static void
xfbma_cache_invalidate(
	struct xfbma	*array)
{
	memset(array->cache, 0, sizeof(array->cache));
}

/* Put something in the cache. */
static void
xfbma_cache_store(
	struct xfbma	*array,
	uint64_t	nr,
	struct xa_item	*item)
{
	memmove(array->cache + 1, array->cache,
			sizeof(struct xma_cache) * (XMA_CACHE_SIZE - 1));
	array->cache[0].item = item;
	array->cache[0].nr = nr;
}

/* Find a particular array item. */
static struct xa_item *
xfbma_lookup(
	struct xfbma	*array,
	uint64_t	nr)
{
	struct xa_item	*item;
	uint64_t	i;

	if (nr >= array->nr) {
		ASSERT(0);
		return NULL;
	}

	item = xfbma_cache_lookup(array, nr);
	if (item)
		return item;

	i = 0;
	list_for_each_entry(item, &array->list, list) {
		if (i == nr) {
			xfbma_cache_store(array, nr, item);
			return item;
		}
		i++;
	}
	return NULL;
}

/* Get an element from the array. */
int
xfbma_get(
	struct xfbma	*array,
	uint64_t	nr,
	void		*ptr)
{
	struct xa_item	*item;

	item = xfbma_lookup(array, nr);
	if (!item)
		return -ENODATA;
	memcpy(ptr, item + 1, array->obj_size);
	return 0;
}

/* Put an element in the array. */
int
xfbma_set(
	struct xfbma	*array,
	uint64_t	nr,
	void		*ptr)
{
	struct xa_item	*item;

	item = xfbma_lookup(array, nr);
	if (!item)
		return -ENODATA;
	memcpy(item + 1, ptr, array->obj_size);
	return 0;
}

/* Is this array element NULL? */
bool
xfbma_is_null(
	struct xfbma	*array,
	void		*ptr)
{
	return !memchr_inv(ptr, 0, array->obj_size);
}

/* Put an element anywhere in the array that isn't NULL. */
int
xfbma_insert_anywhere(
	struct xfbma	*array,
	void		*ptr)
{
	struct xa_item	*item;

	/* Find a null slot to put it in. */
	list_for_each_entry(item, &array->list, list) {
		if (!xfbma_is_null(array, item + 1))
			continue;
		memcpy(item + 1, ptr, array->obj_size);
		return 0;
	}

	/* No null slots, just dump it on the end. */
	return xfbma_append(array, ptr);
}

/* NULL an element in the array. */
int
xfbma_nullify(
	struct xfbma	*array,
	uint64_t	nr)
{
	struct xa_item	*item;

	item = xfbma_lookup(array, nr);
	if (!item)
		return -ENODATA;
	memset(item + 1, 0, array->obj_size);
	return 0;
}

/* Append an element to the array. */
int
xfbma_append(
	struct xfbma	*array,
	void		*ptr)
{
	struct xa_item	*item;

	item = kmem_alloc(XA_ITEM_SIZE(array->obj_size), KM_NOFS | KM_MAYFAIL);
	if (!item)
		return -ENOMEM;

	INIT_LIST_HEAD(&item->list);
	memcpy(item + 1, ptr, array->obj_size);
	list_add_tail(&item->list, &array->list);
	array->nr++;
	return 0;
}

/*
 * Iterate every element in this array, freeing each element as we go.
 * Array elements will be shifted down.
 */
int
xfbma_iter_del(
	struct xfbma	*array,
	xfbma_iter_fn	iter_fn,
	void		*priv)
{
	struct xa_item	*item, *n;
	int		error = 0;

	list_for_each_entry_safe(item, n, &array->list, list) {
		if (xfbma_is_null(array, item + 1))
			goto next;
		memcpy(array + 1, item + 1, array->obj_size);
		error = iter_fn(array + 1, priv);
		if (error)
			break;
next:
		list_del(&item->list);
		kmem_free(item);
		array->nr--;
	}

	xfbma_cache_invalidate(array);
	return error;
}

/* Return length of array. */
uint64_t
xfbma_length(
	struct xfbma	*array)
{
	return array->nr;
}

static int
xfbma_item_cmp(
	void			*priv,
	struct list_head	*a,
	struct list_head	*b)
{
	int			(*cmp_fn)(void *a, void *b) = priv;
	struct xa_item		*ai, *bi;

	ai = container_of(a, struct xa_item, list);
	bi = container_of(b, struct xa_item, list);

	return cmp_fn(ai + 1, bi + 1);
}

/* Sort everything in this array. */
int
xfbma_sort(
	struct xfbma	*array,
	xfbma_cmp_fn	cmp_fn)
{
	list_sort(cmp_fn, &array->list, xfbma_item_cmp);
	return 0;
}