Whamcloud - gitweb
LU-5810 tests: add client hostname to lctl mark
[fs/lustre-release.git] / lustre / tests / sendfile_grouplock.c
1 /*
2  * GPL HEADDER 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, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 /*
24  * Copyright 2015 Cray Inc, all rights reserved.
25  * Author: Frank Zago.
26  *
27  * A few portions are extracted from llapi_layout_test.c
28  */
29
30 /*
31  * The CRC32 implementation is from RFC 1952, which bears the
32  * following notice:
33  *
34  * Copyright (c) 1996 L. Peter Deutsch
35  *
36  * Permission is granted to copy and distribute this document for any
37  * purpose and without charge, including translations into other
38  * languages and incorporation into compilations, provided that the
39  * copyright notice and this notice are preserved, and that any
40  * substantive changes or deletions from the original are clearly
41  * marked.
42  */
43
44 /*
45  * The purpose of this test is to exert the group lock ioctls in
46  * conjunction with sendfile. Some bugs were found when both were used
47  * at the same time. See LU-6368 and LU-6371.
48  *
49  * The program will exit as soon as a non zero error code is returned.
50  *
51  * It can be called like this:
52  *
53  * dd if=/dev/zero of=/mnt/lustre/foo1 bs=1M count=40
54  * ./sendfile_grouplock /mnt/lustre/foo1
55  */
56
57 #include <stdlib.h>
58 #include <errno.h>
59 #include <getopt.h>
60 #include <fcntl.h>
61 #include <sys/types.h>
62 #include <sys/stat.h>
63 #include <time.h>
64 #include <unistd.h>
65 #include <poll.h>
66 #include <sys/sendfile.h>
67
68 #include <lustre/lustreapi.h>
69 #include <lustre/lustre_idl.h>
70
71 #define ERROR(fmt, ...)                                                 \
72         fprintf(stderr, "%s: %s:%d: %s: " fmt "\n",                     \
73                 program_invocation_short_name, __FILE__, __LINE__,      \
74                 __func__, ## __VA_ARGS__);
75
76 #define DIE(fmt, ...)                           \
77         do {                                    \
78                 ERROR(fmt, ## __VA_ARGS__);     \
79                 exit(EXIT_FAILURE);             \
80         } while (0)
81
82 #define ASSERTF(cond, fmt, ...)                                         \
83         do {                                                            \
84                 if (!(cond))                                            \
85                         DIE("assertion '%s' failed: "fmt,               \
86                             #cond, ## __VA_ARGS__);                     \
87         } while (0)
88
89 #define PERFORM(testfn) \
90         do {                                                            \
91                 fprintf(stderr, "Starting test " #testfn " at %lld\n",  \
92                         (unsigned long long)time(NULL));                \
93                 testfn();                                               \
94                 fprintf(stderr, "Finishing test " #testfn " at %lld\n", \
95                         (unsigned long long)time(NULL));                \
96         } while (0)
97
98 /* This test will copy from source_file to dest_file */
99 static const char *source_file;
100 static char *dest_file;
101 static unsigned long source_crc; /* CRC32 of original file */
102
103 /*
104  * A small CRC32 implementation, from RFC 1952
105  */
106
107 /* Table of CRCs of all 8-bit messages. */
108 static unsigned long crc_table[256];
109
110 /* Flag: has the table been computed? Initially false. */
111 static int crc_table_computed;
112
113 /* Make the table for a fast CRC. */
114 static void make_crc_table(void)
115 {
116         unsigned long c;
117
118         int n, k;
119         for (n = 0; n < 256; n++) {
120                 c = (unsigned long) n;
121                 for (k = 0; k < 8; k++) {
122                         if (c & 1)
123                                 c = 0xedb88320L ^ (c >> 1);
124                         else
125                                 c = c >> 1;
126                 }
127                 crc_table[n] = c;
128         }
129         crc_table_computed = 1;
130 }
131
132 /*
133  * Update a running crc with the bytes buf[0..len-1] and return the
134  * updated crc. The crc should be initialized to zero. Pre- and
135  * post-conditioning (one's complement) is performed within this
136  * function so it shouldn't be done by the caller. Usage example:
137  *
138  *      unsigned long crc = 0L;
139  *
140  *      while (read_buffer(buffer, length) != EOF) {
141  *              crc = update_crc(crc, buffer, length);
142  *      }
143  *      if (crc != original_crc) error();
144  */
145 static unsigned long update_crc(unsigned long crc,
146                                 unsigned char *buf, int len)
147 {
148         unsigned long c = crc ^ 0xffffffffL;
149         int n;
150
151         if (!crc_table_computed)
152                 make_crc_table();
153         for (n = 0; n < len; n++)
154                 c = crc_table[(c ^ buf[n]) & 0xff] ^ (c >> 8);
155
156         return c ^ 0xffffffffL;
157 }
158
159 /* Cleanup our test file. */
160 static void cleanup(void)
161 {
162         unlink(dest_file);
163 }
164
165 /* Compute the CRC32 of a file */
166 static unsigned long compute_crc(const char *fname)
167 {
168         unsigned char buf[1024*1024];
169         unsigned long crc = 0L;
170         struct stat stbuf;
171         int fd;
172         int rc;
173         size_t filesize;
174
175         fd = open(fname, O_RDONLY);
176         ASSERTF(fd >= 0, "open failed for '%s': %s",
177                 fname, strerror(errno));
178
179         rc = fstat(fd, &stbuf);
180         ASSERTF(rc == 0, "fstat of '%s' failed: %s", fname, strerror(errno));
181         filesize = stbuf.st_size;
182
183         while (filesize != 0) {
184                 size_t to_read = sizeof(buf);
185                 ssize_t sret;
186
187                 if (to_read > filesize)
188                         to_read = filesize;
189
190                 sret = read(fd, buf, to_read);
191                 ASSERTF(sret >= 0, "read of %zu bytes from '%s' failed: %s",
192                         to_read, fname, strerror(errno));
193                 ASSERTF(sret > 0, "unexpected EOF for '%s'",
194                         fname);
195
196                 filesize -= sret;
197                 crc = update_crc(crc, buf, sret);
198         }
199
200         close(fd);
201
202         return crc;
203 }
204
205 /* Helper. Copy a file with sendfile. The destination will be
206  * created. If a group lock is 0, it means do not take one. */
207 static int sendfile_copy(const char *source, int source_gid,
208                          const char *dest, int dest_gid)
209 {
210         int rc;
211         struct stat stbuf;
212         size_t filesize;
213         int fd_in;
214         int fd_out;
215
216         fd_in = open(source, O_RDONLY);
217         ASSERTF(fd_in >= 0, "open failed for '%s': %s",
218                 source, strerror(errno));
219
220         rc = fstat(fd_in, &stbuf);
221         ASSERTF(rc == 0, "fstat of '%s' failed: %s", source, strerror(errno));
222         filesize = stbuf.st_size;
223
224         if (source_gid != 0) {
225                 rc = llapi_group_lock(fd_in, source_gid);
226                 ASSERTF(rc == 0, "cannot set group lock %d for '%s': %s",
227                         source_gid, source, strerror(-rc));
228         }
229
230         fd_out = open(dest, O_WRONLY | O_TRUNC | O_CREAT, 0644);
231         ASSERTF(fd_out >= 0, "creation failed for '%s': %s",
232                 dest, strerror(errno));
233
234         if (dest_gid != 0) {
235                 rc = llapi_group_lock(fd_out, dest_gid);
236                 ASSERTF(rc == 0, "cannot set group lock %d for '%s': %s",
237                         dest_gid, dest, strerror(-rc));
238         }
239
240         /* Transfer by 10M blocks */
241         while (filesize != 0) {
242                 size_t to_copy = 10*1024*1024;
243                 ssize_t sret;
244
245                 if (to_copy > filesize)
246                         to_copy = filesize;
247
248                 sret = sendfile(fd_out, fd_in, NULL, to_copy);
249                 rc = errno;
250
251                 /* Although senfile can return less than requested,
252                  * that should not happen under present conditions. At
253                  * the very least, make sure that a decent size was
254                  * copied. See LU-6371. */
255
256                 ASSERTF(sret != 0, "sendfile read 0 bytes");
257                 ASSERTF(sret > 0, "sendfile failed: %s", strerror(rc));
258                 ASSERTF(sret > 100*1024,
259                         "sendfile read too little data: %zd bytes", sret);
260
261                 if (sret != to_copy)
262                         fprintf(stderr,
263                                "Warning: sendfile returned %zd bytes instead of %zu requested\n",
264                                sret, to_copy);
265
266                 filesize -= sret;
267
268         }
269
270         close(fd_out);
271         close(fd_in);
272
273         return 0;
274 }
275
276 /* Basic sendfile, without lock taken */
277 static void test10(void)
278 {
279         unsigned long crc;
280
281         cleanup();
282         sendfile_copy(source_file, 0, dest_file, 0);
283         sync();
284
285         crc = compute_crc(dest_file);
286         ASSERTF(source_crc == crc, "CRC differs: %lu and %lu", source_crc, crc);
287 }
288
289 /* sendfile, source locked */
290 static void test11(void)
291 {
292         unsigned long crc;
293
294         cleanup();
295         sendfile_copy(source_file, 85543, dest_file, 0);
296         sync();
297
298         crc = compute_crc(dest_file);
299         ASSERTF(source_crc == crc, "CRC differs: %lu and %lu", source_crc, crc);
300 }
301
302 /* sendfile, destination locked */
303 static void test12(void)
304 {
305         unsigned long crc;
306
307         cleanup();
308         sendfile_copy(source_file, 0, dest_file, 98765);
309         sync();
310
311         crc = compute_crc(dest_file);
312         ASSERTF(source_crc == crc, "CRC differs: %lu and %lu", source_crc, crc);
313 }
314
315 /* sendfile, source and destination locked, with same lock number */
316 static void test13(void)
317 {
318         const int gid = 8765;
319         unsigned long crc;
320
321         cleanup();
322         sendfile_copy(source_file, gid, dest_file, gid);
323         sync();
324
325         crc = compute_crc(dest_file);
326         ASSERTF(source_crc == crc, "CRC differs: %lu and %lu", source_crc, crc);
327 }
328
329 /* sendfile, source and destination locked, with different lock number */
330 static void test14(void)
331 {
332         unsigned long crc;
333
334         cleanup();
335         sendfile_copy(source_file, 98765, dest_file, 34543);
336         sync();
337
338         crc = compute_crc(dest_file);
339         ASSERTF(source_crc == crc, "CRC differs: %lu and %lu", source_crc, crc);
340 }
341
342 /* Basic sendfile, without lock taken, to /dev/null */
343 static void test15(void)
344 {
345         sendfile_copy(source_file, 0, "/dev/null", 0);
346         sync();
347 }
348
349 /* sendfile, source locked, to /dev/null */
350 static void test16(void)
351 {
352         sendfile_copy(source_file, 85543, "/dev/null", 0);
353         sync();
354 }
355
356 int main(int argc, char *argv[])
357 {
358         int rc;
359
360         if (argc != 2 || argv[1][0] != '/') {
361                 fprintf(stderr,
362                         "Argument must be an absolute path to a Lustre file\n");
363                 return EXIT_FAILURE;
364         }
365
366         source_file = argv[1];
367         rc = asprintf(&dest_file, "%s-dest", source_file);
368         if (rc == -1) {
369                 fprintf(stderr, "Allocation failure\n");
370                 return EXIT_FAILURE;
371         }
372
373         /* Play nice with Lustre test scripts. Non-line buffered output
374          * stream under I/O redirection may appear incorrectly. */
375         setvbuf(stdout, NULL, _IOLBF, 0);
376
377         cleanup();
378         atexit(cleanup);
379
380         /* Compute crc of original file */
381         source_crc = compute_crc(source_file);
382
383         PERFORM(test10);
384         PERFORM(test11);
385         PERFORM(test12);
386         PERFORM(test13);
387         PERFORM(test14);
388         PERFORM(test15);
389         PERFORM(test16);
390
391         return EXIT_SUCCESS;
392 }