nginx/src/os/unix/ngx_linux_sendfile_chain.c

249 lines
6.4 KiB
C
Raw Normal View History

2003-05-29 21:02:09 +08:00
2004-03-01 05:03:02 +08:00
/*
* Copyright (C) 2002-2004 Igor Sysoev, http://sysoev.ru/en/
*/
2003-05-29 21:02:09 +08:00
#include <ngx_config.h>
#include <ngx_core.h>
2003-11-26 04:44:56 +08:00
#include <ngx_event.h>
2003-05-29 21:02:09 +08:00
2003-11-28 16:40:40 +08:00
/*
* On Linux up to 2.4.21 sendfile() (syscall #187) works with 32-bit
2004-02-09 15:46:43 +08:00
* offsets only and the including <sys/sendfile.h> breaks the compiling
* if off_t is 64 bit wide. So we use own sendfile() definition where offset
2003-12-01 04:03:18 +08:00
* parameter is int32_t and use sendfile() with the file parts below 2G.
2003-11-28 16:40:40 +08:00
*
* Linux 2.4.21 has a new sendfile64() syscall #239.
*/
2003-05-29 21:02:09 +08:00
ngx_chain_t *ngx_linux_sendfile_chain(ngx_connection_t *c, ngx_chain_t *in)
{
2003-11-26 04:44:56 +08:00
int rc;
2003-05-29 21:02:09 +08:00
char *prev;
2003-11-28 03:01:37 +08:00
off_t fprev;
2003-11-26 04:44:56 +08:00
size_t size, fsize, sent;
2004-02-01 16:10:52 +08:00
ngx_int_t eintr;
2003-05-29 21:02:09 +08:00
struct iovec *iov;
ngx_err_t err;
ngx_hunk_t *file;
2003-11-26 04:44:56 +08:00
ngx_array_t header;
ngx_event_t *wev;
2003-11-26 23:42:18 +08:00
ngx_chain_t *cl, *tail;
2003-11-28 03:01:37 +08:00
#if (HAVE_SENDFILE64)
off_t offset;
#else
int32_t offset;
#endif
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
wev = c->write;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (!wev->ready) {
return in;
}
do {
file = NULL;
fsize = 0;
eintr = 0;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
ngx_init_array(header, c->pool, 10, sizeof(struct iovec),
NGX_CHAIN_ERROR);
2003-05-29 21:02:09 +08:00
prev = NULL;
iov = NULL;
2003-11-26 04:44:56 +08:00
/* create the iovec and coalesce the neighbouring hunks */
2004-02-18 23:45:21 +08:00
for (cl = in; cl && header.nelts < IOV_MAX; cl = cl->next) {
2003-11-26 04:44:56 +08:00
if (ngx_hunk_special(cl->hunk)) {
continue;
}
if (!ngx_hunk_in_memory_only(cl->hunk)) {
break;
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (prev == cl->hunk->pos) {
iov->iov_len += cl->hunk->last - cl->hunk->pos;
2003-05-29 21:02:09 +08:00
} else {
ngx_test_null(iov, ngx_push_array(&header), NGX_CHAIN_ERROR);
2003-11-26 04:44:56 +08:00
iov->iov_base = cl->hunk->pos;
iov->iov_len = cl->hunk->last - cl->hunk->pos;
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
prev = cl->hunk->last;
}
/* set TCP_CORK if there is a header before a file */
2004-01-06 04:55:48 +08:00
if (!c->tcp_nopush == 0
2003-11-26 04:44:56 +08:00
&& header.nelts != 0
&& cl
&& cl->hunk->type & NGX_HUNK_FILE)
{
2004-02-09 15:46:43 +08:00
if (ngx_tcp_nopush(c->fd) == NGX_ERROR) {
err = ngx_errno;
2003-11-26 04:44:56 +08:00
2004-02-09 15:46:43 +08:00
/*
* there is a tiny chance to be interrupted, however
* we continue a processing without the TCP_CORK
*/
2003-05-29 21:02:09 +08:00
2004-02-09 15:46:43 +08:00
if (err != NGX_EINTR) {
wev->error = 1;
ngx_connection_error(c, err, ngx_tcp_nopush_n " failed");
return NGX_CHAIN_ERROR;
}
} else {
c->tcp_nopush = 1;
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, 0,
"tcp_nopush");
2003-11-26 04:44:56 +08:00
}
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
if (header.nelts == 0 && cl && cl->hunk->type & NGX_HUNK_FILE) {
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
/* get the file hunk */
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
file = cl->hunk;
fsize = (size_t) (file->file_last - file->file_pos);
2003-11-28 03:01:37 +08:00
fprev = file->file_last;
2003-11-26 04:44:56 +08:00
cl = cl->next;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
/* coalesce the neighbouring file hunks */
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
while (cl && (cl->hunk->type & NGX_HUNK_FILE)) {
if (file->file->fd != cl->hunk->file->fd
2003-11-28 03:01:37 +08:00
|| fprev != cl->hunk->file_pos)
2003-11-26 04:44:56 +08:00
{
break;
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
fsize += (size_t) (cl->hunk->file_last - cl->hunk->file_pos);
2003-11-28 03:01:37 +08:00
fprev = cl->hunk->file_last;
2003-11-26 04:44:56 +08:00
cl = cl->next;
}
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
/*
* the tail is the rest of the chain that exceeded
* a single sendfile() capability
*/
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
tail = cl;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (fsize) {
2003-11-28 03:01:37 +08:00
#if (HAVE_SENDFILE64)
2003-11-26 23:42:18 +08:00
offset = file->file_pos;
2003-11-28 03:01:37 +08:00
#else
offset = (int32_t) file->file_pos;
#endif
2003-11-26 23:42:18 +08:00
rc = sendfile(c->fd, file->file->fd, &offset, fsize);
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (rc == -1) {
err = ngx_errno;
2003-05-29 21:02:09 +08:00
2004-02-09 15:46:43 +08:00
if (err == NGX_EAGAIN || err == NGX_EINTR) {
if (err == NGX_EINTR) {
eintr = 1;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"sendfile() is not ready");
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
} else {
2004-02-09 15:46:43 +08:00
wev->error = 1;
ngx_connection_error(c, err, "sendfile() failed");
2003-11-26 04:44:56 +08:00
return NGX_CHAIN_ERROR;
}
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
sent = rc > 0 ? rc : 0;
2003-05-29 21:02:09 +08:00
2004-02-09 15:46:43 +08:00
ngx_log_debug4(NGX_LOG_DEBUG_EVENT, c->log, 0,
"sendfile: %d, @" OFF_T_FMT " %d:%d",
rc, file->file_pos, sent, fsize);
2003-11-26 04:44:56 +08:00
} else {
rc = writev(c->fd, header.elts, header.nelts);
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (rc == -1) {
err = ngx_errno;
2003-05-29 21:02:09 +08:00
2004-02-09 15:46:43 +08:00
if (err == NGX_EAGAIN || err == NGX_EINTR) {
if (err == NGX_EINTR) {
eintr = 1;
}
ngx_log_debug0(NGX_LOG_DEBUG_EVENT, c->log, err,
"writev() not ready");
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
} else {
2004-02-09 15:46:43 +08:00
wev->error = 1;
ngx_connection_error(c, err, "writev() failed");
return NGX_CHAIN_ERROR;
2003-11-26 04:44:56 +08:00
}
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
sent = rc > 0 ? rc : 0;
2003-05-29 21:02:09 +08:00
2004-02-09 15:46:43 +08:00
ngx_log_debug1(NGX_LOG_DEBUG_EVENT, c->log, 0, "writev: %d", sent);
2003-11-26 04:44:56 +08:00
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
c->sent += sent;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
for (cl = in; cl; cl = cl->next) {
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (ngx_hunk_special(cl->hunk)) {
continue;
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (sent == 0) {
break;
}
size = ngx_hunk_size(cl->hunk);
if (sent >= size) {
sent -= size;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos = cl->hunk->last;
}
if (cl->hunk->type & NGX_HUNK_FILE) {
cl->hunk->file_pos = cl->hunk->file_last;
}
continue;
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
if (cl->hunk->type & NGX_HUNK_IN_MEMORY) {
cl->hunk->pos += sent;
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
if (cl->hunk->type & NGX_HUNK_FILE) {
cl->hunk->file_pos += sent;
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
break;
2003-05-29 21:02:09 +08:00
}
2003-11-26 04:44:56 +08:00
in = cl;
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
/* "tail == in" means that a single sendfile() is complete */
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
} while ((tail && tail == in) || eintr);
if (in) {
wev->ready = 0;
}
2003-05-29 21:02:09 +08:00
2003-11-26 04:44:56 +08:00
return in;
2003-05-29 21:02:09 +08:00
}