| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 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 |
|
| 60 |
|
| 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 |
|
| 76 |
|
| 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 |
|
| 89 |
|
| 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 |
|
| 102 |
|
| 103 |
|
| 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 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 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 |
|
| 131 |
|
| 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 |
} |