Whamcloud - gitweb
LU-3321 clio: revert LU-2622 for removing global env list
[fs/lustre-release.git] / lustre / liblustre / tests / replay_single.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) 2003, 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  * lustre/liblustre/tests/replay_single.c
35  *
36  * Lustre Light user test program
37  */
38
39 #define _BSD_SOURCE
40
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <getopt.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <sys/queue.h>
50 #include <signal.h>
51
52 #include <sysio.h>
53 #include <mount.h>
54
55 #include "test_common.h"
56
57 #define MAX_STRING_SIZE 2048
58
59 static char mds_server[MAX_STRING_SIZE] = {0,};
60 static char barrier_script[MAX_STRING_SIZE] = {0,};
61 static char failover_script[MAX_STRING_SIZE] = {0,};
62 static char barrier_cmd[MAX_STRING_SIZE] = {0,};
63 static char failover_cmd[MAX_STRING_SIZE] = {0,};
64 static char ssh_cmd[MAX_STRING_SIZE] = {0,};
65
66 static void replay_barrier()
67 {
68         int rc;
69
70         if ((rc = system(barrier_cmd))) {
71                 printf("excute barrier error: %d\n", rc);
72                 exit(rc);
73         }
74 }
75
76 static void mds_failover()
77 {
78         int rc;
79
80         if ((rc = system(failover_cmd))) {
81                 printf("excute failover error: %d\n", rc);
82                 exit(rc);
83         }
84 }
85
86
87 #define ENTRY(str)                                                      \
88         do {                                                            \
89                 char buf[100];                                          \
90                 int len;                                                \
91                 sprintf(buf, "===== START: %s ", (str));                \
92                 len = strlen(buf);                                      \
93                 if (len < 79) {                                         \
94                         memset(buf+len, '=', 100-len);                  \
95                         buf[79] = '\n';                                 \
96                         buf[80] = 0;                                    \
97                 }                                                       \
98                 printf("%s", buf);                                      \
99         } while (0)
100
101 #define LEAVE()                                                         \
102         do {                                                            \
103                 printf("----- END TEST successfully ---");              \
104                 printf("-----------------------------");                \
105                 printf("-------------------\n");                        \
106         } while (0)
107
108 void t0()
109 {
110         char *path="/mnt/lustre/f0";
111         ENTRY("empty replay");
112
113         replay_barrier();
114         mds_failover();
115         t_check_stat_fail(path);
116         LEAVE();
117 }
118
119 void t1()
120 {
121         char *path="/mnt/lustre/f1";
122         ENTRY("simple create");
123
124         replay_barrier();
125         t_create(path);
126         mds_failover();
127         t_check_stat(path, NULL);
128         t_unlink(path);
129         LEAVE();
130 }
131
132 void t2a()
133 {
134         char *path="/mnt/lustre/f2a";
135         ENTRY("touch");
136
137         replay_barrier();
138         t_touch(path);
139         mds_failover();
140         t_check_stat(path, NULL);
141         t_unlink(path);
142         LEAVE();
143 }
144
145 void t2b()
146 {
147         char *path="/mnt/lustre/f2b";
148         ENTRY("mcreate+touch");
149
150         t_create(path);
151         replay_barrier();
152         t_touch(path);
153         mds_failover();
154         t_check_stat(path, NULL);
155         t_unlink(path);
156         LEAVE();
157 }
158
159
160 void n_create_delete(int nfiles)
161 {
162         char *base="/mnt/lustre/f3_";
163         char path[100];
164         char str[100];
165         int i;
166
167         replay_barrier();
168         for (i = 0; i < nfiles; i++) {
169                 sprintf(path, "%s%d\n", base, i);
170                 sprintf(str, "TEST#%d CONTENT\n", i);
171                 t_echo_create(path, str);
172         }
173         mds_failover();
174         for (i = 0; i < nfiles; i++) {
175                 sprintf(path, "%s%d\n", base, i);
176                 sprintf(str, "TEST#%d CONTENT\n", i);
177                 t_grep(path, str);
178         }
179         replay_barrier();
180         for (i = 0; i < nfiles; i++) {
181                 sprintf(path, "%s%d\n", base, i);
182                 t_unlink(path);
183         }
184         mds_failover();
185         for (i = 0; i < nfiles; i++) {
186                 sprintf(path, "%s%d\n", base, i);
187                 t_check_stat_fail(path);
188         }
189         LEAVE();
190 }
191
192 void t3a()
193 {
194         ENTRY("10 create/delete");
195         n_create_delete(10);
196         LEAVE();
197 }
198
199 void t3b()
200 {
201         ENTRY("30 create/delete(>1'st block precreated)");
202         n_create_delete(30);
203         LEAVE();
204 }
205
206 void t4()
207 {
208         char *dir="/mnt/lustre/d4";
209         char *path="/mnt/lustre/d4/f1";
210         ENTRY("mkdir + contained create");
211
212         replay_barrier();
213         t_mkdir(dir);
214         t_create(path);
215         mds_failover();
216         t_check_stat(dir, NULL);
217         t_check_stat(path, NULL);
218         sleep(2); /* wait for log process thread */
219
220         replay_barrier();
221         t_unlink(path);
222         t_rmdir(dir);
223         mds_failover();
224         t_check_stat_fail(dir);
225         t_check_stat_fail(path);
226         LEAVE();
227 }
228
229 void t5()
230 {
231         char *dir="/mnt/lustre/d5";
232         char *path="/mnt/lustre/d5/f1";
233         ENTRY("mkdir |X| contained create");
234
235         t_mkdir(dir);
236         replay_barrier();
237         t_create(path);
238         mds_failover();
239         t_check_stat(dir, NULL);
240         t_check_stat(path, NULL);
241         t_unlink(path);
242         t_rmdir(dir);
243         LEAVE();
244 }
245
246 void t6()
247 {
248         char *path="/mnt/lustre/f6";
249         int fd;
250         ENTRY("open |X| close");
251
252         replay_barrier();
253         t_create(path);
254         fd = t_open(path);
255         sleep(1);
256         mds_failover();
257         t_check_stat(path, NULL);
258         t_close(fd);
259         t_unlink(path);
260         LEAVE();
261 }
262
263 void t7()
264 {
265         char *path="/mnt/lustre/f7";
266         char *path2="/mnt/lustre/f7-2";
267         ENTRY("create |X| rename unlink");
268
269         t_create(path);
270         replay_barrier();
271         t_rename(path, path2);
272         mds_failover();
273         t_check_stat_fail(path);
274         t_check_stat(path2, NULL);
275         t_unlink(path2);
276 }
277
278 void t8()
279 {
280         char *path="/mnt/lustre/f8";
281         char *path2="/mnt/lustre/f8-2";
282         ENTRY("create open write rename |X| create-old-name read");
283
284         t_create(path);
285         t_echo_create(path, "old");
286         t_rename(path, path2);
287         replay_barrier();
288         t_echo_create(path, "new");
289         mds_failover();
290         t_grep(path, "new");
291         t_grep(path2, "old");
292         t_unlink(path);
293         t_unlink(path2);
294 }
295
296 void t9()
297 {
298         char *path="/mnt/lustre/f9";
299         char *path2="/mnt/lustre/f9-2";
300         ENTRY("|X| open(O_CREAT), unlink, touch new, unlink new");
301
302         replay_barrier();
303         t_create(path);
304         t_unlink(path);
305         t_create(path2);
306         mds_failover();
307         t_check_stat_fail(path);
308         t_check_stat(path2, NULL);
309         t_unlink(path2);
310 }
311
312 void t10()
313 {
314         char *path="/mnt/lustre/f10";
315         char *path2="/mnt/lustre/f10-2";
316         ENTRY("|X| mcreate, open write, rename");
317
318         replay_barrier();
319         t_create(path);
320         t_echo_create(path, "old");
321         t_rename(path, path2);
322         t_grep(path2, "old");
323         mds_failover();
324         t_grep(path2, "old");
325         t_unlink(path2);
326 }
327
328 extern int libcfs_debug;
329 extern int libcfs_subsystem_debug;
330
331 extern void __liblustre_setup_(void);
332 extern void __liblustre_cleanup_(void);
333
334 void usage(const char *cmd)
335 {
336         printf("Usage: \t%s --target mdsnid:/mdsname/profile -s mds_hostname "
337                 "-b \"barrier cmd\" -f \"failover cmd\" [--rsh \"rsh_cmd\"]\n", cmd);
338         printf("       \t%s --dumpfile dumpfile -s mds_hostname -b \"barrier cmd\" "
339                 "-f \"failover cmd\" [--rsh \"rsh_cmd\"]\n", cmd);
340         exit(-1);
341 }
342
343 void test_ssh()
344 {
345         char cmd[MAX_STRING_SIZE];
346
347         sprintf(cmd, "%s %s cat /dev/null", ssh_cmd, mds_server);
348         if (system(cmd)) {
349                 printf("Can't access server node: %s using method: %s\n", mds_server, ssh_cmd);
350                 exit(-1);
351         }
352 }
353
354 int main(int argc, char * const argv[])
355 {
356         int opt_index, c;
357         static struct option long_opts[] = {
358                 {"target", 1, 0, 0},
359                 {"dumpfile", 1, 0, 0},
360                 {"ssh", 1, 0, 0},
361                 {0, 0, 0, 0}
362         };
363
364         if (argc < 4 - (getenv(ENV_LUSTRE_MNTTGT)||getenv(ENV_LUSTRE_DUMPFILE)))
365                 usage(argv[0]);
366
367         while ((c = getopt_long(argc, argv, "s:b:f:", long_opts, &opt_index)) != -1) {
368                 switch (c) {
369                 case 0: {
370                         if (!optarg[0])
371                                 usage(argv[0]);
372
373                         if (!strcmp(long_opts[opt_index].name, "target")) {
374                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
375                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
376                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
377                         } else if (!strcmp(long_opts[opt_index].name, "ssh")) {
378                                 safe_strncpy(ssh_cmd, optarg, MAX_STRING_SIZE);
379                         } else
380                                 usage(argv[0]);
381                         break;
382                 }
383                 case 's':
384                         safe_strncpy(mds_server, optarg, MAX_STRING_SIZE);
385                         break;
386                 case 'b':
387                         safe_strncpy(barrier_script, optarg, MAX_STRING_SIZE);
388                         break;
389                 case 'f':
390                         safe_strncpy(failover_script, optarg, MAX_STRING_SIZE);
391                         break;
392                 default:
393                         usage(argv[0]);
394                 }
395         }
396
397         if (optind != argc)
398                 usage(argv[0]);
399         if (!strlen(mds_server) || !strlen(barrier_script) ||
400             !strlen(failover_script))
401                 usage(argv[0]);
402
403         /* default to using ssh */
404         if (!strlen(ssh_cmd)) {
405                 safe_strncpy(ssh_cmd, "ssh", MAX_STRING_SIZE);
406         }
407
408         test_ssh();
409
410         /* prepare remote command */
411         sprintf(barrier_cmd, "%s %s \"%s\"", 
412                 ssh_cmd, mds_server, barrier_script);
413         sprintf(failover_cmd, "%s %s \"%s\"", 
414                 ssh_cmd, mds_server, failover_script);
415
416         setenv(ENV_LUSTRE_TIMEOUT, "10", 1);
417
418         __liblustre_setup_();
419
420         t0();
421         t1();
422         t2a();
423         t2b();
424         t3a();
425         t3b();
426         t4();
427         t5();
428         t6();
429         t7();
430         t8();
431         t9();
432         t10();
433
434         printf("liblustre is about shutdown\n");
435         __liblustre_cleanup_();
436
437         printf("complete successfully\n");
438         return 0;
439 }