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