Debian Codegraph @

tests/fallocate_align.c

raw symbols SCIP
1 /*
2 * libhugetlbfs - Easy use of Linux hugepages
3 * Copyright (C) 20015 Mike Kravetz, Oracle Corporation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public License
7 * as published by the Free Software Foundation; either version 2.1 of
8 * the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #define _GNU_SOURCE
20
21 #include <linux/falloc.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <unistd.h>
25 #include <signal.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <linux/falloc.h>
29
30 #include <hugetlbfs.h>
31
32 #include "hugetests.h"
33
34 #define P "fallocate-align"
35 #define DESC \
36 "* Test alignment of fallocate arguments. fallocate will take *\n"\
37 "* non-huge page aligned offsets and addresses. However, *\n"\
38 "* operations are only performed on huge pages. This is different *\n"\
39 "* that than fallocate behavior in "normal" filesystems. *"
40
41 int main(int argc, char *argv[])
42 {
43 long hpage_size;
44 int fd;
45 int err;
46 unsigned long free_before, free_after;
47
48 test_init(argc, argv);
49
50 hpage_size = check_hugepagesize();
51
52 fd = hugetlbfs_unlinked_fd();
53 if (fd < 0)
54 FAIL("hugetlbfs_unlinked_fd()");
55
56 free_before = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
57
58 /*
59 * First preallocate file with with just 1 byte. Allocation sizes
60 * are rounded up, so we should get an entire huge page.
61 */
62 err = fallocate(fd, 0, 0, 1);
63 if (err) {
64 if (errno == EOPNOTSUPP)
65 IRRELEVANT();
66 if (err)
67 FAIL("fallocate(): %s", strerror(errno));
68 }
69
70 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
71 if (free_before - free_after != 1)
72 FAIL("fallocate 1 byte did not preallocate entire huge page\n");
73
74 /*
75 * Now punch a hole with just 1 byte. On hole punch, sizes are
76 * rounded down. So, this operation should not create a hole.
77 */
78 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
79 0, 1);
80 if (err)
81 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
82
83 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
84 if (free_after == free_before)
85 FAIL("fallocate hole punch 1 byte free'ed a huge page\n");
86
87 /*
88 * Now punch a hole with of 2 * hpage_size - 1 byte. This size
89 * should be rounded down to a single huge page and the hole created.
90 */
91 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
92 0, (2 * hpage_size) - 1);
93 if (err)
94 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
95
96 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
97 if (free_after != free_before)
98 FAIL("fallocate hole punch 2 * hpage_size - 1 byte did not free huge page\n");
99
100 /*
101 * Perform a preallocate operation with offset 1 and size of
102 * hpage_size. The offset should be rounded down and the
103 * size rounded up to preallocate two huge pages.
104 */
105 err = fallocate(fd, 0, 1, hpage_size);
106 if (err)
107 FAIL("fallocate(): %s", strerror(errno));
108
109 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
110 if (free_before - free_after != 2)
111 FAIL("fallocate 1 byte offset, huge page size did not preallocate two huge pages\n");
112
113 /*
114 * The hole punch code will only delete 'whole' huge pags that are
115 * in the specified range. The offset is rounded up, and (offset
116 * + size) is rounded down to determine the huge pages to be deleted.
117 * In this case, after rounding the range is (hpage_size, hpage_size).
118 * So, no pages should be deleted.
119 */
120 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
121 1, hpage_size);
122 if (err)
123 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
124
125 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
126 if (free_before - free_after != 2)
127 FAIL("fallocate hole punch 1 byte offset, huge page size incorrectly deleted a huge page\n");
128
129 /*
130 * To delete both huge pages, the range passed to hole punch must
131 * overlap the allocated pages
132 */
133 err = fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
134 0, 2 * hpage_size);
135 if (err)
136 FAIL("fallocate(FALLOC_FL_PUNCH_HOLE): %s", strerror(errno));
137
138 free_after = get_huge_page_counter(hpage_size, HUGEPAGES_FREE);
139 if (free_after != free_before)
140 FAIL("fallocate hole punch did not delete two huge pages\n");
141
142 PASS();
143 }