^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2013 Fusion IO. All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/pagemap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "btrfs-tests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include "../ctree.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include "../extent_io.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include "../btrfs_inode.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #define PROCESS_UNLOCK (1 << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define PROCESS_RELEASE (1 << 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define PROCESS_TEST_LOCKED (1 << 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) static noinline int process_page_range(struct inode *inode, u64 start, u64 end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) unsigned long flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) struct page *pages[16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long index = start >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) unsigned long end_index = end >> PAGE_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned long nr_pages = end_index - index + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int loops = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) while (nr_pages > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) ret = find_get_pages_contig(inode->i_mapping, index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) min_t(unsigned long, nr_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) ARRAY_SIZE(pages)), pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) for (i = 0; i < ret; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) if (flags & PROCESS_TEST_LOCKED &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) !PageLocked(pages[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) if (flags & PROCESS_UNLOCK && PageLocked(pages[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) unlock_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) if (flags & PROCESS_RELEASE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) put_page(pages[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) nr_pages -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) index += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) loops++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (loops > 100000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) printk(KERN_ERR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) "stuck in a loop, start %llu, end %llu, nr_pages %lu, ret %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) start, end, nr_pages, ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static int test_find_delalloc(u32 sectorsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct inode *inode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct extent_io_tree *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct page *locked_page = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) unsigned long index = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) /* In this test we need at least 2 file extents at its maximum size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) u64 max_bytes = BTRFS_MAX_EXTENT_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) u64 total_dirty = 2 * max_bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) u64 start, end, test_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) bool found;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) test_msg("running find delalloc tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) inode = btrfs_new_test_inode();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) if (!inode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) test_std_err(TEST_ALLOC_INODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) tmp = &BTRFS_I(inode)->io_tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Passing NULL as we don't have fs_info but tracepoints are not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) * at this point
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) extent_io_tree_init(NULL, tmp, IO_TREE_SELFTEST, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * First go through and create and mark all of our pages dirty, we pin
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * everything to make sure our pages don't get evicted and screw up our
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * test.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) for (index = 0; index < (total_dirty >> PAGE_SHIFT); index++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) page = find_or_create_page(inode->i_mapping, index, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) test_err("failed to allocate test page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) SetPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) if (index) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) unlock_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) get_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) locked_page = page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) /* Test this scenario
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * |--- delalloc ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) * |--- search ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) set_extent_delalloc(tmp, 0, sectorsize - 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) start = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) found = find_lock_delalloc_range(inode, locked_page, &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) test_err("should have found at least one delalloc");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (start != 0 || end != (sectorsize - 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) test_err("expected start 0 end %u, got start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) sectorsize - 1, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) unlock_extent(tmp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) unlock_page(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) put_page(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * Test this scenario
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * |--- delalloc ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * |--- search ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) test_start = SZ_64M;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) locked_page = find_lock_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) test_start >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (!locked_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) test_err("couldn't find the locked page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) set_extent_delalloc(tmp, sectorsize, max_bytes - 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) start = test_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) found = find_lock_delalloc_range(inode, locked_page, &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) test_err("couldn't find delalloc in our range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (start != test_start || end != max_bytes - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) test_err("expected start %llu end %llu, got start %llu, end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) test_start, max_bytes - 1, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (process_page_range(inode, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) test_err("there were unlocked pages in the range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) unlock_extent(tmp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /* locked_page was unlocked above */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) put_page(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) * Test this scenario
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) * |--- delalloc ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) * |--- search ---|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) test_start = max_bytes + sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) locked_page = find_lock_page(inode->i_mapping, test_start >>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (!locked_page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) test_err("couldn't find the locked page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) start = test_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) found = find_lock_delalloc_range(inode, locked_page, &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) if (found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) test_err("found range when we shouldn't have");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) if (end != (u64)-1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) test_err("did not return the proper end offset");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * Test this scenario
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) * [------- delalloc -------|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) * [max_bytes]|-- search--|
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) * We are re-using our test_start from above since it works out well.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) set_extent_delalloc(tmp, max_bytes, total_dirty - 1, 0, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) start = test_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) found = find_lock_delalloc_range(inode, locked_page, &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) test_err("didn't find our range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (start != test_start || end != total_dirty - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) test_err("expected start %llu end %llu, got start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) test_start, total_dirty - 1, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (process_page_range(inode, start, end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) PROCESS_TEST_LOCKED | PROCESS_UNLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) test_err("pages in range were not all locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) unlock_extent(tmp, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * Now to test where we run into a page that is no longer dirty in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) * range we want to find.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) page = find_get_page(inode->i_mapping,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) (max_bytes + SZ_1M) >> PAGE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) test_err("couldn't find our page");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) ClearPageDirty(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) put_page(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* We unlocked it in the previous test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) lock_page(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) start = test_start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) end = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) * Currently if we fail to find dirty pages in the delalloc range we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) * will adjust max_bytes down to PAGE_SIZE and then re-search. If
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) * this changes at any point in the future we will need to fix this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) * tests expected behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) found = find_lock_delalloc_range(inode, locked_page, &start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) &end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if (!found) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) test_err("didn't find our range");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) if (start != test_start && end != test_start + PAGE_SIZE - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) test_err("expected start %llu end %llu, got start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) test_start, test_start + PAGE_SIZE - 1, start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (process_page_range(inode, start, end, PROCESS_TEST_LOCKED |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) PROCESS_UNLOCK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) test_err("pages in range were not all locked");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) goto out_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) out_bits:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) clear_extent_bits(tmp, 0, total_dirty - 1, (unsigned)-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (locked_page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) put_page(locked_page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) process_page_range(inode, 0, total_dirty - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) PROCESS_UNLOCK | PROCESS_RELEASE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) iput(inode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) static int check_eb_bitmap(unsigned long *bitmap, struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) unsigned long i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) for (i = 0; i < len * BITS_PER_BYTE; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) int bit, bit1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) bit = !!test_bit(i, bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) bit1 = !!extent_buffer_test_bit(eb, 0, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) if (bit1 != bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) test_err("bits do not match");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) bit1 = !!extent_buffer_test_bit(eb, i / BITS_PER_BYTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) i % BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if (bit1 != bit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) test_err("offset bits do not match");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) static int __test_eb_bitmaps(unsigned long *bitmap, struct extent_buffer *eb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) unsigned long i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) u32 x;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) memset(bitmap, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) memzero_extent_buffer(eb, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) if (memcmp_extent_buffer(eb, bitmap, 0, len) != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) test_err("bitmap was not zeroed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) ret = check_eb_bitmap(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) test_err("setting all bits failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) ret = check_eb_bitmap(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) test_err("clearing all bits failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) /* Straddling pages test */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) if (len > PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) bitmap_set(bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) sizeof(long) * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) extent_buffer_bitmap_set(eb, PAGE_SIZE - sizeof(long) / 2, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) sizeof(long) * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) ret = check_eb_bitmap(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) test_err("setting straddling pages failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) bitmap_set(bitmap, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) bitmap_clear(bitmap,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) (PAGE_SIZE - sizeof(long) / 2) * BITS_PER_BYTE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) sizeof(long) * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) extent_buffer_bitmap_set(eb, 0, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) extent_buffer_bitmap_clear(eb, PAGE_SIZE - sizeof(long) / 2, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) sizeof(long) * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) ret = check_eb_bitmap(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) test_err("clearing straddling pages failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * Generate a wonky pseudo-random bit pattern for the sake of not using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * something repetitive that could miss some hypothetical off-by-n bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) x = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) bitmap_clear(bitmap, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) extent_buffer_bitmap_clear(eb, 0, 0, len * BITS_PER_BYTE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) for (i = 0; i < len * BITS_PER_BYTE / 32; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) x = (0x19660dULL * (u64)x + 0x3c6ef35fULL) & 0xffffffffU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) for (j = 0; j < 32; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) if (x & (1U << j)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) bitmap_set(bitmap, i * 32 + j, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) extent_buffer_bitmap_set(eb, 0, i * 32 + j, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) ret = check_eb_bitmap(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) test_err("random bit pattern failed");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int test_eb_bitmaps(u32 sectorsize, u32 nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct btrfs_fs_info *fs_info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) unsigned long len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) unsigned long *bitmap = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) struct extent_buffer *eb = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) test_msg("running extent buffer bitmap tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) * In ppc64, sectorsize can be 64K, thus 4 * 64K will be larger than
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) * BTRFS_MAX_METADATA_BLOCKSIZE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) len = (sectorsize < BTRFS_MAX_METADATA_BLOCKSIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) ? sectorsize * 4 : sectorsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) fs_info = btrfs_alloc_dummy_fs_info(len, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (!fs_info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) test_std_err(TEST_ALLOC_FS_INFO);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) bitmap = kmalloc(len, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) if (!bitmap) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) test_err("couldn't allocate test bitmap");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) eb = __alloc_dummy_extent_buffer(fs_info, 0, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) if (!eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) test_std_err(TEST_ALLOC_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) ret = __test_eb_bitmaps(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) /* Do it over again with an extent buffer which isn't page-aligned. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) eb = __alloc_dummy_extent_buffer(fs_info, nodesize / 2, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (!eb) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) test_std_err(TEST_ALLOC_ROOT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) ret = __test_eb_bitmaps(bitmap, eb, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) free_extent_buffer(eb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) kfree(bitmap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) btrfs_free_dummy_fs_info(fs_info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) static int test_find_first_clear_extent_bit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) struct extent_io_tree tree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) u64 start, end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) int ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) test_msg("running find_first_clear_extent_bit test");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) extent_io_tree_init(NULL, &tree, IO_TREE_SELFTEST, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) /* Test correct handling of empty tree */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) find_first_clear_extent_bit(&tree, 0, &start, &end, CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) if (start != 0 || end != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) "error getting a range from completely empty tree: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) * Set 1M-4M alloc/discard and 32M-64M thus leaving a hole between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) * 4M-32M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) set_extent_bits(&tree, SZ_1M, SZ_4M - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) find_first_clear_extent_bit(&tree, SZ_512K, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (start != 0 || end != SZ_1M - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) test_err("error finding beginning range: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) /* Now add 32M-64M so that we have a hole between 4M-32M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) set_extent_bits(&tree, SZ_32M, SZ_64M - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) * Request first hole starting at 12M, we should get 4M-32M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) find_first_clear_extent_bit(&tree, 12 * SZ_1M, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) if (start != SZ_4M || end != SZ_32M - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) test_err("error finding trimmed range: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) * Search in the middle of allocated range, should get the next one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) * available, which happens to be unallocated -> 4M-32M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) find_first_clear_extent_bit(&tree, SZ_2M, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) if (start != SZ_4M || end != SZ_32M - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) test_err("error finding next unalloc range: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) * Set 64M-72M with CHUNK_ALLOC flag, then search for CHUNK_TRIMMED flag
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) * being unset in this range, we should get the entry in range 64M-72M
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) set_extent_bits(&tree, SZ_64M, SZ_64M + SZ_8M - 1, CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) find_first_clear_extent_bit(&tree, SZ_64M + SZ_1M, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) test_err("error finding exact range: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) find_first_clear_extent_bit(&tree, SZ_64M - SZ_8M, &start, &end,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) * Search in the middle of set range whose immediate neighbour doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) * have the bits set so it must be returned
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) if (start != SZ_64M || end != SZ_64M + SZ_8M - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) test_err("error finding next alloc range: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * Search beyond any known range, shall return after last known range
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * and end should be -1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) find_first_clear_extent_bit(&tree, -1, &start, &end, CHUNK_TRIMMED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) if (start != SZ_64M + SZ_8M || end != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) test_err(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) "error handling beyond end of range search: start %llu end %llu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) start, end);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) clear_extent_bits(&tree, 0, (u64)-1, CHUNK_TRIMMED | CHUNK_ALLOCATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) int btrfs_test_extent_io(u32 sectorsize, u32 nodesize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) test_msg("running extent I/O tests");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) ret = test_find_delalloc(sectorsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) ret = test_find_first_clear_extent_bit();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) ret = test_eb_bitmaps(sectorsize, nodesize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }