Linux Audio

Check our new training course

Loading...
v6.8
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 Fusion IO.  All rights reserved.
  4 */
  5
  6#include <linux/pagemap.h>
  7#include <linux/pagevec.h>
  8#include <linux/sched.h>
  9#include <linux/slab.h>
 10#include <linux/sizes.h>
 11#include "btrfs-tests.h"
 12#include "../ctree.h"
 13#include "../extent_io.h"
 14#include "../btrfs_inode.h"
 15
 16#define PROCESS_UNLOCK		(1 << 0)
 17#define PROCESS_RELEASE		(1 << 1)
 18#define PROCESS_TEST_LOCKED	(1 << 2)
 19
 20static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
 21				       unsigned long flags)
 22{
 23	int ret;
 24	struct folio_batch fbatch;
 25	unsigned long index = start >> PAGE_SHIFT;
 26	unsigned long end_index = end >> PAGE_SHIFT;
 
 27	int i;
 28	int count = 0;
 29	int loops = 0;
 30
 31	folio_batch_init(&fbatch);
 32
 33	while (index <= end_index) {
 34		ret = filemap_get_folios_contig(inode->i_mapping, &index,
 35				end_index, &fbatch);
 36		for (i = 0; i < ret; i++) {
 37			struct folio *folio = fbatch.folios[i];
 38
 39			if (flags & PROCESS_TEST_LOCKED &&
 40			    !folio_test_locked(folio))
 41				count++;
 42			if (flags & PROCESS_UNLOCK && folio_test_locked(folio))
 43				folio_unlock(folio);
 
 44			if (flags & PROCESS_RELEASE)
 45				folio_put(folio);
 46		}
 47		folio_batch_release(&fbatch);
 
 48		cond_resched();
 49		loops++;
 50		if (loops > 100000) {
 51			printk(KERN_ERR
 52		"stuck in a loop, start %llu, end %llu, ret %d\n",
 53				start, end, ret);
 54			break;
 55		}
 56	}
 57
 58	return count;
 59}
 60
 61#define STATE_FLAG_STR_LEN			256
 62
 63#define PRINT_ONE_FLAG(state, dest, cur, name)				\
 64({									\
 65	if (state->state & EXTENT_##name)				\
 66		cur += scnprintf(dest + cur, STATE_FLAG_STR_LEN - cur,	\
 67				 "%s" #name, cur == 0 ? "" : "|");	\
 68})
 69
 70static void extent_flag_to_str(const struct extent_state *state, char *dest)
 71{
 72	int cur = 0;
 73
 74	dest[0] = 0;
 75	PRINT_ONE_FLAG(state, dest, cur, DIRTY);
 76	PRINT_ONE_FLAG(state, dest, cur, UPTODATE);
 77	PRINT_ONE_FLAG(state, dest, cur, LOCKED);
 78	PRINT_ONE_FLAG(state, dest, cur, NEW);
 79	PRINT_ONE_FLAG(state, dest, cur, DELALLOC);
 80	PRINT_ONE_FLAG(state, dest, cur, DEFRAG);
 81	PRINT_ONE_FLAG(state, dest, cur, BOUNDARY);
 82	PRINT_ONE_FLAG(state, dest, cur, NODATASUM);
 83	PRINT_ONE_FLAG(state, dest, cur, CLEAR_META_RESV);
 84	PRINT_ONE_FLAG(state, dest, cur, NEED_WAIT);
 85	PRINT_ONE_FLAG(state, dest, cur, NORESERVE);
 86	PRINT_ONE_FLAG(state, dest, cur, QGROUP_RESERVED);
 87	PRINT_ONE_FLAG(state, dest, cur, CLEAR_DATA_RESV);
 88}
 89
 90static void dump_extent_io_tree(const struct extent_io_tree *tree)
 91{
 92	struct rb_node *node;
 93	char flags_str[STATE_FLAG_STR_LEN];
 94
 95	node = rb_first(&tree->state);
 96	test_msg("io tree content:");
 97	while (node) {
 98		struct extent_state *state;
 99
100		state = rb_entry(node, struct extent_state, rb_node);
101		extent_flag_to_str(state, flags_str);
102		test_msg("  start=%llu len=%llu flags=%s", state->start,
103			 state->end + 1 - state->start, flags_str);
104		node = rb_next(node);
105	}
106}
107
108static int test_find_delalloc(u32 sectorsize)
109{
110	struct inode *inode;
111	struct extent_io_tree *tmp;
112	struct page *page;
113	struct page *locked_page = NULL;
114	unsigned long index = 0;
115	/* In this test we need at least 2 file extents at its maximum size */
116	u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
117	u64 total_dirty = 2 * max_bytes;
118	u64 start, end, test_start;
119	bool found;
120	int ret = -EINVAL;
121
122	test_msg("running find delalloc tests");
123
124	inode = btrfs_new_test_inode();
125	if (!inode) {
126		test_std_err(TEST_ALLOC_INODE);
127		return -ENOMEM;
128	}
129	tmp = &BTRFS_I(inode)->io_tree;
130
131	/*
132	 * Passing NULL as we don't have fs_info but tracepoints are not used
133	 * at this point
134	 */
135	extent_io_tree_init(NULL, tmp, IO_TREE_SELFTEST);
136
137	/*
138	 * First go through and create and mark all of our pages dirty, we pin
139	 * everything to make sure our pages don't get evicted and screw up our
140	 * test.
141	 */
142	for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
143		page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
144		if (!page) {
145			test_err("failed to allocate test page");
146			ret = -ENOMEM;
147			goto out;
148		}
149		SetPageDirty(page);
150		if (index) {
151			unlock_page(page);
152		} else {
153			get_page(page);
154			locked_page = page;
155		}
156	}
157
158	/* Test this scenario
159	 * |--- delalloc ---|
160	 * |---  search  ---|
161	 */
162	set_extent_bit(tmp, 0, sectorsize - 1, EXTENT_DELALLOC, NULL);
163	start = 0;
164	end = start + PAGE_SIZE - 1;
165	found = find_lock_delalloc_range(inode, locked_page, &start,
166					 &end);
167	if (!found) {
168		test_err("should have found at least one delalloc");
169		goto out_bits;
170	}
171	if (start != 0 || end != (sectorsize - 1)) {
172		test_err("expected start 0 end %u, got start %llu end %llu",
173			sectorsize - 1, start, end);
174		goto out_bits;
175	}
176	unlock_extent(tmp, start, end, NULL);
177	unlock_page(locked_page);
178	put_page(locked_page);
179
180	/*
181	 * Test this scenario
182	 *
183	 * |--- delalloc ---|
184	 *           |--- search ---|
185	 */
186	test_start = SZ_64M;
187	locked_page = find_lock_page(inode->i_mapping,
188				     test_start >> PAGE_SHIFT);
189	if (!locked_page) {
190		test_err("couldn't find the locked page");
191		goto out_bits;
192	}
193	set_extent_bit(tmp, sectorsize, max_bytes - 1, EXTENT_DELALLOC, NULL);
194	start = test_start;
195	end = start + PAGE_SIZE - 1;
196	found = find_lock_delalloc_range(inode, locked_page, &start,
197					 &end);
198	if (!found) {
199		test_err("couldn't find delalloc in our range");
200		goto out_bits;
201	}
202	if (start != test_start || end != max_bytes - 1) {
203		test_err("expected start %llu end %llu, got start %llu, end %llu",
204				test_start, max_bytes - 1, start, end);
205		goto out_bits;
206	}
207	if (process_page_range(inode, start, end,
208			       PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
209		test_err("there were unlocked pages in the range");
210		goto out_bits;
211	}
212	unlock_extent(tmp, start, end, NULL);
213	/* locked_page was unlocked above */
214	put_page(locked_page);
215
216	/*
217	 * Test this scenario
218	 * |--- delalloc ---|
219	 *                    |--- search ---|
220	 */
221	test_start = max_bytes + sectorsize;
222	locked_page = find_lock_page(inode->i_mapping, test_start >>
223				     PAGE_SHIFT);
224	if (!locked_page) {
225		test_err("couldn't find the locked page");
226		goto out_bits;
227	}
228	start = test_start;
229	end = start + PAGE_SIZE - 1;
230	found = find_lock_delalloc_range(inode, locked_page, &start,
231					 &end);
232	if (found) {
233		test_err("found range when we shouldn't have");
234		goto out_bits;
235	}
236	if (end != test_start + PAGE_SIZE - 1) {
237		test_err("did not return the proper end offset");
238		goto out_bits;
239	}
240
241	/*
242	 * Test this scenario
243	 * [------- delalloc -------|
244	 * [max_bytes]|-- search--|
245	 *
246	 * We are re-using our test_start from above since it works out well.
247	 */
248	set_extent_bit(tmp, max_bytes, total_dirty - 1, EXTENT_DELALLOC, NULL);
249	start = test_start;
250	end = start + PAGE_SIZE - 1;
251	found = find_lock_delalloc_range(inode, locked_page, &start,
252					 &end);
253	if (!found) {
254		test_err("didn't find our range");
255		goto out_bits;
256	}
257	if (start != test_start || end != total_dirty - 1) {
258		test_err("expected start %llu end %llu, got start %llu end %llu",
259			 test_start, total_dirty - 1, start, end);
260		goto out_bits;
261	}
262	if (process_page_range(inode, start, end,
263			       PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
264		test_err("pages in range were not all locked");
265		goto out_bits;
266	}
267	unlock_extent(tmp, start, end, NULL);
268
269	/*
270	 * Now to test where we run into a page that is no longer dirty in the
271	 * range we want to find.
272	 */
273	page = find_get_page(inode->i_mapping,
274			     (max_bytes + SZ_1M) >> PAGE_SHIFT);
275	if (!page) {
276		test_err("couldn't find our page");
277		goto out_bits;
278	}
279	ClearPageDirty(page);
280	put_page(page);
281
282	/* We unlocked it in the previous test */
283	lock_page(locked_page);
284	start = test_start;
285	end = start + PAGE_SIZE - 1;
286	/*
287	 * Currently if we fail to find dirty pages in the delalloc range we
288	 * will adjust max_bytes down to PAGE_SIZE and then re-search.  If
289	 * this changes at any point in the future we will need to fix this
290	 * tests expected behavior.
291	 */
292	found = find_lock_delalloc_range(inode, locked_page, &start,
293					 &end);
294	if (!found) {
295		test_err("didn't find our range");
296		goto out_bits;
297	}
298	if (start != test_start && end != test_start + PAGE_SIZE - 1) {
299		test_err("expected start %llu end %llu, got start %llu end %llu",
300			 test_start, test_start + PAGE_SIZE - 1, start, end);
 
301		goto out_bits;
302	}
303	if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
304			       PROCESS_UNLOCK)) {
305		test_err("pages in range were not all locked");
306		goto out_bits;
307	}
308	ret = 0;
309out_bits:
310	if (ret)
311		dump_extent_io_tree(tmp);
312	clear_extent_bits(tmp, 0, total_dirty - 1, (unsigned)-1);
313out:
314	if (locked_page)
315		put_page(locked_page);
316	process_page_range(inode, 0, total_dirty - 1,
317			   PROCESS_UNLOCK | PROCESS_RELEASE);
318	iput(inode);
319	return ret;
320}
321
322static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb)
 
