Whamcloud - gitweb
Don't upload to maloo by default
[fs/lustre-release.git] / lustre / tests / iopentest2.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
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <libgen.h>
46 #include <errno.h>
47 #include <sys/wait.h>
48
49 const char *progname;
50 const char usage_fmt[] = "Usage: %s <mountpoint>\n";
51 #define INAME_LEN (PATH_MAX + 1)
52
53 #define CHECK_IT(exp, pstr) \
54 if (!(exp)) { \
55     fprintf(stderr, "%s: at %s:%d: ", progname, __FILE__, __LINE__); \
56     perror((pstr)); \
57     exit(1); \
58 }
59
60 #define CHECK_SNPRINTF(rc, len) \
61     CHECK_IT((rc) > 0 && (rc) <= (len), "snprintf() failed")
62
63 static char *get_iname(char *fname, const char *mtpt)
64 {
65         char *iname;
66         int fd, rc;
67         struct stat buf;
68
69         iname = malloc(INAME_LEN);
70         CHECK_IT(iname, "malloc() failed");
71
72         fd = open(fname, O_CREAT, 0644);
73         if (fd < 0 && errno != EISDIR) {
74                 fprintf(stderr, "%s:%d: open(%s) failed: %s\n", __FILE__,
75                         __LINE__, fname, strerror(errno));
76                 exit(1);
77         }
78
79         if (fd >= 0)
80                 close(fd);
81
82         rc = stat(fname, &buf);
83         if (rc != 0) {
84                 fprintf(stderr, "%s:%d: stat(%s) failed: %s\n", __FILE__,
85                         __LINE__, fname, strerror(errno));
86                 exit(1);
87         }
88
89         rc = snprintf(iname, INAME_LEN,
90                       "%s/__iopen__/%lu", mtpt, (unsigned long)buf.st_ino);
91         CHECK_SNPRINTF(rc, INAME_LEN);
92
93         return iname;
94 }
95
96 int main(int argc, char *argv[])
97 {
98         char *fname, *mtpt;
99         char *fname_iname, *dir;
100         char *dir_iname = NULL, *foo = NULL, *bar = NULL;
101         int rc, fd, i, thread = 0;
102         int pidlist[10];
103
104         progname = basename(argv[0]);
105
106         if (argc != 2) {
107                 fprintf(stderr, usage_fmt, progname);
108                 return 1;
109         }
110  
111         for (i = 1; i <= 10; i++) {
112                 rc = fork();
113                 if (rc < 0) {
114                         fprintf(stderr, "error: %s: #%d - %s\n", argv[0], i,
115                                 strerror(rc = errno));
116                         break;
117                 } else if (rc == 0) {
118                         thread = i;
119                         break;
120                 }
121                 printf("%s: thread #%d (PID %d) started\n", argv[0], i, rc);
122                 pidlist[i-1] = rc;
123                 rc = 0;
124         }
125
126         if (thread != 0) {
127                 mtpt  = argv[1];
128                 fname = malloc(INAME_LEN);
129                 CHECK_IT(fname, "malloc() failed");
130
131                 rc = snprintf(fname, INAME_LEN,
132                               "%s/%d", mtpt, getpid());
133                 CHECK_SNPRINTF(rc, INAME_LEN);
134
135                 rc = mkdir(fname, 0644);
136                 if (rc != 0) {
137                         fprintf(stderr, "%s:%d: mkdir(%s) failed: %s\n",
138                                 __FILE__, __LINE__, fname, strerror(errno));
139                         exit(1);
140                 }
141
142                 fname_iname = get_iname(fname, mtpt);
143
144                 dir = malloc(INAME_LEN);
145                 CHECK_IT(dir, "malloc() failed");
146
147                 rc = snprintf(dir, INAME_LEN, "%s/dir", fname_iname);
148                 CHECK_SNPRINTF(rc, INAME_LEN);
149
150                 foo = malloc(INAME_LEN);
151                 CHECK_IT(foo, "malloc() failed");
152
153                 bar = malloc(INAME_LEN);
154                 CHECK_IT(bar, "malloc() failed");
155
156                 for (i = 0; i < 1000; i++) {
157                         rc = mkdir(dir, 0644);
158                         if (rc != 0) {
159                                 fprintf(stderr, "%s:%d: mkdir(%s) failed: %s\n",
160                                         __FILE__, __LINE__, dir,
161                                         strerror(errno));
162                                 exit(1);
163                         }
164
165                         dir_iname = get_iname(dir, mtpt);
166
167                         rc = snprintf(foo, INAME_LEN, "%s/bar", dir_iname);
168                         CHECK_SNPRINTF(rc, INAME_LEN);
169
170                         rc = snprintf(bar, INAME_LEN, "%s/bar", dir_iname);
171                         CHECK_SNPRINTF(rc, INAME_LEN);
172
173                         fd = open(foo, O_CREAT, 0644);
174                         if (fd < 0) {
175                                 fprintf(stderr, "%s:%d: open(%s) failed: %s\n",
176                                         __FILE__, __LINE__, foo,
177                                         strerror(errno));
178                                 exit(1);
179                         }
180                         rc = close(fd);
181                         if (rc != 0) {
182                                 fprintf(stderr, "%s:%d: close() failed: %s\n",
183                                         __FILE__, __LINE__, strerror(errno));
184                                 exit(1);
185                         }
186
187                         rc = rename(foo, bar);
188                         if (rc != 0) {
189                                 fprintf(stderr, "%s:%d: rename(%s, %s) failed: "
190                                         "%s\n", __FILE__, __LINE__, foo, bar,
191                                         strerror(errno));
192                                 exit(1);
193                         }
194
195                         rc = unlink(bar);
196                         if (rc != 0) {
197                                 fprintf(stderr, "%s:%d: unlink(%s) failed: "
198                                         "%s\n", __FILE__, __LINE__, bar,
199                                         strerror(errno));
200                                 exit(1);
201                         }
202                         rc = rmdir(dir);
203                         if (rc != 0) {
204                                 fprintf(stderr, "%s:%d: rmdir(%s) failed: %s\n",
205                                         __FILE__, __LINE__, dir,
206                                         strerror(errno));
207                                 exit(1);
208                         }
209
210                         free(dir_iname);
211                 }
212         } else {
213                         for ( i=0; i<10; i++)
214                                 waitpid(pidlist[i], NULL, 0);
215         }
216         return 0;
217 }