aboutsummaryrefslogtreecommitdiff
path: root/tests/zfs-tests/tests/functional/vdev_disk/page_alignment.c
blob: 5c6d28eb2c443c9df0063ec3ac243723f6c84be8 (plain) (blame)
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or https://opensource.org/licenses/CDDL-1.0.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 2023, 2024, Klara Inc.
 */

#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include <sys/param.h>
#include <stdlib.h>

/*
 * This tests the vdev_disk page alignment check callback
 * vdev_disk_check_pages_cb(). For now, this test includes a copy of that
 * function from module/os/linux/zfs/vdev_disk.c. If you change it here,
 * remember to change it there too, and add tests data here to validate the
 * change you're making.
 */

struct page;

typedef struct {
	uint32_t  bmask;
	uint32_t  npages;
	uint32_t  end;
} vdev_disk_check_pages_t;

static int
vdev_disk_check_pages_cb(struct page *page, size_t off, size_t len, void *priv)
{
	(void) page;
	vdev_disk_check_pages_t *s = priv;

	/*
	 * If we didn't finish on a block size boundary last time, then there
	 * would be a gap if we tried to use this ABD as-is, so abort.
	 */
	if (s->end != 0)
		return (1);

	/*
	 * Note if we're taking less than a full block, so we can check it
	 * above on the next call.
	 */
	s->end = (off+len) & s->bmask;

	/* All blocks after the first must start on a block size boundary. */
	if (s->npages != 0 && (off & s->bmask) != 0)
		return (1);

	s->npages++;
	return (0);
}

typedef struct {
	/* test name */
	const char	*name;

	/* blocks size mask */
	uint32_t	mask;

	/* amount of data to take */
	size_t		size;

	/* [start offset in page, len to end of page or size] */
	size_t		pages[16][2];
} page_test_t;

static const page_test_t valid_tests[] = {
	/* 512B block tests */
	{
		"512B blocks, 4K single page",
		0x1ff, 0x1000, {
			{ 0x0, 0x1000 },
		},
	}, {
		"512B blocks, 1K at start of page",
		0x1ff, 0x400, {
			{ 0x0, 0x1000 },
		},
	}, {
		"512B blocks, 1K at end of page",
		0x1ff, 0x400, {
			{ 0x0c00, 0x0400 },
		},
	}, {
		"512B blocks, 1K within page, 512B start offset",
		0x1ff, 0x400, {
			{ 0x0200, 0x0e00 },
		},
	}, {
		"512B blocks, 8K across 2x4K pages",
		0x1ff, 0x2000, {
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
		},
	}, {
		"512B blocks, 4K across two pages, 2K start offset",
		0x1ff, 0x1000, {
			{ 0x0800, 0x0800 },
			{ 0x0,    0x0800 },
		},
	}, {
		"512B blocks, 16K across 5x4K pages, 512B start offset",
		0x1ff, 0x4000, {
			{ 0x0200, 0x0e00 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0200 },
		},
	}, {
		"512B blocks, 64K data, 8x8K compound pages",
		0x1ff, 0x10000, {
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
		},
	}, {
		"512B blocks, 64K data, 9x8K compound pages, 512B start offset",
		0x1ff, 0x10000, {
			{ 0x0200, 0x1e00 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x0200 },
		},
	}, {
		"512B blocks, 64K data, 2x16K compound pages, 8x4K pages",
		0x1ff, 0x10000, {
			{ 0x0, 0x8000 },
			{ 0x0, 0x8000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
		},
	}, {
		"512B blocks, 64K data, mixed 4K/8K/16K pages",
		0x1ff, 0x10000, {
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x8000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
		},
	}, {
		"512B blocks, 64K data, mixed 4K/8K/16K pages, 1K start offset",
		0x1ff, 0x10000, {
			{ 0x0400, 0x0c00 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x8000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0400 },
		},
	},

	/* 4K block tests */
	{
		"4K blocks, 4K single page",
		0xfff, 0x1000, {
			{ 0x0, 0x1000 },
		},
	}, {
		"4K blocks, 1K at start of page",
		0xfff, 0x400, {
			{ 0x0, 0x1000 },
		},
	}, {
		"4K blocks, 1K at end of page",
		0xfff, 0x400, {
			{ 0x0c00, 0x0400 },
		},
	}, {
		"4K blocks, 1K within page, 512B start offset",
		0xfff, 0x400, {
			{ 0x0200, 0x0e00 },
		},
	}, {
		"4K blocks, 8K across 2x4K pages",
		0xfff, 0x2000, {
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
		},
	}, {
		"4K blocks, 4K across two pages, 2K start offset",
		0xfff, 0x1000, {
			{ 0x0800, 0x0800 },
			{ 0x0,    0x0800 },
		},
	}, {
		"4K blocks, 16K across 5x4K pages, 512B start offset",
		0xfff, 0x4000, {
			{ 0x0200, 0x0e00 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0200 },
		},
	}, {
		"4K blocks, 64K data, 8x8K compound pages",
		0xfff, 0x10000, {
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x2000 },
		},
	}, {
		"4K blocks, 64K data, 9x8K compound pages, 512B start offset",
		0xfff, 0x10000, {
			{ 0x0200, 0x1e00 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x0200 },
		},
	}, {
		"4K blocks, 64K data, 2x16K compound pages, 8x4K pages",
		0xfff, 0x10000, {
			{ 0x0, 0x8000 },
			{ 0x0, 0x8000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
		},
	}, {
		"4K blocks, 64K data, mixed 4K/8K/16K pages",
		0xfff, 0x10000, {
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x8000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x2000 },
		},
	}, {
		"4K blocks, 64K data, mixed 4K/8K/16K pages, 1K start offset",
		0xfff, 0x10000, {
			{ 0x0400, 0x0c00 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x2000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x8000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0400 },
		},
	},

	{ 0 },
};

