Whamcloud - gitweb
b=22632 update mptsas driver to PH16-4.18.20.04
[fs/lustre-release.git] / lustre / liblustre / tests / replay_ost_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_ost_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
60
61 static char mds_server[1024] = {0,};
62 static char barrier_script[1024] = {0,};
63 static char failover_script[1024] = {0,};
64 static char barrier_cmd[1024] = {0,};
65 static char failover_cmd[1024] = {0,};
66
67 static void replay_barrier()
68 {
69         int rc;
70
71         if ((rc = system(barrier_cmd))) {
72                 printf("excute barrier error: %d\n", rc);
73                 exit(rc);
74         }
75 }
76
77 static void mds_failover()
78 {
79         int rc;
80
81         if ((rc = system(failover_cmd))) {
82                 printf("excute failover error: %d\n", rc);
83                 exit(rc);
84         }
85 }
86
87
88 #define ENTRY(str)                                                      \
89         do {                                                            \
90                 char buf[100];                                          \
91                 int len;                                                \
92                 sprintf(buf, "===== START: %s ", (str));                \
93                 len = strlen(buf);                                      \
94                 if (len < 79) {                                         \
95                         memset(buf+len, '=', 100-len);                  \
96                         buf[79] = '\n';                                 \
97                         buf[80] = 0;                                    \
98                 }                                                       \
99                 printf("%s", buf);                                      \
100         } while (0)
101
102 #define LEAVE()                                                         \
103         do {                                                            \
104                 printf("----- END TEST successfully ---");              \
105                 printf("-----------------------------");                \
106                 printf("-------------------\n");                        \
107         } while (0)
108
109 void t0()
110 {
111         const int bufsize = 4096;
112         char *path = "/mnt/lustre/rp_ost_t0_file";
113         char buf[bufsize];
114         int fd, i, j, rc;
115         ENTRY("open-failover-write-verification (no ping involved)");
116
117         printf("create/open file...\n");
118         t_touch(path);
119         fd = t_open(path);
120         printf("OST failover...\n");
121         replay_barrier();
122         mds_failover();
123
124         printf("write file...\n");
125         for (i = 0; i < 20; i++) {
126                 memset(buf, i, bufsize);
127                 if ((rc = write(fd, buf, bufsize)) != bufsize) {
128                         perror("write error after failover");
129                         printf("i = %d, rc = %d\n", i, rc);
130                         exit(-1);
131                 }
132         }
133
134         /* verify */
135         printf("read & verify...\n");
136         lseek(fd, 0, SEEK_SET);
137         for (i = 0; i < 20; i++) {
138                 memset(buf, -1, bufsize);
139                 if ((rc = read(fd, buf, bufsize)) != bufsize) {
140                         perror("read error rc");
141                         printf("i = %d, rc = %d\n", i, rc);
142                         exit(-1);
143                 }
144                 for (j = 0; j < bufsize; j++) {
145                         if (buf[j] != i) {
146                                 printf("verify error!\n");
147                                 exit(-1);
148                         }
149                 }
150         }
151         t_close(fd);
152         t_unlink(path);
153         LEAVE();
154 }
155
156 void t1()
157 {
158         const int bufsize = 4096;
159         char *path = "/mnt/lustre/rp_ost_t1_file";
160         char buf[bufsize];
161         int fd, i, j;
162         ENTRY("open-write-close-open-failover-read (no ping involved)");
163
164         printf("create/open file...\n");
165         t_touch(path);
166         fd = t_open(path);
167         printf("write file...\n");
168         for (i = 0; i < 20; i++) {
169                 memset(buf, i, bufsize);
170                 if (write(fd, buf, bufsize) != bufsize) {
171                         perror("write error");
172                         exit(-1);
173                 }
174         }
175         printf("close/reopen...\n");
176         t_close(fd);
177         fd = t_open(path);
178         lseek(fd, 0, SEEK_SET);
179
180         printf("OST failover...\n");
181         replay_barrier();
182         mds_failover();
183
184         printf("read & verify...\n");
185         for (i = 0; i < 20; i++) {
186                 memset(buf, -1, bufsize);
187                 if (read(fd, buf, bufsize) != bufsize) {
188                         perror("read error after failover");
189                         exit(-1);
190                 }
191                 for (j = 0; j < bufsize; j++) {
192                         if (buf[j] != i) {
193                                 printf("verify error after failover\n");
194                                 exit(-1);
195                         }
196                 }
197         }
198
199         t_close(fd);
200         t_unlink(path);
201         LEAVE();
202 }
203
204 void t2()
205 {
206         char *path = "/mnt/lustre/rp_ost_t2_file";
207         char *str = "xxxxjoiwlsdf98lsjdfsjfoajflsjfajfoaidfojaj08eorje;";
208         ENTRY("empty replay");
209
210         replay_barrier();
211         mds_failover();
212
213         t_echo_create(path, str);
214         t_grep(path, str);
215         t_unlink(path);
216 }
217
218 void t3()
219 {
220         char *path = "/mnt/lustre/rp_ost_t3_file";
221         char *str = "xxxxjoiwlsdf98lsjdfsjfoajflsjfajfoaidfojaj08eorje;";
222         ENTRY("touch");
223
224         printf("touch to create a file\n");
225         t_echo_create(path, str);
226         replay_barrier();
227         mds_failover();
228
229         printf("read & verify\n");
230         t_grep(path, str);
231         t_unlink(path);
232         /* XXX have problem without this, seems server side problem XXX */
233         sleep(5);
234 }
235
236 void t4()
237 {
238         char *path = "/mnt/lustre/rp_ost_t4_file";
239         char namebuf[1024];
240         char str[1024];
241         int count = 10, i;
242         ENTRY("|X| 10 open(CREAT)s (ping involved)");
243
244         printf("create %d files\n", count);
245         for (i = 0; i < count; i++) {
246                 sprintf(namebuf, "%s%02d", path, i);
247                 sprintf(str, "%s-%08d-%08x-AAAAA", "content", i, i);
248                 t_echo_create(namebuf, str);
249         }
250         replay_barrier();
251         mds_failover();
252
253         printf("read & verify\n");
254         for (i = 0; i < count; i++) {
255                 sprintf(namebuf, "%s%02d", path, i);
256                 sprintf(str, "%s-%08d-%08x-AAAAA", "content", i, i);
257                 t_grep(namebuf, str);
258                 t_unlink(namebuf);
259         }
260 }
261
262 extern int libcfs_debug;
263 extern int libcfs_subsystem_debug;
264
265 extern void __liblustre_setup_(void);
266 extern void __liblustre_cleanup_(void);
267
268 void usage(const char *cmd)
269 {
270         printf("Usage: \t%s --target mdsnid:/mdsname/profile -s ost_hostname "
271                 "-b \"barrier cmd\" -f \"failover cmd\"\n", cmd);
272         printf("       \t%s --dumpfile dumpfile -s ost_hostname -b \"barrier cmd\" "
273                 "-f \"failover cmd\"\n", cmd);
274         exit(-1);
275 }
276
277 void test_ssh()
278 {
279         char cmd[1024];
280
281         sprintf(cmd, "ssh %s cat /dev/null", mds_server);
282         if (system(cmd)) {
283                 printf("ssh can't access server node: %s\n", mds_server);
284                 exit(-1);
285         }
286 }
287
288 int main(int argc, char * const argv[])
289 {
290         int opt_index, c;
291         static struct option long_opts[] = {
292                 {"target", 1, 0, 0},
293                 {"dumpfile", 1, 0, 0},
294                 {0, 0, 0, 0}
295         };
296
297         if (argc < 4)
298                 usage(argv[0]);
299
300         while ((c = getopt_long(argc, argv, "s:b:f:", long_opts, &opt_index)) != -1) {
301                 switch (c) {
302                 case 0: {
303                         if (!optarg[0])
304                                 usage(argv[0]);
305
306                         if (!strcmp(long_opts[opt_index].name, "target")) {
307                                 setenv(ENV_LUSTRE_MNTTGT, optarg, 1);
308                         } else if (!strcmp(long_opts[opt_index].name, "dumpfile")) {
309                                 setenv(ENV_LUSTRE_DUMPFILE, optarg, 1);
310                         } else
311                                 usage(argv[0]);
312                         break;
313                 }
314                 case 's':
315                         strcpy(mds_server, optarg);
316                         break;
317                 case 'b':
318                         strcpy(barrier_script, optarg);
319                         break;
320                 case 'f':
321                         strcpy(failover_script, optarg);
322                         break;
323                 default:
324                         usage(argv[0]);
325                 }
326         }
327
328         if (optind != argc)
329                 usage(argv[0]);
330         if (!strlen(mds_server) || !strlen(barrier_script) ||
331             !strlen(failover_script))
332                 usage(argv[0]);
333
334         test_ssh();
335
336         /* prepare remote command */
337         sprintf(barrier_cmd, "ssh %s \"%s\"", mds_server, barrier_script);
338         sprintf(failover_cmd, "ssh %s \"%s\"", mds_server, failover_script);
339
340         setenv(ENV_LUSTRE_TIMEOUT, "5", 1);
341
342         __liblustre_setup_();
343
344         t0();
345         t1();
346         t2();
347         t3();
348         t4();
349
350         printf("liblustre is about shutdown\n");
351         __liblustre_cleanup_();
352
353         printf("complete successfully\n");
354         return 0;
355 }