Whamcloud - gitweb
LU-14786 lod: create missing debugfs file
[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) 2014, 2017, Intel Corporation.
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  *
32  * lustre/tests/rwv.c
33  */
34
35 #include <stdio.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <sys/uio.h>
39 #include <sys/mman.h>
40 #include <fcntl.h>
41 #include <errno.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44
45 #include <lustre/lustreapi.h>
46
47 #define ACT_NONE        0
48 #define ACT_READ        1
49 #define ACT_WRITE       2
50 #define ACT_SEEK        4
51 #define ACT_READHOLE    8
52 #define ACT_VERIFY      16
53 #define ACT_OUTPUT      32
54
55 void usage()
56 {
57         printf("usage: rwv -f filename <-r|-w> [-a] [-z] [-d] [-v]");
58         printf(" [-s offset] [-o[outf]] -n iovcnt SIZE1 SIZE2 SIZE3...\n");
59         printf("-a  append IO (O_APPEND)\n");
60         printf("-r  file read (O_RDONLY)\n");
61         printf("-w  file write (O_WRONLY)\n");
62         printf("-s  set the start pos of the read/write test\n");
63         printf("-z  test for read hitting hole\n");
64         printf("-d  create flags (O_LOV_DELAY_CREATE)\n");
65         printf("-v  verify the data content of read\n");
66         printf("-o  write the file content of read to an optional file\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         int out_fd = -1;
96         char pad = 0xba;
97         char *end;
98         char *fname = "FILE";
99         unsigned long len = 0;
100         struct iovec *iov;
101         off64_t offset = 0;
102
103         while ((c = getopt(argc, argv, "f:n:s:rwahvdzo::")) != -1) {
104                 switch (c) {
105                 case 'f':
106                         fname = optarg;
107                         break;
108                 case 'n':
109                         iovcnt = strtoul(optarg, &end, 0);
110                         if (*end) {
111                                 printf("Bad iov count: %s\n", optarg);
112                                 return 1;
113                         }
114                         if (iovcnt > UIO_MAXIOV || iovcnt <= 0) {
115                                 printf("Wrong iov count\n");
116                                 return 1;
117                         }
118                         break;
119                 case 's':
120                         act |= ACT_SEEK;
121                         offset = strtoull(optarg, &end, 0);
122                         if (*end) {
123                                 printf("Bad seek offset: %s\n", optarg);
124                                 return 1;
125                         }
126                         break;
127                 case 'w':
128                         act |= ACT_WRITE;
129                         flags |= O_WRONLY | O_CREAT;
130                         break;
131                 case 'r':
132                         act |= ACT_READ;
133                         flags |= O_RDONLY;
134                         break;
135                 case 'a':
136                         flags |= O_APPEND;
137                         break;
138                 case 'd':
139                         flags |= O_LOV_DELAY_CREATE;
140                         break;
141                 case 'z':
142                         pad = 0;
143                         act |= ACT_READHOLE;
144                         break;
145                 case 'v':
146                         act |= ACT_VERIFY;
147                         break;
148                 case 'o':
149                         act |= ACT_OUTPUT;
150                         if (optarg)
151                                 out_fd = open(optarg, O_WRONLY | O_CREAT, 0644);
152                         else
153                                 out_fd = fileno(stdout);
154                         break;
155                 case 'h':
156                         usage();
157                         break;
158                 }
159         }
160
161         if (act == ACT_NONE) {
162                 usage();
163                 return 1;
164         }
165
166         if ((act & ACT_READ) &&  (act & ACT_WRITE)) {
167                 printf("Read and write test should be exclusive\n");
168                 return 1;
169         }
170
171         if (act & ACT_OUTPUT && (!(act & ACT_READ) || out_fd < 0)) {
172                 printf("-o not in read mode or cannot open the output file");
173                 return 1;
174         }
175
176         if (argc - optind < iovcnt) {
177                 printf("Not enough parameters for iov size\n");
178                 return 1;
179         }
180
181         iov = (struct iovec *)malloc(iovcnt * sizeof(struct iovec));
182         if (!iov) {
183                 printf("No memory %s\n", strerror(errno));
184                 return 1;
185         }
186
187         for (c = 0; c < iovcnt; c++) {
188                 struct iovec *iv = &iov[c];
189
190                 iv->iov_len = strtoul(argv[optind++], &end, 0);
191                 if (*end) {
192                         printf("Error iov size\n");
193                         rc = 1;
194                         goto out;
195                 }
196                 iv->iov_base = mmap(NULL, iv->iov_len, PROT_READ | PROT_WRITE,
197                                     MAP_PRIVATE | MAP_ANON, 0, 0);
198                 if (iv->iov_base == MAP_FAILED) {
199                         printf("No memory %s\n", strerror(errno));
200                         rc = 1;
201                         goto out;
202                 }
203                 if (act & ACT_WRITE)
204                         memset(iv->iov_base, pad, iv->iov_len);
205                 len += iv->iov_len;
206         }
207
208         fd = open(fname, O_LARGEFILE | flags, 0644);
209         if (fd == -1) {
210                 printf("Cannot open %s:%s\n", fname, strerror(errno));
211                 return 1;
212         }
213
214         if ((act & ACT_SEEK) && (lseek64(fd, offset, SEEK_SET) < 0)) {
215                 printf("Cannot seek %s\n", strerror(errno));
216                 rc = 1;
217                 goto out;
218         }
219
220         if (act & ACT_WRITE) {
221                 rc = writev(fd, iov, iovcnt);
222                 if (rc != len) {
223                         printf("Write error: %s (rc = %d, len = %ld)\n",
224                                strerror(errno), rc, len);
225                         rc = 1;
226                         goto out;
227                 }
228         } else if (act & ACT_READ) {
229                 rc = readv(fd, iov, iovcnt);
230                 if (rc != len) {
231                         printf("Read error: %s rc = %d\n", strerror(errno), rc);
232                         rc = 1;
233                         goto out;
234                 }
235
236                 /* It should return zeroed buf if the read hits hole.*/
237                 if (((act & ACT_READHOLE) || (act & ACT_VERIFY)) &&
238                     data_verify(iov, iovcnt, pad)) {
239                         rc = 1;
240                         goto out;
241                 }
242
243                 if (act & ACT_OUTPUT) {
244                         rc = writev(out_fd, iov, iovcnt);
245                         if (rc != len) {
246                                 printf("write error: %s rc = %d\n",
247                                        strerror(errno), rc);
248                                 rc = 1;
249                                 goto out;
250                         }
251                 }
252         }
253
254         rc = 0;
255 out:
256         if (iov)
257                 free(iov);
258         if (out_fd >= 0)
259                 close(out_fd);
260         return rc;
261 }