summaryrefslogtreecommitdiff
path: root/src/unwritten_mmap.c
blob: aec6a69e9bb1af1256f3b8f1d01475ea6068c5a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <xfs/xfs.h>
#include <sys/mman.h>

/*
 * mmap a preallocated file and write to a set of offsets
 * in it. We'll check to see if the underlying extents are
 * converted correctly on writeback.
 */
int main(int argc, char **argv) {
	unsigned long long o;
	int fd, i;
	struct xfs_flock64 space;
	unsigned char *buf;

	if(argc < 3) {
		fprintf(stderr, "%s <count> <file [file...]>\n", argv[0]);
		exit(1);
	}

	errno = 0;
	o = strtoull(argv[1], NULL, 0);
	if (errno) {
		perror("strtoull");
		exit(errno);
	}

	for(i = 2; i < argc; i++) {
		unlink(argv[i]);
		fd = open(argv[i], O_RDWR|O_CREAT|O_LARGEFILE, 0666);
		if(fd < 0) {
			perror("open");
			exit(2);
		}

		if(ftruncate64(fd, o) < 0) {
			perror("ftruncate64");
			exit(3);
		}

		space.l_whence = SEEK_SET;
		space.l_start = 0;
		space.l_len = o;

		if(ioctl(fd, XFS_IOC_RESVSP64, &space)) {
			perror("ioctl()");
			exit(4);
		}

		buf = mmap(NULL, (int)o, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
		if(buf == MAP_FAILED) {
			perror("mmap()");
			exit(5);
		} else {
			buf[o-1] = 0;
			buf[o/2] = 0;
			buf[0] = 0;
			munmap(buf, o);
		}

		fsync(fd);
		if(close(fd) == -1) {
			perror("close");
			exit(6);
		}
	}
	exit(0);
}