Skip to content

Instantly share code, notes, and snippets.

@ddawson
Last active November 29, 2023 04:39
Show Gist options
  • Save ddawson/22cfd4cac32916f6f1dcc86f90eed21a to your computer and use it in GitHub Desktop.
Save ddawson/22cfd4cac32916f6f1dcc86f90eed21a to your computer and use it in GitHub Desktop.
Tester for failing lseek(..., SEEK_HOLE) on ext4 with inline_data
#define _GNU_SOURCE
#include <stdlib.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
int create_file (const char *fn) {
return open(fn, O_CREAT|O_RDWR, 0644);
}
void append_file (int fd, size_t nb) {
void *buf = malloc(nb);
memset(buf, 42, nb);
ssize_t nw = write(fd, buf, nb);
free(buf);
}
void test_seek_hole (int fd) {
off_t hole_start = lseek(fd, 0, SEEK_HOLE);
if (hole_start < 0) {
printf("seek_hole failed with errno %d\n", errno);
} else {
printf("seek_hole got %ld\n", (long)hole_start);
}
}
int main (int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
return 1;
}
const char *fn = argv[1];
puts("Synced single-write test...");
for (int i = 0; i < 10; i++) {
int fd = create_file(fn);
append_file(fd, 192);
fdatasync(fd);
test_seek_hole(fd);
close(fd);
unlink(fn);
}
puts("Unsynced single-write test...");
for (int i = 0; i < 10; i++) {
int fd = create_file(fn);
append_file(fd, 192);
test_seek_hole(fd);
close(fd);
unlink(fn);
}
puts("Synced multiple-write test...");
for (int i = 0; i < 10; i++) {
int fd = create_file(fn);
append_file(fd, 64);
append_file(fd, 128);
fdatasync(fd);
test_seek_hole(fd);
close(fd);
unlink(fn);
}
puts("Unsynced multiple-write test...");
for (int i = 0; i < 10; i++) {
int fd = create_file(fn);
append_file(fd, 64);
append_file(fd, 128);
test_seek_hole(fd);
close(fd);
unlink(fn);
}
puts("Unsynced multiple-write test with delays...");
for (int i = 0; i < 10; i++) {
puts("Create file, wait 5 secs...");
int fd = create_file(fn);
append_file(fd, 64);
append_file(fd, 128);
sleep(5);
puts("Check...");
test_seek_hole(fd);
close(fd);
puts("Delete...");
unlink(fn);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment