summaryrefslogtreecommitdiff
path: root/src/unwritten_sync.c
blob: 45ac7df9709be0b5d98c0b0be68c3f0c8809d247 (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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <malloc.h>
#include <unistd.h>
#include <xfs/xfs.h>

/* test thanks to judith@sgi.com */

#define IO_SIZE	1048576

void
print_getbmapx(
	const char	*pathname,
	int		fd,
	int64_t		start,
	int64_t		limit);

int
main(int argc, char *argv[])
{
	int i;
	int fd;
	char *buf;
	struct dioattr dio;
	xfs_flock64_t flock;
	off_t offset;
	char	*file;
	int	loops;
	char	*dio_env;

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

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

	while (loops-- > 0) {
		sleep(1);
		fd = open(file, O_RDWR|O_CREAT|O_DIRECT, 0666);
		if (fd < 0) {
			perror("open");
			exit(1);
		}
		if (xfsctl(file, fd, XFS_IOC_DIOINFO, &dio) < 0) {
			perror("dioinfo");
			exit(1);
		}

		dio_env = getenv("XFS_DIO_MIN");
		if (dio_env)
			dio.d_mem = dio.d_miniosz = atoi(dio_env);

		if ((dio.d_miniosz > IO_SIZE) || (dio.d_maxiosz < IO_SIZE)) {
			fprintf(stderr, "Test won't work - iosize out of range"
				" (dio.d_miniosz=%d, dio.d_maxiosz=%d)\n",
				dio.d_miniosz, dio.d_maxiosz);

			exit(1);
		}
		buf = (char *)memalign(dio.d_mem , IO_SIZE);
		if (buf == NULL) {
			fprintf(stderr,"Can't get memory\n");
			exit(1);
		}
		memset(buf,'Z',IO_SIZE);
		offset = 0;

		flock.l_whence = 0;
		flock.l_start= 0;
		flock.l_len = IO_SIZE*21;
		if (xfsctl(file, fd, XFS_IOC_RESVSP64, &flock) < 0) {
			perror("xfsctl ");
			exit(1);
		}
		for (i = 0; i < 21; i++) {
			if (pwrite(fd, buf, IO_SIZE, offset) != IO_SIZE) {
				perror("pwrite");
				exit(1);
			}
			offset += IO_SIZE;
		}

		print_getbmapx(file, fd, 0, 0);

		if (ftruncate(fd, 0)) {
			perror("ftruncate");
			exit(1);
		}

		print_getbmapx(file, fd, 0, 0);
		close(fd);
	}
	exit(0);
}

void
print_getbmapx(
const   char	*pathname,
	int	fd,
	int64_t	start,
	int64_t	limit)
{
	struct getbmapx bmapx[50];
	int array_size = sizeof(bmapx) / sizeof(bmapx[0]);
	int x;
	int foundone = 0;
	int foundany = 0;

again:
	foundone = 0;
	memset(bmapx, '\0', sizeof(bmapx));

	bmapx[0].bmv_offset = start;
	bmapx[0].bmv_length = -1; /* limit - start; */
	bmapx[0].bmv_count = array_size;
	bmapx[0].bmv_entries = 0;       /* no entries filled in yet */

	bmapx[0].bmv_iflags = BMV_IF_PREALLOC;

	x = array_size;
	for (;;) {
		if (x > bmapx[0].bmv_entries) {
			if (x != array_size) {
				break;  /* end of file */
			}
			if (xfsctl(pathname, fd, XFS_IOC_GETBMAPX, bmapx) < 0) {
				fprintf(stderr, "XFS_IOC_GETBMAPX failed\n");
				exit(1);
			}
			if (bmapx[0].bmv_entries == 0) {
				break;
			}
			x = 1;  /* back at first extent in buffer */
		}
		if (bmapx[x].bmv_oflags & 1) {
			fprintf(stderr, "FOUND ONE %lld %lld %x\n",
				(long long) bmapx[x].bmv_offset,
				(long long) bmapx[x].bmv_length,
				bmapx[x].bmv_oflags);
			foundone = 1;
			foundany = 1;
		}
		x++;
	}
	if (foundone) {
		sleep(1);
		fprintf(stderr,"Repeat\n");
		goto again;
	}
	if (foundany) {
		exit(1);
	}
}