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