323{
324	unsigned long i;
325
326	for (i = 0; i < eb->len * BITS_PER_BYTE; i++) {
327		int bit, bit1;
328
329		bit = !!test_bit(i, bitmap);
330		bit1 = !!extent_buffer_test_bit(eb, 0, i);
331		if (bit1 != bit) {
332			u8 has;
333			u8 expect;
334
335			read_extent_buffer(eb, &has, i / BITS_PER_BYTE, 1);
336			expect = bitmap_get_value8(bitmap, ALIGN(i, BITS_PER_BYTE));
337
338			test_err(
339		"bits do not match, start byte 0 bit %lu, byte %lu has 0x%02x expect 0x%02x",
340				 i, i / BITS_PER_BYTE, has, expect);
341			return -EINVAL;
342		}
343
344		bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
345						i % BITS_PER_BYTE);
346		if (bit1 != bit) {
347			u8 has;
348			u8 expect;
349
350			read_extent_buffer(eb, &has, i / BITS_PER_BYTE, 1);
351			expect = bitmap_get_value8(bitmap, ALIGN(i, BITS_PER_BYTE));
352
353			test_err(
354		"bits do not match, start byte %lu bit %lu, byte %lu has 0x%02x expect 0x%02x",
355				 i / BITS_PER_BYTE, i % BITS_PER_BYTE,
356				 i / BITS_PER_BYTE, has, expect);
357			return -EINVAL;
358		}
359	}
360	return 0;
361}
362
363static int test_bitmap_set(const char *name, unsigned long *bitmap,
364			   struct extent_buffer *eb,
365			   unsigned long byte_start, unsigned long bit_start,
366			   unsigned long bit_len)
367{
368	int ret;
369
370	bitmap_set(bitmap, byte_start * BITS_PER_BYTE + bit_start, bit_len);
371	extent_buffer_bitmap_set(eb, byte_start, bit_start, bit_len);
372	ret = check_eb_bitmap(bitmap, eb);
373	if (ret < 0)
374		test_err("%s test failed", name);
375	return ret;
376}
377
378static int test_bitmap_clear(const char *name, unsigned long *bitmap,
379			     struct extent_buffer *eb,
380			     unsigned long byte_start, unsigned long bit_start,
381			     unsigned long bit_len)
382{
383	int ret;
384
385	bitmap_clear(bitmap, byte_start * BITS_PER_BYTE + bit_start, bit_len);
386	extent_buffer_bitmap_clear(eb, byte_start, bit_start, bit_len);
387	ret = check_eb_bitmap(bitmap, eb);
388	if (ret < 0)
389		test_err("%s test failed", name);
390	return ret;
391}
392static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb)
393{
394	unsigned long i, j;
395	unsigned long byte_len = eb->len;
396	u32 x;
397	int ret;
398
399	ret = test_bitmap_clear("clear all run 1", bitmap, eb, 0, 0,
400				byte_len * BITS_PER_BYTE);
401	if (ret < 0)
402		return ret;
403
404	ret = test_bitmap_set("set all", bitmap, eb, 0, 0, byte_len * BITS_PER_BYTE);
405	if (ret < 0)
406		return ret;
407
408	ret = test_bitmap_clear("clear all run 2", bitmap, eb, 0, 0,
409				byte_len * BITS_PER_BYTE);
410	if (ret < 0)
411		return ret;
412
413	ret = test_bitmap_set("same byte set", bitmap, eb, 0, 2, 4);
414	if (ret < 0)
415		return ret;
416
417	ret = test_bitmap_clear("same byte partial clear", bitmap, eb, 0, 4, 1);
418	if (ret < 0)
419		return ret;
420
421	ret = test_bitmap_set("cross byte set", bitmap, eb, 2, 4, 8);
422	if (ret < 0)
423		return ret;
424
425	ret = test_bitmap_set("cross multi byte set", bitmap, eb, 4, 4, 24);
426	if (ret < 0)
427		return ret;
428
429	ret = test_bitmap_clear("cross byte clear", bitmap, eb, 2, 6, 4);
430	if (ret < 0)
 
 
 
431		return ret;
 
432
433	ret = test_bitmap_clear("cross multi byte clear", bitmap, eb, 4, 6, 20);
434	if (ret < 0)
 
 
 
435		return ret;
 
436
437	/* Straddling pages test */
438	if (byte_len > PAGE_SIZE) {
439		ret = test_bitmap_set("cross page set", bitmap, eb,
440				      PAGE_SIZE - sizeof(long) / 2, 0,
441				      sizeof(long) * BITS_PER_BYTE);
442		if (ret < 0)
443			return ret;
444
445		ret = test_bitmap_set("cross page set all", bitmap, eb, 0, 0,
446				      byte_len * BITS_PER_BYTE);
447		if (ret < 0)
448			return ret;
 
449
450		ret = test_bitmap_clear("cross page clear", bitmap, eb,
451					PAGE_SIZE - sizeof(long) / 2, 0,
 
 
 
 
452					sizeof(long) * BITS_PER_BYTE);
453		if (ret < 0)
 
 
454			return ret;
 
455	}
456
457	/*
458	 * Generate a wonky pseudo-random bit pattern for the sake of not using
459	 * something repetitive that could miss some hypothetical off-by-n bug.
460	 */
461	x = 0;
462	ret = test_bitmap_clear("clear all run 3", bitmap, eb, 0, 0,
463				byte_len * BITS_PER_BYTE);
464	if (ret < 0)
465		return ret;
466
467	for (i = 0; i < byte_len * BITS_PER_BYTE / 32; i++) {
468		x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
469		for (j = 0; j < 32; j++) {
470			if (x & (1U << j)) {
471				bitmap_set(bitmap, i * 32 + j, 1);
472				extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
473			}
474		}
475	}
476
477	ret = check_eb_bitmap(bitmap, eb);
478	if (ret) {
479		test_err("random bit pattern failed");
480		return ret;
481	}
482
483	return 0;
484}
485
486static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
487{
488	struct btrfs_fs_info *fs_info;
489	unsigned long *bitmap = NULL;
490	struct extent_buffer *eb = NULL;
 
491	int ret;
492
493	test_msg("running extent buffer bitmap tests");
494
495	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
496	if (!fs_info) {
497		test_std_err(TEST_ALLOC_FS_INFO);
498		return -ENOMEM;
499	}
 
500
501	bitmap = kmalloc(nodesize, GFP_KERNEL);
 
 
502	if (!bitmap) {
503		test_err("couldn't allocate test bitmap");
504		ret = -ENOMEM;
505		goto out;
506	}
507
508	eb = __alloc_dummy_extent_buffer(fs_info, 0, nodesize);
509	if (!eb) {
510		test_std_err(TEST_ALLOC_ROOT);
511		ret = -ENOMEM;
512		goto out;
513	}
514
515	ret = __test_eb_bitmaps(bitmap, eb);
516	if (ret)
517		goto out;
518
 
519	free_extent_buffer(eb);
520
521	/*
522	 * Test again for case where the tree block is sectorsize aligned but
523	 * not nodesize aligned.
524	 */
525	eb = __alloc_dummy_extent_buffer(fs_info, sectorsize, nodesize);
526	if (!eb) {
527		test_std_err(TEST_ALLOC_ROOT);
528		ret = -ENOMEM;
529		goto out;
530	}
531
532	ret = __test_eb_bitmaps(bitmap, eb);
533out:
534	free_extent_buffer(eb);
535	kfree(bitmap);
536	btrfs_free_dummy_fs_info(fs_info);
537	return ret;
538}
539
540static int test_find_first_clear_extent_bit(void)
541{
542	struct extent_io_tree tree;
543	u64 start, end;
544	int ret = -EINVAL;
545
546	test_msg("running find_first_clear_extent_bit test");
547
548	extent_io_tree_init(NULL, &tree, IO_TREE_SELFTEST);
549
550	/* Test correct handling of empty tree */
551	find_first_clear_extent_bit(&tree, 0, &start, &end, CHUNK_TRIMMED);
552	if (start != 0 || end != -1) {
553		test_err(
554	"error getting a range from completely empty tree: start %llu end %llu",
555			 start, end);
556		goto out;
557	}
558	/*
559	 * Set 1M-4M alloc/discard and 32M-64M thus leaving a hole between
560	 * 4M-32M
561	 */
562	set_extent_bit(&tree, SZ_1M, SZ_4M - 1,
563		       CHUNK_TRIMMED | CHUNK_ALLOCATED, NULL);
564
565	find_first_clear_extent_bit(&tree, SZ_512K, &start, &end,
566				    CHUNK_TRIMMED | CHUNK_ALLOCATED);
567
568	if (start != 0 || end != SZ_1M - 1) {
569		test_err("error finding beginning range: start %llu end %llu",
570			 start, end);
571		goto out;
572	}
573
574	/* Now add 32M-64M so that we have a hole between 4M-32M */
575	set_extent_bit(&tree, SZ_32M, SZ_64M - 1,
576		       CHUNK_TRIMMED | CHUNK_ALLOCATED, NULL);
577
578	/*
579	 * Request first hole starting at 12M, we should get 4M-32M
580	 */
581	find_first_clear_extent_bit(&tree, 12 * SZ_1M, &start, &end,
582				    CHUNK_TRIMMED | CHUNK_ALLOCATED);
583
584	if (start != SZ_4M || end != SZ_32M - 1) {
585		test_err("error finding trimmed range: start %llu end %llu",
586			 start, end);
587		goto out;
588	}
589
590	/*
591	 * Search in the middle of allocated range, should get the next one
592	 * available, which happens to be unallocated -> 4M-32M
593	 */
594	find_first_clear_extent_bit(&tree, SZ_2M, &start, &end,
595				    CHUNK_TRIMMED | CHUNK_ALLOCATED);
596
597	if (start != SZ_4M || end != SZ_32M - 1) {
598		test_err("error finding next unalloc range: start %llu end %llu",
599			 start, end);
600		goto out;
601	}
602
603	/*
604	 * Set 64M-72M with CHUNK_ALLOC flag, then search for CHUNK_TRIMMED flag
605	 * being unset in this range, we should get the entry in range 64M-72M
606	 */
607	set_extent_bit(&tree, SZ_64M, SZ_64M + SZ_8M - 1, CHUNK_ALLOCATED, NULL);
608	find_first_clear_extent_bit(&tree, SZ_64M + SZ_1M, &start, &end,
609				    CHUNK_TRIMMED);
610
611	if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
612		test_err("error finding exact range: start %llu end %llu",
613			 start, end);
614		goto out;
615	}
616
617	find_first_clear_extent_bit(&tree, SZ_64M - SZ_8M, &start, &end,
618				    CHUNK_TRIMMED);
619
620	/*
621	 * Search in the middle of set range whose immediate neighbour doesn't
622	 * have the bits set so it must be returned
623	 */
624	if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
625		test_err("error finding next alloc range: start %llu end %llu",
626			 start, end);
627		goto out;
628	}
629
630	/*
631	 * Search beyond any known range, shall return after last known range
632	 * and end should be -1
633	 */
634	find_first_clear_extent_bit(&tree, -1, &start, &end, CHUNK_TRIMMED);
635	if (start != SZ_64M + SZ_8M || end != -1) {
636		test_err(
637		"error handling beyond end of range search: start %llu end %llu",
638			start, end);
639		goto out;
640	}
641
642	ret = 0;
643out:
644	if (ret)
645		dump_extent_io_tree(&tree);
646	clear_extent_bits(&tree, 0, (u64)-1, CHUNK_TRIMMED | CHUNK_ALLOCATED);
647
648	return ret;
649}
650
651static void dump_eb_and_memory_contents(struct extent_buffer *eb, void *memory,
652					const char *test_name)
653{
654	for (int i = 0; i < eb->len; i++) {
655		struct page *page = folio_page(eb->folios[i >> PAGE_SHIFT], 0);
656		void *addr = page_address(page) + offset_in_page(i);
657
658		if (memcmp(addr, memory + i, 1) != 0) {
659			test_err("%s failed", test_name);
660			test_err("eb and memory diffs at byte %u, eb has 0x%02x memory has 0x%02x",
661				 i, *(u8 *)addr, *(u8 *)(memory + i));
662			return;
663		}
664	}
665}
666
667static int verify_eb_and_memory(struct extent_buffer *eb, void *memory,
668				const char *test_name)
669{
670	for (int i = 0; i < (eb->len >> PAGE_SHIFT); i++) {
671		void *eb_addr = folio_address(eb->folios[i]);
672
673		if (memcmp(memory + (i << PAGE_SHIFT), eb_addr, PAGE_SIZE) != 0) {
674			dump_eb_and_memory_contents(eb, memory, test_name);
675			return -EUCLEAN;
676		}
677	}
678	return 0;
679}
680
681/*
682 * Init both memory and extent buffer contents to the same randomly generated
683 * contents.
684 */
685static void init_eb_and_memory(struct extent_buffer *eb, void *memory)
686{
687	get_random_bytes(memory, eb->len);
688	write_extent_buffer(eb, memory, 0, eb->len);
689}
690
691static int test_eb_mem_ops(u32 sectorsize, u32 nodesize)
692{
693	struct btrfs_fs_info *fs_info;
694	struct extent_buffer *eb = NULL;
695	void *memory = NULL;
696	int ret;
697
698	test_msg("running extent buffer memory operation tests");
699
700	fs_info = btrfs_alloc_dummy_fs_info(nodesize, sectorsize);
701	if (!fs_info) {
702		test_std_err(TEST_ALLOC_FS_INFO);
703		return -ENOMEM;
704	}
705
706	memory = kvzalloc(nodesize, GFP_KERNEL);
707	if (!memory) {
708		test_err("failed to allocate memory");
709		ret = -ENOMEM;
710		goto out;
711	}
712
713	eb = __alloc_dummy_extent_buffer(fs_info, SZ_1M, nodesize);
714	if (!eb) {
715		test_std_err(TEST_ALLOC_EXTENT_BUFFER);
716		ret = -ENOMEM;
717		goto out;
718	}
719
720	init_eb_and_memory(eb, memory);
721	ret = verify_eb_and_memory(eb, memory, "full eb write");
722	if (ret < 0)
723		goto out;
724
725	memcpy(memory, memory + 16, 16);
726	memcpy_extent_buffer(eb, 0, 16, 16);
727	ret = verify_eb_and_memory(eb, memory, "same page non-overlapping memcpy 1");
728	if (ret < 0)
729		goto out;
730
731	memcpy(memory, memory + 2048, 16);
732	memcpy_extent_buffer(eb, 0, 2048, 16);
733	ret = verify_eb_and_memory(eb, memory, "same page non-overlapping memcpy 2");
734	if (ret < 0)
735		goto out;
736	memcpy(memory, memory + 2048, 2048);
737	memcpy_extent_buffer(eb, 0, 2048, 2048);
738	ret = verify_eb_and_memory(eb, memory, "same page non-overlapping memcpy 3");
739	if (ret < 0)
740		goto out;
741
742	memmove(memory + 512, memory + 256, 512);
743	memmove_extent_buffer(eb, 512, 256, 512);
744	ret = verify_eb_and_memory(eb, memory, "same page overlapping memcpy 1");
745	if (ret < 0)
746		goto out;
747
748	memmove(memory + 2048, memory + 512, 2048);
749	memmove_extent_buffer(eb, 2048, 512, 2048);
750	ret = verify_eb_and_memory(eb, memory, "same page overlapping memcpy 2");
751	if (ret < 0)
752		goto out;
753	memmove(memory + 512, memory + 2048, 2048);
754	memmove_extent_buffer(eb, 512, 2048, 2048);
755	ret = verify_eb_and_memory(eb, memory, "same page overlapping memcpy 3");
756	if (ret < 0)
757		goto out;
758
759	if (nodesize > PAGE_SIZE) {
760		memcpy(memory, memory + 4096 - 128, 256);
761		memcpy_extent_buffer(eb, 0, 4096 - 128, 256);
762		ret = verify_eb_and_memory(eb, memory, "cross page non-overlapping memcpy 1");
763		if (ret < 0)
764			goto out;
765
766		memcpy(memory + 4096 - 128, memory + 4096 + 128, 256);
767		memcpy_extent_buffer(eb, 4096 - 128, 4096 + 128, 256);
768		ret = verify_eb_and_memory(eb, memory, "cross page non-overlapping memcpy 2");
769		if (ret < 0)
770			goto out;
771
772		memmove(memory + 4096 - 128, memory + 4096 - 64, 256);
773		memmove_extent_buffer(eb, 4096 - 128, 4096 - 64, 256);
774		ret = verify_eb_and_memory(eb, memory, "cross page overlapping memcpy 1");
775		if (ret < 0)
776			goto out;
777
778		memmove(memory + 4096 - 64, memory + 4096 - 128, 256);
779		memmove_extent_buffer(eb, 4096 - 64, 4096 - 128, 256);
780		ret = verify_eb_and_memory(eb, memory, "cross page overlapping memcpy 2");
781		if (ret < 0)
782			goto out;
783	}
784out:
785	free_extent_buffer(eb);
786	kvfree(memory);
787	btrfs_free_dummy_fs_info(fs_info);
788	return ret;
789}
790
791int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
792{
793	int ret;
794
795	test_msg("running extent I/O tests");
796
797	ret = test_find_delalloc(sectorsize);
798	if (ret)
799		goto out;
800
801	ret = test_find_first_clear_extent_bit();
802	if (ret)
803		goto out;
804
805	ret = test_eb_bitmaps(sectorsize, nodesize);
806	if (ret)
807		goto out;
808
809	ret = test_eb_mem_ops(sectorsize, nodesize);
810out:
 
811	return ret;
812}
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Copyright (C) 2013 Fusion IO.  All rights reserved.
  4 */
  5
  6#include <linux/pagemap.h>
 
  7#include <linux/sched.h>
  8#include <linux/slab.h>
  9#include <linux/sizes.h>
 10#include "btrfs-tests.h"
 11#include "../ctree.h"
 12#include "../extent_io.h"
 
 13
 14#define PROCESS_UNLOCK		(1 << 0)
 15#define PROCESS_RELEASE		(1 << 1)
 16#define PROCESS_TEST_LOCKED	(1 << 2)
 17
 18static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
 19				       unsigned long flags)
 20{
 21	int ret;
 22	struct page *pages[16];
 23	unsigned long index = start >> PAGE_SHIFT;
 24	unsigned long end_index = end >> PAGE_SHIFT;
 25	unsigned long nr_pages = end_index - index + 1;
 26	int i;
 27	int count = 0;
 28	int loops = 0;
 29
 30	while (nr_pages > 0) {
 31		ret = find_get_pages_contig(inode->i_mapping, index,
 32				     min_t(unsigned long, nr_pages,
 33				     ARRAY_SIZE(pages)), pages);
 
 34		for (i = 0; i < ret; i++) {
 
 
 35			if (flags & PROCESS_TEST_LOCKED &&
 36			    !PageLocked(pages[i]))
 37				count++;
 38			if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
 39				unlock_page(pages[i]);
 40			put_page(pages[i]);
 41			if (flags & PROCESS_RELEASE)
 42				put_page(pages[i]);
 43		}
 44		nr_pages -= ret;
 45		index += ret;
 46		cond_resched();
 47		loops++;
 48		if (loops > 100000) {
 49			printk(KERN_ERR "stuck in a loop, start %Lu, end %Lu, nr_pages %lu, ret %d\n", start, end, nr_pages, ret);
 
 
 50			break;
 51		}
 52	}
 
 53	return count;
 54}
 55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 56static int test_find_delalloc(u32 sectorsize)
 57{
 58	struct inode *inode;
 59	struct extent_io_tree tmp;
 60	struct page *page;
 61	struct page *locked_page = NULL;
 62	unsigned long index = 0;
 63	u64 total_dirty = SZ_256M;
 64	u64 max_bytes = SZ_128M;
 
 65	u64 start, end, test_start;
 66	u64 found;
 67	int ret = -EINVAL;
 68
 69	test_msg("Running find delalloc tests\n");
 70
 71	inode = btrfs_new_test_inode();
 72	if (!inode) {
 73		test_msg("Failed to allocate test inode\n");
 74		return -ENOMEM;
 75	}
 
 76
 77	extent_io_tree_init(&tmp, inode);
 
 
 
 
 78
 79	/*
 80	 * First go through and create and mark all of our pages dirty, we pin
 81	 * everything to make sure our pages don't get evicted and screw up our
 82	 * test.
 83	 */
 84	for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
 85		page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
 86		if (!page) {
 87			test_msg("Failed to allocate test page\n");
 88			ret = -ENOMEM;
 89			goto out;
 90		}
 91		SetPageDirty(page);
 92		if (index) {
 93			unlock_page(page);
 94		} else {
 95			get_page(page);
 96			locked_page = page;
 97		}
 98	}
 99
