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