3 * Copyright (C) Igor Sysoev
7 #include <ngx_config.h>
13 * On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
14 * offsets only, and the including <sys/sendfile.h> breaks the compiling,
15 * if off_t is 64 bit wide. So we use own sendfile() definition, where offset
16 * parameter is int32_t, and use sendfile() for the file parts below 2G only,
17 * see src/os/unix/ngx_linux_config.h
19 * Linux 2.4.21 has the new sendfile64() syscall #239.
21 * On Linux up to 2.6.16 sendfile() does not allow to pass the count parameter
22 * more than 2G-1 bytes even on 64-bit platforms: it returns EINVAL,
23 * so we limit it to 2G-1 bytes.
26 #define NGX_SENDFILE_LIMIT 2147483647L
30 #define NGX_HEADERS 64
32 #define NGX_HEADERS IOV_MAX
37 ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in, off_t limit)
40 off_t size, send, prev_send, aligned, sent, fprev;
45 ngx_uint_t eintr, complete;
49 struct iovec *iov, headers[NGX_HEADERS];
50 #if (NGX_HAVE_SENDFILE64)
63 /* the maximum limit size is 2G-1 - the page size */
65 if (limit == 0 || limit > (off_t) (NGX_SENDFILE_LIMIT - ngx_pagesize)) {
66 limit = NGX_SENDFILE_LIMIT - ngx_pagesize;
72 header.elts = headers;
73 header.size = sizeof(struct iovec);
74 header.nalloc = NGX_HEADERS;
75 header.pool = c->pool;
89 /* create the iovec and coalesce the neighbouring bufs */
92 cl && header.nelts < IOV_MAX && send < limit;
95 if (ngx_buf_special(cl->buf)) {
100 if (!ngx_buf_in_memory(cl->buf) && !cl->buf->in_file) {
101 ngx_log_error(NGX_LOG_ALERT, c->log, 0,
102 "zero size buf in sendfile "
103 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
116 return NGX_CHAIN_ERROR;
120 if (!ngx_buf_in_memory_only(cl->buf)) {
124 size = cl->buf->last - cl->buf->pos;
126 if (send + size > limit) {
130 if (prev == cl->buf->pos) {
131 iov->iov_len += (size_t) size;
134 iov = ngx_array_push(&header);
136 return NGX_CHAIN_ERROR;
139 iov->iov_base = (void *) cl->buf->pos;
140 iov->iov_len = (size_t) size;
143 prev = cl->buf->pos + (size_t) size;
147 /* set TCP_CORK if there is a header before a file */
149 if (c->tcp_nopush == NGX_TCP_NOPUSH_UNSET
154 /* the TCP_CORK and TCP_NODELAY are mutually exclusive */
156 if (c->tcp_nodelay == NGX_TCP_NODELAY_SET) {
160 if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
161 (const void *) &tcp_nodelay, sizeof(int)) == -1)
166 * there is a tiny chance to be interrupted, however,
167 * we continue a processing with the TCP_NODELAY
168 * and without the TCP_CORK
171 if (err != NGX_EINTR) {
173 ngx_connection_error(c, err,
174 "setsockopt(TCP_NODELAY) failed");
175 return NGX_CHAIN_ERROR;
179 c->tcp_nodelay = NGX_TCP_NODELAY_UNSET;
181 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
186 if (c->tcp_nodelay == NGX_TCP_NODELAY_UNSET) {
188 if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
192 * there is a tiny chance to be interrupted, however,
193 * we continue a processing without the TCP_CORK
196 if (err != NGX_EINTR) {
198 ngx_connection_error(c, err,
199 ngx_tcp_nopush_n " failed");
200 return NGX_CHAIN_ERROR;
204 c->tcp_nopush = NGX_TCP_NOPUSH_SET;
206 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
212 /* get the file buf */
214 if (header.nelts == 0 && cl && cl->buf->in_file && send < limit) {
217 /* coalesce the neighbouring file bufs */
220 size = cl->buf->file_last - cl->buf->file_pos;
222 if (send + size > limit) {
225 aligned = (cl->buf->file_pos + size + ngx_pagesize - 1)
226 & ~((off_t) ngx_pagesize - 1);
228 if (aligned <= cl->buf->file_last) {
229 size = aligned - cl->buf->file_pos;
233 file_size += (size_t) size;
235 fprev = cl->buf->file_pos + size;
241 && file->file->fd == cl->buf->file->fd
242 && fprev == cl->buf->file_pos);
247 if (file_size == 0) {
249 return NGX_CHAIN_ERROR;
252 #if (NGX_HAVE_SENDFILE64)
253 offset = file->file_pos;
255 offset = (int32_t) file->file_pos;
258 ngx_log_debug2(NGX_LOG_DEBUG_EVENT, c->log, 0,
259 "sendfile: @%O %uz", file->file_pos, file_size);
261 rc = sendfile(c->fd, file->file->fd, &offset, file_size);
266 if (err == NGX_EAGAIN || err == NGX_EINTR) {
267 if (err == NGX_EINTR) {
271 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
272 "sendfile() is not ready");
276 ngx_connection_error(c, err, "sendfile() failed");
277 return NGX_CHAIN_ERROR;
281 sent = rc > 0 ? rc : 0;
283 ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
284 "sendfile: %d, @%O %O:%uz",
285 rc, file->file_pos, sent, file_size);
288 rc = writev(c->fd, header.elts, header.nelts);
293 if (err == NGX_EAGAIN || err == NGX_EINTR) {
294 if (err == NGX_EINTR) {
298 ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
299 "writev() not ready");
303 ngx_connection_error(c, err, "writev() failed");
304 return NGX_CHAIN_ERROR;
308 sent = rc > 0 ? rc : 0;
310 ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %O", sent);
313 if (send - prev_send == sent) {
319 for (cl = in; cl; cl = cl->next) {
321 if (ngx_buf_special(cl->buf)) {
329 size = ngx_buf_size(cl->buf);
334 if (ngx_buf_in_memory(cl->buf)) {
335 cl->buf->pos = cl->buf->last;
338 if (cl->buf->in_file) {
339 cl->buf->file_pos = cl->buf->file_last;
345 if (ngx_buf_in_memory(cl->buf)) {
346 cl->buf->pos += (size_t) sent;
349 if (cl->buf->in_file) {
350 cl->buf->file_pos += sent;
365 if (send >= limit || cl == NULL) {