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