Whamcloud - gitweb
LU-10212 test: ESTALE read
[fs/lustre-release.git] / lustre / tests / rename_many.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #define PATH_LENGTH 35
32 #include <math.h>
33 #include <signal.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <stdio.h>
39 #include <fcntl.h>
40 #include <stdlib.h>
41 #include <getopt.h>
42
43 struct names {
44         char from[PATH_LENGTH];
45         char to[PATH_LENGTH];
46 } *names;
47
48 unsigned int loop_count = 500;
49 int file_count = 1000;
50 int seed;
51 int loops;
52 int stop;
53 long start;
54
55 int opt_exit_on_err;
56 int opt_verbose;
57 int opt_create_only;
58 int opt_rename_only;
59 int creat_errors;
60 int rename_errors;
61 int unlink_errors;
62
63 void usage(const char *progname)
64 {
65         fprintf(stderr, "usage: %s [-n numfiles] [-s seed] [-v] [-x] [dir]\n"
66                 "\t-c: only do the create step of first loop\n"
67                 "\t-f: number of files to create/rename/unlink per loop\n"
68                 "\t-n: number of test loops (0 to run forever)\n"
69                 "\t-r: only do the rename step of first loop\n"
70                 "\t-s: starting seed (equals loop number by default)\n"
71                 "\t-v: verbose\n"
72                 "\t-x: don't exit on error\n", progname);
73 }
74
75 void handler(int sig) {
76         static long last_time;
77         long now = time(0);
78
79         signal(SIGINT, handler);
80         signal(SIGALRM, handler);
81         printf("%6lds %8d iterations %d/%d/%d errors",
82                now - start, loops, creat_errors, rename_errors, unlink_errors);
83         if (sig != 0)
84                 printf(" - use SIGQUIT (^\\) or ^C^C to kill\n");
85         else
86                 printf("\n");
87
88         if (sig == SIGQUIT)
89                 stop = 1;
90         else if (sig == SIGINT) {
91                 if (now - last_time < 2)
92                         stop = 1;
93                 last_time = now;
94         }
95         alarm(60);
96 }
97
98 extern char *optarg;
99 extern int optind;
100
101 int main(int argc, char *argv[])
102 {
103         unsigned long n;
104         char msg[100], *end = NULL;
105         int h1, h2;
106         int i, c;
107
108         while ((c = getopt(argc, argv, "cf:n:rs:vx")) != EOF) {
109                 switch(c) {
110                 case 'c':
111                         ++opt_create_only;
112                         break;
113                 case 'f':
114                         i = strtoul(optarg, &end, 0);
115                         if (i && end != NULL && *end == '\0') {
116                                 file_count = i;
117                         } else {
118                                 fprintf(stderr, "bad file count '%s'\n",optarg);
119                                 usage(argv[0]);
120                                 return 1;
121                         }
122                         break;
123                 case 'n':
124                         i = strtoul(optarg, &end, 0);
125                         if (i && end != NULL && *end == '\0') {
126                                 loop_count = i;
127                         } else {
128                                 fprintf(stderr, "bad loop count '%s'\n",optarg);
129                                 usage(argv[0]);
130                                 return 1;
131                         }
132                         break;
133                 case 'r':
134                         ++opt_rename_only;
135                         break;
136                 case 's':
137                         i = strtoul(optarg, &end, 0);
138                         if (end && *end == '\0') {
139                                 seed = i;
140                         } else {
141                                 seed = random();
142                                 fprintf(stderr, "using random seed %u\n", seed);
143                         }
144                         break;
145                 case 'v':
146                         ++opt_verbose;
147                         break;
148                 case 'x':
149                         ++opt_exit_on_err;
150                         break;
151                 default:
152                         usage(argv[0]);
153                         return 1;
154                 }
155         }
156
157         names = malloc(sizeof(struct names) * file_count);
158         if (names == NULL) {
159                 perror("calloc");
160                 return(1);
161         }
162
163         h2 = sprintf(msg, "%x", file_count); /* just to figure length */
164         h1 = (PATH_LENGTH - h2 - 2) / 4;
165
166         n = (1ULL << h1 * 4) - 1;
167
168         //printf("h1 = %d, h2 = %d n = %lu\n", h1, h2, n);
169
170         start = time(0);
171
172         signal(SIGQUIT, handler);
173         signal(SIGINT, handler);
174         signal(SIGALRM, handler);
175         signal(SIGUSR1, handler);
176         alarm(60);
177
178         if (argc > optind + 1) {
179                 fprintf(stderr, "too many extra args %d\n", argc - optind);
180                 usage(argv[0]);
181                 return 1;
182         } else if (argv[optind] != NULL) {
183                 if (chdir(argv[optind]) < 0) {
184                         snprintf(msg, sizeof(msg),
185                                  "chdir '%s'\n", argv[optind]);
186                         perror(msg);
187                         return 2;
188                 }
189         }
190
191         while (!stop && loop_count != 0 && loops < loop_count) {
192                 int j,k,l,m;
193
194                 srand(seed + loops);
195                 if (mkdir("tmp", S_IRWXU) == -1) {
196                         perror("mkdir tmp");
197                         return(1);
198                 }
199                 if (chdir("tmp") == -1) {
200                         perror("chdir tmp");
201                         return(1);
202                 }
203
204                 for (i = 0; i < file_count ; i++) {
205                         j = random() & n;
206                         k = random() & n;
207                         l = random() & n;
208                         m = random() & n;
209                         sprintf(names[i].from, "%0*x%0*x%0*x%0*x0%0*x",
210                                 h1, j, h1, k, h1, l, h1, m, h2, i);
211                         sprintf(names[i].to, "%0*x%0*x%0*x%0*x1%0*x",
212                                 h1, j, h1, k, h1, l, h1, m, h2, i);
213
214                 }
215
216                 for (i = 0; i < file_count; i++) {
217                         if (mknod(names[i].from, S_IFREG | S_IRWXU, 0) == -1) {
218                                 sprintf(msg, "loop %d.%d: creat %s",
219                                         loops, i, names[i].from);
220                                 perror(msg);
221                                 creat_errors++;
222                                 if (!opt_exit_on_err)
223                                         return 4;
224                         }
225                 }
226
227                 if (opt_create_only)
228                         return 0;
229
230                 for (i = 0; i < file_count; i++) {
231                         if (rename(names[i].from, names[i].to) == -1) {
232                                 sprintf(msg, "loop %d.%d: rename %s to %s",
233                                         loops, i, names[i].from, names[i].to);
234                                 perror(msg);
235                                 rename_errors++;
236                                 if (!opt_exit_on_err)
237                                         return 4;
238                         }
239                 }
240
241                 if (opt_rename_only)
242                         return 0;
243
244                 for (i = 0; i < file_count; i++) {
245                         if (unlink(names[i].to) == -1) {
246                                 sprintf(msg, "loop %d.%d: unlink %s",
247                                         loops, i, names[i].to);
248                                 perror(msg);
249                                 unlink_errors++;
250                                 if (!opt_exit_on_err)
251                                         return 4;
252                         }
253                 }
254
255                 if (chdir("..") == -1) {
256                         perror("chdir ..");
257                         return(1);
258                 }
259
260                 if (rmdir("tmp") == -1) {
261                         if (chdir("tmp") == -1) {
262                                 perror("chdir tmp 2");
263                                 return(1);
264                         }
265                         for (i = 0; i < file_count; i++) {
266                                 if (unlink(names[i].from) != -1) {
267                                         fprintf(stderr, "loop %d.%d: "
268                                                 "unexpected file %s\n",
269                                                 loops, i, names[i].to);
270                                         unlink_errors++;
271                                         if (!opt_exit_on_err)
272                                                 return 4;
273                                 }
274                         }
275                         if (chdir("..") == -1) {
276                                 perror("chdir .. 2");
277                                 return(1);
278                         }
279                         if (rmdir("tmp") == -1) {
280                                 perror("rmdir tmp");
281                                 return(1);
282                         }
283                 }
284
285                 loops++;
286                 if (opt_verbose)
287                         handler(0);
288         }
289
290         if (!opt_verbose)
291                 handler(0);
292         return(0);
293 }