Whamcloud - gitweb
LU-2093 lod: fall back to RR allocation when QoS fails
[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 <liblustre.h>
47 #include <obd.h> /* needed for lov_stripe_md_size */
48 #include <lustre/lustreapi.h>
49
50 #define ACT_NONE        0
51 #define ACT_READ        1
52 #define ACT_WRITE       2
53 #define ACT_SEEK        4
54 #define ACT_READHOLE    8
55 #define ACT_VERIFY      16
56
57 void usage()
58 {
59         printf("usage: rwv -f filename <-r|-w> [-a] [-z] [-d] [-v]"
60                 "[-s offset] -n iovcnt SIZE1 SIZE2 SIZE3...\n");
61         printf("-a  append IO (O_APPEND)\n");
62         printf("-r  file read (O_RDONLY)\n");
63         printf("-w  file write (O_WRONLY)\n");
64         printf("-s  set the start pos of the read/write test\n");
65         printf("-z  test for read hitting hole\n");
66         printf("-d  create flags (O_LOV_DELAY_CREATE)\n");
67         printf("-v  verify the data content of read\n");
68 }
69
70 int data_verify(struct iovec *iov, int iovcnt, char c)
71 {
72         int i;
73
74         for (i = 0; i < iovcnt; i++) {
75                 size_t count = iov[i].iov_len;
76                 char *s = iov[i].iov_base;
77
78                 for (; count > 0; ++s, count--) {
79                         if (*s != c) {
80                                 printf("Data mismatch %x: %x\n", *s, c);
81                                 return 1;
82                         }
83                 }
84         }
85         return 0;
86 }
87
88 int main(int argc, char** argv)
89 {
90         int c;
91         int fd;
92         int rc = 0;
93         int flags = 0;
94         int iovcnt = 0;
95         int act = ACT_NONE;
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:rwahvdz")) != -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                         break;
130                 case 'r':
131                         act |= ACT_READ;
132                         break;
133                 case 'a':
134                         flags |= O_APPEND;
135                         break;
136                 case 'd':
137                         flags |= O_LOV_DELAY_CREATE;
138                         break;
139                 case 'z':
140                         pad = 0;
141                         act |= ACT_READHOLE;
142                         break;
143                 case 'v':
144                         act |= ACT_VERIFY;
145                         break;
146                 case 'h':
147                         usage();
148                         break;
149                 }
150         }
151
152         if (act == ACT_NONE) {
153                 usage();
154                 return 1;
155         }
156
157         if ((act & ACT_READ) &&  (act & ACT_WRITE)) {
158                 printf("Read and write test should be exclusive\n");
159                 return 1;
160         }
161
162         if (argc - optind < iovcnt) {
163                 printf("Not enough parameters for iov size\n");
164                 return 1;
165         }
166
167         iov = (struct iovec *)malloc(iovcnt * sizeof(struct iovec));
168         if (iov == NULL) {
169                 printf("No memory %s\n", strerror(errno));
170                 return 1;
171         }
172
173         for (c = 0; c < iovcnt; c++) {
174                 struct iovec *iv = &iov[c];
175
176                 iv->iov_len = strtoul(argv[optind++], &end, 0);
177                 if (*end) {
178                         printf("Error iov size\n");
179                         GOTO(out, rc = 1);
180                 }
181                 iv->iov_base = mmap(NULL, iv->iov_len, PROT_READ | PROT_WRITE,
182                                     MAP_PRIVATE | MAP_ANON, 0, 0);
183                 if (iv->iov_base == MAP_FAILED) {
184                         printf("No memory %s\n", strerror(errno));
185                         GOTO(out, rc = 1);
186                 }
187                 if (act & ACT_WRITE)
188                         memset(iv->iov_base, pad, iv->iov_len);
189                 len += iv->iov_len;
190         }
191
192         fd = open(fname, O_LARGEFILE | O_RDWR | O_CREAT | flags, 0644);
193         if (fd == -1) {
194                 printf("Cannot open %s:%s\n", fname, strerror(errno));
195                 return 1;
196         }
197
198         if ((act & ACT_SEEK) && (lseek64(fd, offset, SEEK_SET) < 0)) {
199                 printf("Cannot seek %s\n", strerror(errno));
200                 GOTO(out, rc == 1);
201         }
202
203         if (act & ACT_WRITE) {
204                 rc = writev(fd, iov, iovcnt);
205                 if (rc != len) {
206                         printf("Write error: %s (rc = %d, len = %ld)\n",
207                                 strerror(errno), rc, len);
208                         GOTO(out, rc = 1);
209                 }
210         } else if (act & ACT_READ) {
211                 rc = readv(fd, iov, iovcnt);
212                 if (rc != len) {
213                         printf("Read error: %s rc = %d\n", strerror(errno), rc);
214                         GOTO(out, rc = 1);
215                 }
216
217                 /* It should return zeroed buf if the read hits hole.*/
218                 if (((act & ACT_READHOLE) || (act & ACT_VERIFY)) &&
219                     data_verify(iov, iovcnt, pad))
220                         GOTO(out, rc = 1);
221         }
222
223         rc = 0;
224 out:
225         if (iov)
226                 free(iov);
227         return rc;
228 }