static const page_test_t invalid_tests[] = {
	{
		"512B blocks, 16K data, 512 leader (gang block simulation)",
		0x1ff, 0x8000, {
			{ 0x0, 0x0200 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x1000 },
			{ 0x0, 0x0c00 },
		},
	}, {
		"4K blocks, 32K data, 2 incompatible spans "
		"(gang abd simulation)",
		0xfff, 0x8000, {
			{ 0x0800, 0x0800 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0800 },
			{ 0x0800, 0x0800 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x1000 },
			{ 0x0,    0x0800 },
		},
	},
	{ 0 },
};

static bool
run_test(const page_test_t *test, bool verbose)
{
	size_t rem = test->size;

	vdev_disk_check_pages_t s = {
	    .bmask = 0xfff,
	    .npages = 0,
	    .end = 0,
	};

	for (int i = 0; test->pages[i][1] > 0; i++) {
		size_t off = test->pages[i][0];
		size_t len = test->pages[i][1];

		size_t take = MIN(rem, len);

		if (verbose)
			printf("  page %d [off %lx len %lx], "
			    "rem %lx, take %lx\n",
			    i, off, len, rem, take);

		if (vdev_disk_check_pages_cb(NULL, off, take, &s)) {
			if (verbose)
				printf("  ABORT: misalignment detected, "
				    "rem %lx\n", rem);
			return (false);
		}

		rem -= take;
		if (rem == 0)
			break;
	}

	if (rem > 0) {
		if (verbose)
			printf("  ABORT: ran out of pages, rem %lx\n", rem);
		return (false);
	}

	return (true);
}

static void
run_test_set(const page_test_t *tests, bool want, int *ntests, int *npassed)
{
	for (const page_test_t *test = &tests[0]; test->name; test++) {
		bool pass = (run_test(test, false) == want);
		if (pass) {
			printf("%s: PASS\n", test->name);
			(*npassed)++;
		} else {
			printf("%s: FAIL [expected %s, got %s]\n", test->name,
			    want ? "VALID" : "INVALID",
			    want ? "INVALID" : "VALID");
			run_test(test, true);
		}
		(*ntests)++;
	}
}

int main(void) {
	int ntests = 0, npassed = 0;

	run_test_set(valid_tests, true, &ntests, &npassed);
	run_test_set(invalid_tests, false, &ntests, &npassed);

	printf("\n%d/%d tests passed\n", npassed, ntests);

	return (ntests == npassed ? 0 : 1);
}