Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[fs/lustre-release.git] / lustre / liblustre / replay_single.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Lustre Light user test program
5  *
6  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #define _BSD_SOURCE
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <getopt.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <fcntl.h>
34 #include <sys/queue.h>
35 #include <signal.h>
36
37 #include <sysio.h>
38 #include <mount.h>
39
40 #include "test_common.h"
41
42
43
44 static char mds_server[1024] = {0,};
45 static char barrier_script[1024] = {0,};
46 static char failover_script[1024] = {0,};
47 static char barrier_cmd[1024] = {0,};
48 static char failover_cmd[1024] = {0,};
49
50 static void replay_barrier()
51 {
52         int rc;
53
54         if ((rc = system(barrier_cmd))) {
55                 printf("excute barrier error: %d\n", rc);
56                 exit(rc);
57         }
58 }
59
60 static void mds_failover()
61 {
62         int rc;
63
64         if ((rc = system(failover_cmd))) {
65                 printf("excute failover error: %d\n", rc);
66                 exit(rc);
67         }
68 }
69
70
71 #define ENTRY(str)                                                      \
72         do {                                                            \
73                 char buf[100];                                          \
74                 int len;                                                \
75                 sprintf(buf, "===== START: %s ", (str));                \
76                 len = strlen(buf);                                      \
77                 if (len < 79) {                                         \
78                         memset(buf+len, '=', 100-len);                  \
79                         buf[79] = '\n';                                 \
80                         buf[80] = 0;                                    \
81                 }                                                       \
82                 printf("%s", buf);                                      \
83         } while (0)
84
85 #define LEAVE()                                                         \
86         do {                                                            \
87                 printf("----- END TEST successfully ---");              \
88                 printf("-----------------------------");                \
89                 printf("-------------------\n");                        \
90         } while (0)
91
92 void t0()
93 {
94         ENTRY("empty replay");
95         replay_barrier();
96         mds_failover();
97         LEAVE();
98 }
99
100 void t1()
101 {
102         char *path="/mnt/lustre/f1";
103         ENTRY("simple create");
104
105         replay_barrier();
106         t_create(path);
107         mds_failover();
108         t_check_stat(path, NULL);
109         t_unlink(path);
110         LEAVE();
111 }
112
113 void t2a()
114 {
115         char *path="/mnt/lustre/f2a";
116         ENTRY("touch");
117
118         replay_barrier();
119         t_touch(path);
120         mds_failover();
121         t_check_stat(path, NULL);
122         t_unlink(path);
123         LEAVE();
124 }
125
126 void t2b()
127 {
128         char *path="/mnt/lustre/f2b";
129         ENTRY("mcreate+touch");
130
131         t_create(path);
132         replay_barrier();
133         t_touch(path);
134         mds_failover();
135         t_check_stat(path, NULL);
136         t_unlink(path);
137         LEAVE();
138 }
139
140
141 void n_create_delete(int nfiles)
142 {
143         char *base="/mnt/lustre/f3_";
144         char path[100];
145         char str[100];
146         int i;
147
148         replay_barrier();
149         for (i = 0; i < nfiles; i++) {
150                 sprintf(path, "%s%d\n", base, i);
151                 sprintf(str, "TEST#%d CONTENT\n", i);
152                 t_echo_create(path, str);
153         }
154         mds_failover();
155         for (i = 0; i < nfiles; i++) {
156                 sprintf(path, "%s%d\n", base, i);
157                 sprintf(str, "TEST#%d CONTENT\n", i);
158                 t_grep(path, str);
159         }
160         replay_barrier();
161         for (i = 0; i < nfiles; i++) {
162                 sprintf(path, "%s%d\n", base, i);
163                 t_unlink(path);
164         }
165         mds_failover();
166         for (i = 0; i < nfiles; i++) {
167                 sprintf(path, "%s%d\n", base, i);
168                 t_check_stat_fail(path);
169         }
170         LEAVE();
171 }
172
173 void t3a()
174 {
175         ENTRY("10 create/delete");
176         n_create_delete(10);
177         LEAVE();
178 }
179
180 void t3b()
181 {
182         ENTRY("30 create/delete(>1'st block precreated)");
183         n_create_delete(30);
184         LEAVE();
185 }
186
187 void t4()
188 {
189         char *dir="/mnt/lustre/d4";
190         char *path="/mnt/lustre/d4/f1";
191         ENTRY("mkdir + contained create");
192
193         replay_barrier();
194         t_mkdir(dir);
195         t_create(path);
196         mds_failover();
197         t_check_stat(dir, NULL);
198         t_check_stat(path, NULL);
199         sleep(2); /* wait for log process thread */
200
201         replay_barrier();
202         t_unlink(path);
203         t_rmdir(dir);
204         mds_failover();
205         t_check_stat_fail(dir);
206         t_check_stat_fail(path);
207         LEAVE();
208 }
209
210 void t5()
211 {
212         char *dir="/mnt/lustre/d5";
213         char *path="/mnt/lustre/d5/f1";
214         ENTRY("mkdir |X| contained create");
215
216         t_mkdir(dir);
217         replay_barrier();
218         t_create(path);
219         mds_failover();
220         t_check_stat(dir, NULL);
221         t_check_stat(path, NULL);
222         t_unlink(path);
223         t_rmdir(dir);
224         LEAVE();
225 }
226
227 void t6()
228 {
229         char *path="/mnt/lustre/f6";
230         int fd;
231         ENTRY("open |X| close");
232
233         replay_barrier();
234         t_create(path);
235         fd = t_open(path);
236         sleep(1);
237         mds_failover();
238         t_check_stat(path, NULL);
239         t_close(fd);
240         t_unlink(path);
241         LEAVE();
242 }
243
244 void t7()
245 {
246         char *path="/mnt/lustre/f7";
247         char *path2="/mnt/lustre/f7-2";
248         ENTRY("create |X| rename unlink");
249
250         t_create(path);
251         replay_barrier();
252         t_rename(path, path2);
253         mds_failover();
254         t_check_stat_fail(path);
255         t_check_stat(path2, NULL);
256         t_unlink(path2);
257 }
258
259 void t8()
260 {
261         char *path="/mnt/lustre/f8";
262         char *path2="/mnt/lustre/f8-2";
263         ENTRY("create open write rename |X| create-old-name read");
264
265         t_create(path);
266         t_echo_create(path, "old");
267         t_rename(path, path2);
268         replay_barrier();
269         t_echo_create(path, "new");
270         mds_failover();
271         t_grep(path, "new");
272         t_grep(path2, "old");
273         t_unlink(path);
274         t_unlink(path2);
275 }
276
277 void t9()
278 {
279         char *path="/mnt/lustre/f9";
280         char *path2="/mnt/lustre/f9-2";
281         ENTRY("|X| open(O_CREAT), unlink, touch new, unlink new");
282
283         replay_barrier();
284         t_create(path);
285         t_unlink(path);
286         t_create(path2);
287         mds_failover();
288         t_check_stat_fail(path);
289         t_check_stat(path2, NULL);
290         t_unlink(path2);
291 }
292
293 void t10()
294 {
295         char *path="/mnt/lustre/f10";
296         char *path2="/mnt/lustre/f10-2";
297         ENTRY("|X| mcreate, open write, rename");
298
299         replay_barrier();
300         t_create(path);
301         t_echo_create(path, "old");
302         t_rename(path, path2);
303         t_grep(path2, "old");
304         mds_failover();
305         t_grep(path2, "old");
306         t_unlink(path2);
307 }
308
309 extern int portal_debug;
310 extern int portal_subsystem_debug;
311
312 extern void __liblustre_setup_(void);
313 extern void __liblustre_cleanup_(void);
314
315 void usage(const char *cmd)
316 {
317         printf("Usage: \t%s --target mdsnid:/mdsname/profile -s mds_hostname "
318                 "-b \"barrier cmd\" -f \"failover cmd\"\n", cmd);
319         printf("       \t%s --dumpfile dumpfile -s mds_hostname -b \"barrier cmd\" "
320                 "-f \"failover cmd\"\n", cmd);
321         exit(-1);
322 }
323
324 void test_ssh()
325 {
326         char cmd[1024];
327
328         sprintf(cmd, "ssh %s cat /dev/null", mds_server);
329         if (system(cmd)) {
330                 printf("ssh can't access server node: %s\n", mds_server);
331                 exit(-1);
332         }
333 }
334
335 int main(int argc, char * const argv[])
336 {
337         int opt_index, c;
338         static struct option long_opts[] = {
339                 {"target", 1, 0, 0},
340                 {"dumpfile", 1, 0, 0},
341                 {0, 0, 0, 0}
342         };
343
344         if (argc < 4)
345                 usage(argv[0]);
346
347         while ((c = getopt_long(argc, argv, "s:b:f:", long_opts, &opt_index)) != -1) {
348                 switch (c) {
349                 case 0: {
350                         if (!optarg[0])
351                                 usage(argv[0]);
352
353                         if (!strcmp(long_opts[opt_index].name, "target")) {
354                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
355                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
356                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
357                         } else
358                                 usage(argv[0]);
359                         break;
360                 }
361                 case 's':
362                         strcpy(mds_server, optarg);
363                         break;
364                 case 'b':
365                         strcpy(barrier_script, optarg);
366                         break;
367                 case 'f':
368                         strcpy(failover_script, optarg);
369                         break;
370                 default:
371                         usage(argv[0]);
372                 }
373         }
374
375         if (optind != argc)
376                 usage(argv[0]);
377         if (!strlen(mds_server) || !strlen(barrier_script) ||
378             !strlen(failover_script))
379                 usage(argv[0]);
380
381         test_ssh();
382
383         /* prepare remote command */
384         sprintf(barrier_cmd, "ssh %s \"%s\"", mds_server, barrier_script);
385         sprintf(failover_cmd, "ssh %s \"%s\"", mds_server, failover_script);
386
387         __liblustre_setup_();
388
389         t0();
390         t1();
391         t2a();
392         t2b();
393         t3a();
394         t3b();
395         t4();
396         t5();
397         t6();
398         t7();
399         t8();
400         t9();
401         t10();
402
403         printf("liblustre is about shutdown\n");
404         __liblustre_cleanup_();
405
406         printf("complete successfully\n");
407         return 0;
408 }