summaryrefslogtreecommitdiff
path: root/src/bulkstat_unlink_test_modified.c
blob: a106749dc12412582019c7114ba5605472decc91 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
 * $Id: bulkstat_unlink_test_modified.c,v 1.1 2007/10/03 16:23:57 mohamedb.longdrop.melbourne.sgi.com Exp $
 * Test bulkstat doesn't returned unlinked inodes.
 * Mark Goodwin <markgw@sgi.com> Fri Jul 20 09:13:57 EST 2007
 *
 * This is a modified version of bulkstat_unlink_test.c to reproduce a specific
 * problem see pv 969192
 */
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <xfs/xfs.h>
#include <unistd.h>
#include <string.h>

int
main(int argc, char *argv[])
{
    int e;
    int fd = 0;
    int i;
    int j;
    int k;
    int nfiles;
    int stride;
    struct stat sbuf;
    ino_t *inodelist;
    __u32 *genlist;
    struct xfs_fsop_bulkreq a;
    struct xfs_bstat *ret;
    int iterations;
    char fname[MAXPATHLEN];
    char *dirname;

    if (argc != 5) {
    	fprintf(stderr, "Usage: %s iterations nfiles stride dir\n", argv[0]);
    	fprintf(stderr, "Create dir with nfiles, unlink each stride'th file, sync, bulkstat\n");
	exit(1);
    }

    iterations = atoi(argv[1]);
    nfiles = atoi(argv[2]);
    stride = atoi(argv[3]);
    dirname = argv[4];
    if (!nfiles || !iterations) {
	fprintf(stderr, "Iterations and nfiles showld be non zero.\n");
    	exit(1);
    }

    inodelist = (ino_t *)malloc(nfiles * sizeof(ino_t));
    genlist = (__u32 *)malloc(nfiles * sizeof(__u32));
    ret = (struct xfs_bstat *)malloc(nfiles * sizeof(struct xfs_bstat));

    for (k=0; k < iterations; k++) {
	xfs_ino_t last_inode = 0;
	int count = 0;
	int testFiles = 0;

	printf("Iteration %d ... \n", k);

	memset(inodelist, 0, nfiles * sizeof(ino_t));
	memset(genlist, 0, nfiles * sizeof(__u32));
	memset(ret, 0, nfiles * sizeof(struct xfs_bstat));
	memset(&a, 0, sizeof(struct xfs_fsop_bulkreq));
	a.lastip = (__u64 *)&last_inode;
	a.icount = nfiles;
	a.ubuffer = ret;
	a.ocount = &count;

	if (mkdir(dirname, 0755) < 0) {
	    perror(dirname);
	    exit(1);
	}

	/* create nfiles and store their inode numbers in inodelist */
	for (i=0; i < nfiles; i++) {
	    sprintf(fname, "%s/file%06d", dirname, i);
	    if ((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) {
		perror(fname);
		exit(1);
	    }
	    write(fd, fname, sizeof(fname));
	    if (fstat(fd, &sbuf) < 0) {
		perror(fname);
		exit(1);
	    }
	    inodelist[i] = sbuf.st_ino;
	    close(fd);
	}
	
	sync();
	
	/* collect bs_gen for the nfiles files */
	if ((fd = open(dirname, O_RDONLY)) < 0) {
	    perror(dirname);
	    exit(1);
	}

	testFiles = 0;
	for (;;) {
	    if ((e = xfsctl(dirname, fd, XFS_IOC_FSBULKSTAT, &a)) < 0) {
		perror("XFS_IOC_FSBULKSTAT1:");
		exit(1);
	    }

	    if (count == 0)
		break;

	    for (i=0; i < count; i++) {
		for (j=0; j < nfiles; j += stride) {
		    if (ret[i].bs_ino == inodelist[j]) {
			genlist[j] = ret[i].bs_gen;
			testFiles++;
		    }
		}
	    }
	}
	close(fd);
	
	printf("testFiles %d ... \n", testFiles);

	/* remove some of the first set of files */
	for (i=0; i < nfiles; i += stride) {
	    sprintf(fname, "%s/file%06d", dirname, i);
	    if (unlink(fname) < 0) {
	    	perror(fname);
		exit(1);
	    }
	}

	/* create a new set of files (replacing the unlinked ones) */
	for (i=0; i < nfiles; i += stride) {
	    sprintf(fname, "%s/file%06d", dirname, i);
	    if ((fd = open(fname, O_RDWR | O_CREAT | O_TRUNC, 0644)) < 0) {
		perror(fname);
		exit(1);
	    }
	    write(fd, fname, sizeof(fname));
	    close(fd);
	}

	sync();
	last_inode = 0; count = 0;

	if ((fd = open(dirname, O_RDONLY)) < 0) {
	    perror(dirname);
	    exit(1);
	}

	for (;;) {
	    if ((e = xfsctl(dirname, fd, XFS_IOC_FSBULKSTAT, &a)) < 0) {
		perror("XFS_IOC_FSBULKSTAT:");
		exit(1);
	    }

	    if (count == 0)
		    break;

	    for (i=0; i < count; i++) {
		for (j=0; j < nfiles; j += stride) {
		    if ((ret[i].bs_ino == inodelist[j]) &&
			(ret[i].bs_gen == genlist[j])) {
			/* oops, the same inode with old gen number */
			printf("Unlinked inode %llu with generation %d "
			       "returned by bulkstat\n",
				(unsigned long long)inodelist[j],
				 genlist[j]);
			exit(1);
		    }
		    if (ret[i].bs_ino == inodelist[j] &&
			ret[i].bs_gen != genlist[j] + 1) {
			/* oops, the new gen number is not 1 bigger than the old */
			printf("Inode with old generation %d, new generation %d\n",
			genlist[j], ret[i].bs_gen);
			exit(1);
		    }
		}
	    }
	}

	close(fd);

	sprintf(fname, "rm -rf %s\n", dirname);
	system(fname);

	sync();
	sleep(2);
	printf("passed\n");
    }

    exit(0);
}