Whamcloud - gitweb
Test to exercise htree rename bug.
[fs/lustre-release.git] / lustre / tests / rename_many.c
1 #define PATH_LENGTH 35
2 #include <math.h>
3 #include <signal.h>
4 #include <unistd.h>
5 #include <time.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <stdio.h>
9 #include <fcntl.h>
10 #include <stdlib.h>
11
12 int count = 1000;
13
14 struct names {
15         char from[PATH_LENGTH];
16         char to[PATH_LENGTH];
17 } *names;
18
19 int loops = 0;
20 int stop = 0;
21 long start;
22
23 int creat_errors = 0;
24 int rename_errors = 0;
25 int unlink_errors = 0;
26
27 void handler(int sig) {
28         static long last_time;
29         long now = time(0);
30
31         signal(SIGINT, handler);
32         signal(SIGALRM, handler);
33         printf("%6ld sec %14d iterations errors %d/%d/%d - "
34                "Use SIGQUIT (^\\) to kill\n", now - start, loops,
35                creat_errors, rename_errors, unlink_errors);
36
37         if (sig == SIGQUIT)
38                 stop = 1;
39         else if (sig == SIGALRM)
40                 alarm(60);
41         else if (sig == SIGINT) {
42                 if (last_time - now < 2)
43                         stop = 1;
44                 last_time = now;
45         }
46 }
47
48 int main(int argc, char *argv[])
49 {
50         int i;
51         unsigned long n;
52         int h1, h2;
53
54         names = malloc(sizeof(struct names) * count);
55         if (names == NULL) {
56                 perror("calloc");
57                 return(1);
58         }
59
60         h2 = sprintf(names[0].from, "%x", count); /* just to figure length */
61         h1 = (PATH_LENGTH-h2-2)/4;
62
63         n = 1 << h1 * 4;
64
65         printf("h1 = %d, h2 = %d n = %lu\n", h1, h2, n);
66
67         start = time(0);
68         srand(start);
69
70         signal(SIGQUIT, handler);
71         signal(SIGINT, handler);
72         signal(SIGALRM, handler);
73         alarm(60);
74
75         while (!stop) {
76                 int j,k,l,m;
77
78                 if (mkdir("tmp", S_IRWXU) == -1) {
79                         perror("mkdir");
80                         return(1);
81                 }
82                 if (chdir("tmp") == -1) {
83                         perror("chdir");
84                         return(1);
85                 }
86
87                 for (i = 0; i < count ; i++) {
88                         j = random() & (n - 1);
89                         k = random() & (n - 1);
90                         l = random() & (n - 1);
91                         m = random() & (n - 1);
92                         sprintf(names[i].from, "%0*x%0*x%0*x%0*x0%0*x",
93                                 h1, j, h1, k, h1, l, h1, m, h2, i);
94                         sprintf(names[i].to, "%0*x%0*x%0*x%0*x1%0*x",
95                                 h1, j, h1, k, h1, l, h1, m, h2, i);
96
97                 }
98
99                 for (i = 0; i < count; i++) {
100                         int fd;
101                         if ((fd = creat(names[i].from, S_IRUSR|S_IWUSR)) == -1){
102                                 char msg[100];
103                                 sprintf(msg, "creat %s", names[i].from);
104                                 perror(msg);
105                                 creat_errors++;
106                         }
107                         if (close(fd) == -1) {
108                                 perror("close");
109                                 return(1);
110                         }
111                 }
112
113                 for (i = 0; i < count; i++) {
114                         if (rename(names[i].from, names[i].to) == -1) {
115                                 char msg[100];
116                                 sprintf(msg, "rename %s to %s",
117                                         names[i].from, names[i].to);
118                                 perror(msg);
119                                 rename_errors++;
120                         }
121                 }
122
123                 for (i = 0; i < count; i++) {
124                         if (unlink(names[i].to) == -1) {
125                                 char msg[100];
126                                 sprintf(msg, "unlink %s", names[i].to);
127                                 perror(msg);
128                                 unlink_errors++;
129                         }
130                 }
131
132                 if (chdir("..") == -1) {
133                         perror("chdir");
134                         return(1);
135                 }
136
137                 if (rmdir("tmp") == -1) {
138                         if (chdir("tmp") == -1) {
139                                 perror("chdir");
140                                 return(1);
141                         }
142                         for (i = 0; i < count; i++) {
143                                 if (unlink(names[i].from) != -1) {
144                                         fprintf(stderr, "Unexpected file %s\n",
145                                                 names[i].to);
146                                         unlink_errors++;
147                                 }
148                         }
149                         if (chdir("..") == -1) {
150                                 perror("chdir");
151                                 return(1);
152                         }
153                         if (rmdir("tmp") == -1) {
154                                 perror("rmdir");
155                                 return(1);
156                         }
157                 }
158
159                 loops++;
160         }
161
162         return(0);
163 }