100	/* Test this scenario
101	 * |--- delalloc ---|
102	 * |---  search  ---|
103	 */
104	set_extent_delalloc(&tmp, 0, sectorsize - 1, 0, NULL);
105	start = 0;
106	end = 0;
107	found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
108					 &end, max_bytes);
109	if (!found) {
110		test_msg("Should have found at least one delalloc\n");
111		goto out_bits;
112	}
113	if (start != 0 || end != (sectorsize - 1)) {
114		test_msg("Expected start 0 end %u, got start %llu end %llu\n",
115			sectorsize - 1, start, end);
116		goto out_bits;
117	}
118	unlock_extent(&tmp, start, end);
119	unlock_page(locked_page);
120	put_page(locked_page);
121
122	/*
123	 * Test this scenario
124	 *
125	 * |--- delalloc ---|
126	 *           |--- search ---|
127	 */
128	test_start = SZ_64M;
129	locked_page = find_lock_page(inode->i_mapping,
130				     test_start >> PAGE_SHIFT);
131	if (!locked_page) {
132		test_msg("Couldn't find the locked page\n");
133		goto out_bits;
134	}
135	set_extent_delalloc(&tmp, sectorsize, max_bytes - 1, 0, NULL);
136	start = test_start;
137	end = 0;
138	found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
139					 &end, max_bytes);
140	if (!found) {
141		test_msg("Couldn't find delalloc in our range\n");
142		goto out_bits;
143	}
144	if (start != test_start || end != max_bytes - 1) {
145		test_msg("Expected start %Lu end %Lu, got start %Lu, end "
146			 "%Lu\n", test_start, max_bytes - 1, start, end);
147		goto out_bits;
148	}
149	if (process_page_range(inode, start, end,
150			       PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
151		test_msg("There were unlocked pages in the range\n");
152		goto out_bits;
153	}
154	unlock_extent(&tmp, start, end);
155	/* locked_page was unlocked above */
156	put_page(locked_page);
157
158	/*
159	 * Test this scenario
160	 * |--- delalloc ---|
161	 *                    |--- search ---|
162	 */
163	test_start = max_bytes + sectorsize;
164	locked_page = find_lock_page(inode->i_mapping, test_start >>
165				     PAGE_SHIFT);
166	if (!locked_page) {
167		test_msg("Couldn't find the locked page\n");
168		goto out_bits;
169	}
170	start = test_start;
171	end = 0;
172	found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
173					 &end, max_bytes);
174	if (found) {
175		test_msg("Found range when we shouldn't have\n");
176		goto out_bits;
177	}
178	if (end != (u64)-1) {
179		test_msg("Did not return the proper end offset\n");
180		goto out_bits;
181	}
182
183	/*
184	 * Test this scenario
185	 * [------- delalloc -------|
186	 * [max_bytes]|-- search--|
187	 *
188	 * We are re-using our test_start from above since it works out well.
189	 */
190	set_extent_delalloc(&tmp, max_bytes, total_dirty - 1, 0, NULL);
191	start = test_start;
192	end = 0;
193	found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
194					 &end, max_bytes);
195	if (!found) {
196		test_msg("Didn't find our range\n");
197		goto out_bits;
198	}
199	if (start != test_start || end != total_dirty - 1) {
200		test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
201			 test_start, total_dirty - 1, start, end);
202		goto out_bits;
203	}
204	if (process_page_range(inode, start, end,
205			       PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
206		test_msg("Pages in range were not all locked\n");
207		goto out_bits;
208	}
209	unlock_extent(&tmp, start, end);
210
211	/*
212	 * Now to test where we run into a page that is no longer dirty in the
213	 * range we want to find.
214	 */
215	page = find_get_page(inode->i_mapping,
216			     (max_bytes + SZ_1M) >> PAGE_SHIFT);
217	if (!page) {
218		test_msg("Couldn't find our page\n");
219		goto out_bits;
220	}
221	ClearPageDirty(page);
222	put_page(page);
223
224	/* We unlocked it in the previous test */
225	lock_page(locked_page);
226	start = test_start;
227	end = 0;
228	/*
229	 * Currently if we fail to find dirty pages in the delalloc range we
230	 * will adjust max_bytes down to PAGE_SIZE and then re-search.  If
231	 * this changes at any point in the future we will need to fix this
232	 * tests expected behavior.
233	 */
234	found = find_lock_delalloc_range(inode, &tmp, locked_page, &start,
235					 &end, max_bytes);
236	if (!found) {
237		test_msg("Didn't find our range\n");
238		goto out_bits;
239	}
240	if (start != test_start && end != test_start + PAGE_SIZE - 1) {
241		test_msg("Expected start %Lu end %Lu, got start %Lu end %Lu\n",
242			 test_start, test_start + PAGE_SIZE - 1, start,
243			 end);
244		goto out_bits;
245	}
246	if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
247			       PROCESS_UNLOCK)) {
248		test_msg("Pages in range were not all locked\n");
249		goto out_bits;
250	}
251	ret = 0;
252out_bits:
253	clear_extent_bits(&tmp, 0, total_dirty - 1, (unsigned)-1);
 
 
254out:
255	if (locked_page)
256		put_page(locked_page);
257	process_page_range(inode, 0, total_dirty - 1,
258			   PROCESS_UNLOCK | PROCESS_RELEASE);
259	iput(inode);
260	return ret;
261}
262
263static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
264			   unsigned long len)
265{
266	unsigned long i;
267
268	for (i = 0; i < len * BITS_PER_BYTE; i++) {
269		int bit, bit1;
270
271		bit = !!test_bit(i, bitmap);
272		bit1 = !!extent_buffer_test_bit(eb, 0, i);
273		if (bit1 != bit) {
274			test_msg("Bits do not match\n");
 
 
 
 
 
 
 
 
275			return -EINVAL;
276		}
277
278		bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
279						i % BITS_PER_BYTE);
280		if (bit1 != bit) {
281			test_msg("Offset bits do not match\n");
 
 
 
 
 
 
 
 
 
282			return -EINVAL;
283		}
284	}
285	return 0;
286}
287
288static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
289			     unsigned long len)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
290{
291	unsigned long i, j;
 
292	u32 x;
293	int ret;
294
295	memset(bitmap, 0, len);
296	memzero_extent_buffer(eb, 0, len);
297	if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
298		test_msg("Bitmap was not zeroed\n");
299		return -EINVAL;
300	}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
301
302	bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
303	extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
304	ret = check_eb_bitmap(bitmap, eb, len);
305	if (ret) {
306		test_msg("Setting all bits failed\n");
307		return ret;
308	}
309
310	bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
311	extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
312	ret = check_eb_bitmap(bitmap, eb, len);
313	if (ret) {
314		test_msg("Clearing all bits failed\n");
315		return ret;
316	}
317
318	/* Straddling pages test */
319	if (len > PAGE_SIZE) {
320		bitmap_set(bitmap,
321			(PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
322			sizeof(long) * BITS_PER_BYTE);
323		extent_buffer_bitmap_set(eb, PAGE_SIZE - sizeof(long) / 2, 0,
324					sizeof(long) * BITS_PER_BYTE);
325		ret = check_eb_bitmap(bitmap, eb, len);
326		if (ret) {
327			test_msg("Setting straddling pages failed\n");
 
328			return ret;
329		}
330
331		bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
332		bitmap_clear(bitmap,
333			(PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
334			sizeof(long) * BITS_PER_BYTE);
335		extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
336		extent_buffer_bitmap_clear(eb, PAGE_SIZE - sizeof(long) / 2, 0,
337					sizeof(long) * BITS_PER_BYTE);
338		ret = check_eb_bitmap(bitmap, eb, len);
339		if (ret) {
340			test_msg("Clearing straddling pages failed\n");
341			return ret;
342		}
343	}
344
345	/*
346	 * Generate a wonky pseudo-random bit pattern for the sake of not using
347	 * something repetitive that could miss some hypothetical off-by-n bug.
348	 */
349	x = 0;
350	bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
351	extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
352	for (i = 0; i < len * BITS_PER_BYTE / 32; i++) {
 
 
 
353		x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
354		for (j = 0; j < 32; j++) {
355			if (x & (1U << j)) {
356				bitmap_set(bitmap, i * 32 + j, 1);
357				extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
358			}
359		}
360	}
361
362	ret = check_eb_bitmap(bitmap, eb, len);
363	if (ret) {
364		test_msg("Random bit pattern failed\n");
365		return ret;
366	}
367
368	return 0;
369}
370
371static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
372{
373	struct btrfs_fs_info *fs_info;
374	unsigned long len;
375	unsigned long *bitmap;
376	struct extent_buffer *eb;
377	int ret;
378
379	test_msg("Running extent buffer bitmap tests\n");
380
381	/*
382	 * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
383	 * BTRFS_MAX_METADATA_BLOCKSIZE.
384	 */
385	len = (sectorsize < BTRFS_MAX_METADATA_BLOCKSIZE)
386		? sectorsize * 4 : sectorsize;
387
388	fs_info = btrfs_alloc_dummy_fs_info(len, len);
389
390	bitmap = kmalloc(len, GFP_KERNEL);
391	if (!bitmap) {
392		test_msg("Couldn't allocate test bitmap\n");
393		return -ENOMEM;
 
394	}
395
396	eb = __alloc_dummy_extent_buffer(fs_info, 0, len);
397	if (!eb) {
398		test_msg("Couldn't allocate test extent buffer\n");
399		kfree(bitmap);
400		return -ENOMEM;
401	}
402
403	ret = __test_eb_bitmaps(bitmap, eb, len);
404	if (ret)
405		goto out;
406
407	/* Do it over again with an extent buffer which isn't page-aligned. */
408	free_extent_buffer(eb);
409	eb = __alloc_dummy_extent_buffer(NULL, nodesize / 2, len);
 
 
 
 
 
410	if (!eb) {
411		test_msg("Couldn't allocate test extent buffer\n");
412		kfree(bitmap);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
413		return -ENOMEM;
414	}
415
416	ret = __test_eb_bitmaps(bitmap, eb, len);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
417out:
418	free_extent_buffer(eb);
419	kfree(bitmap);
 
420	return ret;
421}
422
423int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
424{
425	int ret;
426
427	test_msg("Running extent I/O tests\n");
428
429	ret = test_find_delalloc(sectorsize);
430	if (ret)
431		goto out;
432
 
 
 
 
433	ret = test_eb_bitmaps(sectorsize, nodesize);
 
 
 
 
434out:
435	test_msg("Extent I/O tests finished\n");
436	return ret;
437}