Whamcloud - gitweb
LU-4018 tests: improve racer file_create workload
[fs/lustre-release.git] / lustre / tests / rwv.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, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Use is subject to license terms.
26  *
27  * Copyright (c) 2012, Whamcloud, Inc.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  * Lustre is a trademark of Sun Microsystems, Inc.
32  *
33  * lustre/tests/rwv.c
34  */
35
36 #include <stdio.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <sys/uio.h>
40 #include <sys/mman.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <stdlib.h>
44 #include <unistd.h>
45
46 #include <libcfs/libcfs.h>
47 #include <lustre/lustreapi.h>
48
49 #define ACT_NONE        0
50 #define ACT_READ        1
51 #define ACT_WRITE       2
52 #define ACT_SEEK        4
53 #define ACT_READHOLE    8
54 #define ACT_VERIFY      16
55
56 void usage()
57 {
58         printf("usage: rwv -f filename <-r|-w> [-a] [-z] [-d] [-v]"
59                 "[-s offset] -n iovcnt SIZE1 SIZE2 SIZE3...\n");
60         printf("-a  append IO (O_APPEND)\n");
61         printf("-r  file read (O_RDONLY)\n");
62         printf("-w  file write (O_WRONLY)\n");
63         printf("-s  set the start pos of the read/write test\n");
64         printf("-z  test for read hitting hole\n");
65         printf("-d  create flags (O_LOV_DELAY_CREATE)\n");
66         printf("-v  verify the data content of read\n");
67 }
68
69 int data_verify(struct iovec *iov, int iovcnt, char c)
70 {
71         int i;
72
73         for (i = 0; i < iovcnt; i++) {
74                 size_t count = iov[i].iov_len;
75                 char *s = iov[i].iov_base;
76
77                 for (; count > 0; ++s, count--) {
78                         if (*s != c) {
79                                 printf("Data mismatch %x: %x\n", *s, c);
80                                 return 1;
81                         }
82                 }
83         }
84         return 0;
85 }
86
87 int main(int argc, char** argv)
88 {
89         int c;
90         int fd;
91         int rc = 0;
92         int flags = 0;
93         int iovcnt = 0;
94         int act = ACT_NONE;
95         char pad = 0xba;
96         char *end;
97         char *fname = "FILE";
98         unsigned long len = 0;
99         struct iovec *iov;
100         off64_t offset = 0;
101
102         while ((c = getopt(argc, argv, "f:n:s:rwahvdz")) != -1) {
103                 switch (c) {
104                 case 'f':
105                         fname = optarg;
106                         break;
107                 case 'n':
108                         iovcnt = strtoul(optarg, &end, 0);
109                         if (*end) {
110                                 printf("Bad iov count: %s\n", optarg);
111                                 return 1;
112                         }
113                         if (iovcnt > UIO_MAXIOV || iovcnt <= 0) {
114                                 printf("Wrong iov count\n");
115                                 return 1;
116                         }
117                         break;
118                 case 's':
119                         act |= ACT_SEEK;
120                         offset = strtoull(optarg, &end, 0);
121                         if (*end) {
122                                 printf("Bad seek offset: %s\n", optarg);
123                                 return 1;
124                         }
125                         break;
126                 case 'w':
127                         act |= ACT_WRITE;
128                         break;
129                 case 'r':
130                         act |= ACT_READ;
131                         break;
132                 case 'a':
133                         flags |= O_APPEND;
134                         break;
135                 case 'd':
136                         flags |= O_LOV_DELAY_CREATE;
137                         break;
138                 case 'z':
139                         pad = 0;
140                         act |= ACT_READHOLE;
141                         break;
142                 case 'v':
143                         act |= ACT_VERIFY;
144                         break;
145                 case 'h':
146                         usage();
147                         break;
148                 }
149         }
150
151         if (act == ACT_NONE) {
152                 usage();
153                 return 1;
154         }
155
156         if ((act & ACT_READ) &&  (act & ACT_WRITE)) {
157                 printf("Read and write test should be exclusive\n");
158                 return 1;
159         }
160
161         if (argc - optind < iovcnt) {
162                 printf("Not enough parameters for iov size\n");
163                 return 1;
164         }
165
166         iov = (struct iovec *)malloc(iovcnt * sizeof(struct iovec));
167         if (iov == NULL) {
168                 printf("No memory %s\n", strerror(errno));
169                 return 1;
170         }
171
172         for (c = 0; c < iovcnt; c++) {
173                 struct iovec *iv = &iov[c];
174
175                 iv->iov_len = strtoul(argv[optind++], &end, 0);
176                 if (*end) {
177                         printf("Error iov size\n");
178                         GOTO(out, rc = 1);
179                 }
180                 iv->iov_base = mmap(NULL, iv->iov_len, PROT_READ | PROT_WRITE,
181                                     MAP_PRIVATE | MAP_ANON, 0, 0);
182                 if (iv->iov_base == MAP_FAILED) {
183                         printf("No memory %s\n", strerror(errno));
184                         GOTO(out, rc = 1);
185                 }
186                 if (act & ACT_WRITE)
187                         memset(iv->iov_base, pad, iv->iov_len);
188                 len += iv->iov_len;
189         }
190
191         fd = open(fname, O_LARGEFILE | O_RDWR | O_CREAT | flags, 0644);
192         if (fd == -1) {
193                 printf("Cannot open %s:%s\n", fname, strerror(errno));
194                 return 1;
195         }
196
197         if ((act & ACT_SEEK) && (lseek64(fd, offset, SEEK_SET) < 0)) {
198                 printf("Cannot seek %s\n", strerror(errno));
199                 GOTO(out, rc == 1);
200         }
201
202         if (act & ACT_WRITE) {
203                 rc = writev(fd, iov, iovcnt);
204                 if (rc != len) {
205                         printf("Write error: %s (rc = %d, len = %ld)\n",
206                                 strerror(errno), rc, len);
207                         GOTO(out, rc = 1);
208                 }
209         } else if (act & ACT_READ) {
210                 rc = readv(fd, iov, iovcnt);
211                 if (rc != len) {
212                         printf("Read error: %s rc = %d\n", strerror(errno), rc);
213                         GOTO(out, rc = 1);
214                 }
215
216                 /* It should return zeroed buf if the read hits hole.*/
217                 if (((act & ACT_READHOLE) || (act & ACT_VERIFY)) &&
218                     data_verify(iov, iovcnt, pad))
219                         GOTO(out, rc = 1);
220         }
221
222         rc = 0;
223 out:
224         if (iov)
225                 free(iov);
226         return rc;
227 }