Whamcloud - gitweb
LU-13004 ptlrpc: Allow BULK_BUF_KIOV to accept a kvec
[fs/lustre-release.git] / lustre / tests / sendfile.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, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #include <stdio.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <time.h>
37 #include <sys/types.h>
38 #include <sys/socket.h>
39 #include <sys/sendfile.h>
40 #include <sys/stat.h>
41 #include <sys/socket.h>
42
43 #include <lustre/lustreapi.h>
44
45 #define syserr(str) { perror(str); exit(-1); }
46
47 int main(int argc, char *argv[])
48 {
49         char *sfile, *tfile;
50         struct stat stbuf;
51         int size;
52         unsigned long bufsize = 1024 * 1024;
53         int infd, outfd;
54         int sd[2];
55         int rc;
56         char *buf;
57         char cmd[1024];
58         loff_t pos;
59
60         if (argc < 3) {
61                 fprintf(stderr, "%s <source file> <dest file>\n", argv[0]);
62                 exit(-1);
63         }
64
65         sfile = argv[1];
66         tfile = argv[2];
67
68         if (stat(sfile, &stbuf) < 0) {
69                 if (errno == ENOENT) {
70                         /* assume doing non-object file testing */
71                         infd = open(sfile,
72                                     O_LOV_DELAY_CREATE | O_CREAT | O_RDWR,
73                                     0644);
74                         if (infd < 0)
75                                 syserr("open source file:");
76
77                         size = random() % (1 * 1024 * 1024) + 1024;
78                         if (ftruncate(infd, (off_t)size) < 0)
79                                 syserr("truncate file error:");
80                 } else {
81                         syserr("stat file: ");
82                 }
83         } else if (S_ISREG(stbuf.st_mode)) {
84                 size = (int)stbuf.st_size;
85                 infd = open(sfile, O_RDONLY, 0644);
86                 if (infd < 0)
87                         syserr("Open an existing file error:");
88         } else {
89                 fprintf(stderr, "%s is not a regular file\n", sfile);
90                 exit(-1);
91         }
92
93         outfd = open(tfile, O_WRONLY | O_TRUNC | O_CREAT, 0666);
94         if (outfd < 0)
95                 syserr("open dest file:");
96
97         rc = socketpair(AF_LOCAL, SOCK_STREAM, 0, sd);
98         if (rc < 0)
99                 syserr("socketpair");
100
101         rc = fcntl(sd[0], F_SETFL, O_NONBLOCK);
102         if (rc < 0)
103                 syserr("fcntl");
104
105         rc = setsockopt(sd[0], SOL_SOCKET, SO_SNDBUF,
106                         &bufsize, sizeof(bufsize));
107         if (rc)
108                 syserr("setsockopt");
109
110         srandom(time(NULL));
111
112         pos = 0;
113         while (size > 0) {
114                 int rc2;
115                 size_t seg_size;
116
117                 seg_size = random() % bufsize + 1;
118                 if (seg_size > size)
119                         seg_size = size;
120
121                 while (seg_size) {
122                         rc = sendfile(sd[0], infd, &pos, seg_size);
123                         if (rc < 0)
124                                 syserr("sendfile:");
125
126                         seg_size -= rc;
127                         size -= rc;
128                         if (size == 0)
129                                 close(sd[0]);
130
131                         buf = malloc(rc);
132                         if (read(sd[1], buf, rc) < 0)
133                                 syserr("read from socket:");
134
135                         rc2 = write(outfd, buf, rc);
136                         if (rc2 != rc)
137                                 syserr("write dest file error:");
138                         free(buf);
139                 }
140         }
141         close(sd[1]), close(infd), close(outfd);
142
143         snprintf(cmd, sizeof(cmd), "cmp %s %s\n", sfile, tfile);
144         return system(cmd);
145 }