Whamcloud - gitweb
LU-116 fix mmp test 9 and test 10
[fs/lustre-release.git] / lustre / tests / small_write.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 <stdio.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #define GOTO(label, rc)   do { rc; goto label; } while (0)
47
48 int main (int argc, char **argv) {
49         int fd, i, rc = 0;
50         unsigned long bytes, lbytes;
51         struct stat st;
52         char *str, *str2, *readbuf;
53
54         if (argc != 3) {
55                 fprintf(stderr, "usage: %s <filename> <bytes>\n", argv[0]);
56                 GOTO(out, rc = 1);
57         }
58
59         bytes = strtoul(argv[2], NULL, 10);
60         if (!bytes) {
61                 printf("No bytes!\n");
62                 GOTO(out, rc = 2);
63         }
64         if (bytes % 2) {
65                 printf("Need an even number of bytes!\n");
66                 GOTO(out, rc = 3);
67         }
68         lbytes = 3*bytes/2;
69
70         str = malloc(bytes+1);
71         if (!str) {
72                 printf("No enough memory for %lu bytes.\n", bytes);
73                 GOTO(out, rc = 4);
74         }
75         str2 = malloc(lbytes+1);
76         if (!str2) {
77                 printf("No enough memory for %lu bytes.\n", lbytes);
78                 GOTO(out_str, rc = 5);
79         }
80         readbuf = malloc(bytes*2);
81         if (!readbuf) {
82                 printf("No enough memory for %lu bytes.\n", bytes*2);
83                 GOTO(out_str2, rc = 6);
84         }
85
86         for(i=0; i < bytes; i++)
87                 str[i] = 'a' + (i % 26);
88         str[i] = '\0';
89
90         memcpy(str2, str, bytes);
91         memcpy(str2+(bytes/2), str, bytes);
92         str2[lbytes] = '\0';
93
94         if (bytes < 320)
95                 printf("First  String: %s\nSecond String: %s\n", str, str2);
96
97         fd = open(argv[1], O_CREAT|O_RDWR|O_TRUNC, 0700);
98         if (fd == -1) {
99                 printf("Could not open file %s.\n", argv[1]);
100                 GOTO(out_readbuf, rc = 7);
101         }
102
103         rc = write(fd, str, bytes);
104         if (rc != bytes) {
105                 printf("Write failed!\n");
106                 GOTO(out_fd, rc = 8);
107         }
108
109         sleep(1);
110         rc = fstat(fd, &st);
111         if (rc < 0 || st.st_size != bytes) {
112                 printf("bad file %lu size first write %lu != %lu: rc %d\n",
113                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
114                        bytes, rc);
115                 GOTO(out_fd, rc = 9);
116         }
117
118         rc = lseek(fd, bytes / 2, SEEK_SET);
119         if (rc != bytes / 2) {
120                 printf("Seek failed!\n");
121                 GOTO(out_fd, rc = 10);
122         }
123
124         rc = write(fd, str, bytes);
125         if (rc != bytes) {
126                 printf("Write failed!\n");
127                 GOTO(out_fd, rc = 11);
128         }
129
130         rc = fstat(fd, &st);
131         if (rc < 0 || st.st_size != bytes + bytes / 2) {
132                 printf("bad file %lu size second write %lu != %lu: rc %d\n",
133                        (unsigned long)st.st_ino, (unsigned long)st.st_size,
134                        bytes, rc);
135                 GOTO(out_fd, rc = 12);
136         }
137
138         rc = lseek(fd, 0, SEEK_SET);
139         if (rc != 0) {
140                 printf("Seek failed!\n");
141                 GOTO(out_fd, rc = 13);
142         }
143
144         rc = read(fd, readbuf, bytes * 2);
145         if (rc != lbytes) {
146                 printf("Read %d bytes instead of %lu.\n", rc, lbytes);
147                 if (rc == -1)
148                         perror("");
149                 else
150                         printf("%s\n%s\n", readbuf, str2);
151                 rc = fstat(fd, &st);
152                 if (rc < 0 || st.st_size != bytes + bytes / 2) {
153                         printf("bad file size after read %lu != %lu: rc %d\n",
154                                (unsigned long)st.st_size, bytes + bytes / 2,
155                                rc);
156                         GOTO(out_fd, rc = 14);
157                 }
158
159                 GOTO(out_fd, rc = 15);
160         }
161         rc = 0;
162
163         if (bytes < 320)
164                 printf("%s\n%s\n", readbuf, str2);
165         if (strcmp(readbuf, str2)) {
166                 printf("No match!\n");
167                 GOTO(out_fd, rc = 16);
168         }
169
170         printf("Pass!\n");
171 out_fd:
172         close(fd);
173 out_readbuf:
174         free(readbuf);
175 out_str2:
176         free(str2);
177 out_str:
178         free(str);
179 out:
180         return rc;
181 }