0
|
1
|
|
2 /*
|
|
3 * Copyright (C) Igor Sysoev
|
|
4 */
|
|
5
|
|
6
|
|
7 #include <ngx_config.h>
|
|
8 #include <ngx_core.h>
|
|
9 #include <ngx_event.h>
|
|
10
|
|
11
|
18
|
12 #if 0
|
|
13 #define NGX_SENDFILE_LIMIT 4096
|
|
14 #endif
|
|
15
|
|
16
|
|
17 #define NGX_NONE 1
|
0
|
18
|
|
19
|
16
|
20 static ngx_inline ngx_int_t
|
412
|
21 ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf);
|
18
|
22 static ngx_int_t ngx_output_chain_add_copy(ngx_pool_t *pool,
|
48
|
23 ngx_chain_t **chain, ngx_chain_t *in);
|
412
|
24 static ngx_int_t ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx,
|
|
25 off_t bsize);
|
|
26 static ngx_int_t ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx,
|
|
27 off_t bsize);
|
|
28 static ngx_int_t ngx_output_chain_copy_buf(ngx_output_chain_ctx_t *ctx);
|
0
|
29
|
|
30
|
48
|
31 ngx_int_t
|
|
32 ngx_output_chain(ngx_output_chain_ctx_t *ctx, ngx_chain_t *in)
|
0
|
33 {
|
22
|
34 off_t bsize;
|
146
|
35 ngx_int_t rc, last;
|
0
|
36 ngx_chain_t *cl, *out, **last_out;
|
|
37
|
4
|
38 if (ctx->in == NULL && ctx->busy == NULL) {
|
0
|
39
|
18
|
40 /*
|
|
41 * the short path for the case when the ctx->in and ctx->busy chains
|
|
42 * are empty, the incoming chain is empty too or has the single buf
|
|
43 * that does not require the copy
|
|
44 */
|
0
|
45
|
|
46 if (in == NULL) {
|
|
47 return ctx->output_filter(ctx->filter_ctx, in);
|
|
48 }
|
|
49
|
|
50 if (in->next == NULL
|
18
|
51 #if (NGX_SENDFILE_LIMIT)
|
|
52 && !(in->buf->in_file && in->buf->file_last > NGX_SENDFILE_LIMIT)
|
|
53 #endif
|
412
|
54 && ngx_output_chain_as_is(ctx, in->buf))
|
0
|
55 {
|
|
56 return ctx->output_filter(ctx->filter_ctx, in);
|
|
57 }
|
|
58 }
|
|
59
|
|
60 /* add the incoming buf to the chain ctx->in */
|
|
61
|
|
62 if (in) {
|
18
|
63 if (ngx_output_chain_add_copy(ctx->pool, &ctx->in, in) == NGX_ERROR) {
|
0
|
64 return NGX_ERROR;
|
|
65 }
|
|
66 }
|
|
67
|
|
68 out = NULL;
|
|
69 last_out = &out;
|
48
|
70 last = NGX_NONE;
|
0
|
71
|
|
72 for ( ;; ) {
|
|
73
|
|
74 while (ctx->in) {
|
|
75
|
|
76 /*
|
|
77 * cycle while there are the ctx->in bufs
|
412
|
78 * and there are the free output bufs to copy in
|
0
|
79 */
|
|
80
|
|
81 bsize = ngx_buf_size(ctx->in->buf);
|
|
82
|
|
83 if (bsize == 0 && !ngx_buf_special(ctx->in->buf)) {
|
|
84
|
|
85 ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0,
|
32
|
86 "zero size buf in output "
|
|
87 "t:%d r:%d f:%d %p %p-%p %p %O-%O",
|
|
88 ctx->in->buf->temporary,
|
|
89 ctx->in->buf->recycled,
|
|
90 ctx->in->buf->in_file,
|
|
91 ctx->in->buf->start,
|
|
92 ctx->in->buf->pos,
|
|
93 ctx->in->buf->last,
|
|
94 ctx->in->buf->file,
|
|
95 ctx->in->buf->file_pos,
|
|
96 ctx->in->buf->file_last);
|
0
|
97
|
22
|
98 ngx_debug_point();
|
|
99
|
0
|
100 ctx->in = ctx->in->next;
|
|
101
|
|
102 continue;
|
|
103 }
|
|
104
|
412
|
105 if (ngx_output_chain_as_is(ctx, ctx->in->buf)) {
|
0
|
106
|
|
107 /* move the chain link to the output chain */
|
|
108
|
|
109 cl = ctx->in;
|
|
110 ctx->in = cl->next;
|
|
111
|
|
112 *last_out = cl;
|
|
113 last_out = &cl->next;
|
|
114 cl->next = NULL;
|
|
115
|
|
116 continue;
|
|
117 }
|
|
118
|
|
119 if (ctx->buf == NULL) {
|
|
120
|
412
|
121 rc = ngx_output_chain_align_file_buf(ctx, bsize);
|
0
|
122
|
412
|
123 if (rc == NGX_ERROR) {
|
|
124 return NGX_ERROR;
|
|
125 }
|
0
|
126
|
412
|
127 if (rc != NGX_OK) {
|
0
|
128
|
412
|
129 if (ctx->free) {
|
0
|
130
|
412
|
131 /* get the free buf */
|
0
|
132
|
412
|
133 cl = ctx->free;
|
|
134 ctx->buf = cl->buf;
|
|
135 ctx->free = cl->next;
|
0
|
136
|
412
|
137 ngx_free_chain(ctx->pool, cl);
|
0
|
138
|
412
|
139 } else if (out || ctx->allocated == ctx->bufs.num) {
|
0
|
140
|
412
|
141 break;
|
0
|
142
|
412
|
143 } else if (ngx_output_chain_get_buf(ctx, bsize) != NGX_OK) {
|
0
|
144 return NGX_ERROR;
|
|
145 }
|
|
146 }
|
|
147 }
|
|
148
|
412
|
149 rc = ngx_output_chain_copy_buf(ctx);
|
0
|
150
|
|
151 if (rc == NGX_ERROR) {
|
|
152 return rc;
|
|
153 }
|
|
154
|
|
155 if (rc == NGX_AGAIN) {
|
|
156 if (out) {
|
|
157 break;
|
|
158 }
|
|
159
|
|
160 return rc;
|
|
161 }
|
|
162
|
|
163 /* delete the completed buf from the ctx->in chain */
|
|
164
|
|
165 if (ngx_buf_size(ctx->in->buf) == 0) {
|
|
166 ctx->in = ctx->in->next;
|
|
167 }
|
|
168
|
50
|
169 cl = ngx_alloc_chain_link(ctx->pool);
|
|
170 if (cl == NULL) {
|
10
|
171 return NGX_ERROR;
|
|
172 }
|
50
|
173
|
10
|
174 cl->buf = ctx->buf;
|
|
175 cl->next = NULL;
|
0
|
176 *last_out = cl;
|
|
177 last_out = &cl->next;
|
|
178 ctx->buf = NULL;
|
|
179 }
|
|
180
|
|
181 if (out == NULL && last != NGX_NONE) {
|
58
|
182
|
|
183 if (ctx->in) {
|
|
184 return NGX_AGAIN;
|
|
185 }
|
|
186
|
0
|
187 return last;
|
|
188 }
|
|
189
|
|
190 last = ctx->output_filter(ctx->filter_ctx, out);
|
|
191
|
118
|
192 if (last == NGX_ERROR || last == NGX_DONE) {
|
|
193 return last;
|
|
194 }
|
|
195
|
0
|
196 ngx_chain_update_chains(&ctx->free, &ctx->busy, &out, ctx->tag);
|
|
197 last_out = &out;
|
|
198 }
|
|
199 }
|
|
200
|
|
201
|
16
|
202 static ngx_inline ngx_int_t
|
412
|
203 ngx_output_chain_as_is(ngx_output_chain_ctx_t *ctx, ngx_buf_t *buf)
|
0
|
204 {
|
18
|
205 ngx_uint_t sendfile;
|
|
206
|
0
|
207 if (ngx_buf_special(buf)) {
|
412
|
208 return 1;
|
|
209 }
|
|
210
|
|
211 if (buf->in_file && buf->file->directio) {
|
0
|
212 return 0;
|
|
213 }
|
|
214
|
18
|
215 sendfile = ctx->sendfile;
|
|
216
|
|
217 #if (NGX_SENDFILE_LIMIT)
|
|
218
|
|
219 if (buf->in_file && buf->file_pos >= NGX_SENDFILE_LIMIT) {
|
|
220 sendfile = 0;
|
|
221 }
|
|
222
|
|
223 #endif
|
|
224
|
|
225 if (!sendfile) {
|
4
|
226
|
0
|
227 if (!ngx_buf_in_memory(buf)) {
|
412
|
228 return 0;
|
0
|
229 }
|
|
230
|
|
231 buf->in_file = 0;
|
|
232 }
|
|
233
|
|
234 if (ctx->need_in_memory && !ngx_buf_in_memory(buf)) {
|
412
|
235 return 0;
|
0
|
236 }
|
|
237
|
|
238 if (ctx->need_in_temp && (buf->memory || buf->mmap)) {
|
412
|
239 return 0;
|
0
|
240 }
|
|
241
|
412
|
242 return 1;
|
0
|
243 }
|
|
244
|
|
245
|
48
|
246 static ngx_int_t
|
|
247 ngx_output_chain_add_copy(ngx_pool_t *pool, ngx_chain_t **chain,
|
|
248 ngx_chain_t *in)
|
18
|
249 {
|
|
250 ngx_chain_t *cl, **ll;
|
|
251 #if (NGX_SENDFILE_LIMIT)
|
|
252 ngx_buf_t *b, *buf;
|
|
253 #endif
|
|
254
|
|
255 ll = chain;
|
|
256
|
|
257 for (cl = *chain; cl; cl = cl->next) {
|
|
258 ll = &cl->next;
|
|
259 }
|
|
260
|
|
261 while (in) {
|
|
262
|
50
|
263 cl = ngx_alloc_chain_link(pool);
|
|
264 if (cl == NULL) {
|
18
|
265 return NGX_ERROR;
|
|
266 }
|
|
267
|
|
268 #if (NGX_SENDFILE_LIMIT)
|
|
269
|
|
270 buf = in->buf;
|
|
271
|
|
272 if (buf->in_file
|
|
273 && buf->file_pos < NGX_SENDFILE_LIMIT
|
|
274 && buf->file_last > NGX_SENDFILE_LIMIT)
|
|
275 {
|
412
|
276 /* split a file buf on two bufs by the sendfile limit */
|
|
277
|
50
|
278 b = ngx_calloc_buf(pool);
|
|
279 if (b == NULL) {
|
18
|
280 return NGX_ERROR;
|
|
281 }
|
|
282
|
|
283 ngx_memcpy(b, buf, sizeof(ngx_buf_t));
|
|
284
|
|
285 if (ngx_buf_in_memory(buf)) {
|
|
286 buf->pos += (ssize_t) (NGX_SENDFILE_LIMIT - buf->file_pos);
|
|
287 b->last = buf->pos;
|
|
288 }
|
|
289
|
|
290 buf->file_pos = NGX_SENDFILE_LIMIT;
|
|
291 b->file_last = NGX_SENDFILE_LIMIT;
|
|
292
|
|
293 cl->buf = b;
|
|
294
|
|
295 } else {
|
|
296 cl->buf = buf;
|
|
297 in = in->next;
|
|
298 }
|
|
299
|
|
300 #else
|
|
301 cl->buf = in->buf;
|
|
302 in = in->next;
|
|
303
|
|
304 #endif
|
|
305
|
|
306 *ll = cl;
|
|
307 ll = &cl->next;
|
|
308 }
|
|
309
|
|
310 *ll = NULL;
|
|
311
|
|
312 return NGX_OK;
|
|
313 }
|
|
314
|
|
315
|
48
|
316 static ngx_int_t
|
412
|
317 ngx_output_chain_align_file_buf(ngx_output_chain_ctx_t *ctx, off_t bsize)
|
|
318 {
|
|
319 size_t size;
|
|
320 ngx_buf_t *in;
|
|
321
|
|
322 in = ctx->in->buf;
|
|
323
|
|
324 if (in->file == NULL || !in->file->directio) {
|
|
325 return NGX_DECLINED;
|
|
326 }
|
|
327
|
|
328 ctx->directio = 1;
|
|
329
|
|
330 size = (size_t) (in->file_pos - (in->file_pos & ~511));
|
|
331
|
|
332 if (size == 0) {
|
|
333
|
414
|
334 if (bsize >= (off_t) ctx->bufs.size) {
|
412
|
335 return NGX_DECLINED;
|
|
336 }
|
|
337
|
|
338 size = (size_t) bsize;
|
|
339
|
|
340 } else {
|
|
341 size = 512 - size;
|
|
342
|
414
|
343 if ((off_t) size > bsize) {
|
412
|
344 size = (size_t) bsize;
|
|
345 }
|
|
346 }
|
|
347
|
|
348 ctx->buf = ngx_create_temp_buf(ctx->pool, size);
|
|
349 if (ctx->buf == NULL) {
|
|
350 return NGX_ERROR;
|
|
351 }
|
|
352
|
|
353 /*
|
|
354 * we do not set ctx->buf->tag, because we do not want
|
|
355 * to reuse the buf via ctx->free list
|
|
356 */
|
|
357
|
416
|
358 #if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
359 ctx->unaligned = 1;
|
|
360 #endif
|
|
361
|
412
|
362 return NGX_OK;
|
|
363 }
|
|
364
|
|
365
|
|
366 static ngx_int_t
|
|
367 ngx_output_chain_get_buf(ngx_output_chain_ctx_t *ctx, off_t bsize)
|
0
|
368 {
|
412
|
369 size_t size;
|
|
370 ngx_buf_t *b, *in;
|
|
371 ngx_uint_t recycled;
|
|
372
|
|
373 in = ctx->in->buf;
|
|
374 size = ctx->bufs.size;
|
|
375 recycled = 1;
|
|
376
|
|
377 if (in->last_in_chain) {
|
|
378
|
|
379 if (bsize < (off_t) size) {
|
|
380
|
|
381 /*
|
|
382 * allocate a small temp buf for a small last buf
|
|
383 * or its small last part
|
|
384 */
|
|
385
|
|
386 size = (size_t) bsize;
|
|
387 recycled = 0;
|
|
388
|
|
389 } else if (!ctx->directio
|
|
390 && ctx->bufs.num == 1
|
|
391 && (bsize < (off_t) (size + size / 4)))
|
|
392 {
|
|
393 /*
|
|
394 * allocate a temp buf that equals to a last buf,
|
|
395 * if there is no directio, the last buf size is lesser
|
|
396 * than 1.25 of bufs.size and the temp buf is single
|
|
397 */
|
|
398
|
|
399 size = (size_t) bsize;
|
|
400 recycled = 0;
|
|
401 }
|
|
402 }
|
|
403
|
|
404 b = ngx_calloc_buf(ctx->pool);
|
|
405 if (b == NULL) {
|
|
406 return NGX_ERROR;
|
|
407 }
|
|
408
|
|
409 if (ctx->directio) {
|
|
410
|
|
411 /*
|
|
412 * allocate block aligned to a disk sector size to enable
|
|
413 * userland buffer direct usage conjunctly with directio
|
|
414 */
|
|
415
|
|
416 b->start = ngx_pmemalign(ctx->pool, size, 512);
|
|
417 if (b->start == NULL) {
|
|
418 return NGX_ERROR;
|
|
419 }
|
|
420
|
|
421 } else {
|
|
422 b->start = ngx_palloc(ctx->pool, size);
|
|
423 if (b->start == NULL) {
|
|
424 return NGX_ERROR;
|
|
425 }
|
|
426 }
|
|
427
|
|
428 b->pos = b->start;
|
|
429 b->last = b->start;
|
|
430 b->end = b->last + size;
|
|
431 b->temporary = 1;
|
|
432 b->tag = ctx->tag;
|
|
433 b->recycled = recycled;
|
|
434
|
|
435 ctx->buf = b;
|
|
436 ctx->allocated++;
|
|
437
|
|
438 return NGX_OK;
|
|
439 }
|
|
440
|
|
441
|
|
442 static ngx_int_t
|
|
443 ngx_output_chain_copy_buf(ngx_output_chain_ctx_t *ctx)
|
|
444 {
|
|
445 off_t size;
|
|
446 ssize_t n;
|
|
447 ngx_buf_t *src, *dst;
|
|
448 ngx_uint_t sendfile;
|
|
449
|
|
450 src = ctx->in->buf;
|
|
451 dst = ctx->buf;
|
0
|
452
|
|
453 size = ngx_buf_size(src);
|
|
454
|
22
|
455 if (size > dst->end - dst->pos) {
|
0
|
456 size = dst->end - dst->pos;
|
|
457 }
|
|
458
|
412
|
459 sendfile = ctx->sendfile & !ctx->directio;
|
|
460
|
18
|
461 #if (NGX_SENDFILE_LIMIT)
|
|
462
|
|
463 if (src->in_file && src->file_pos >= NGX_SENDFILE_LIMIT) {
|
|
464 sendfile = 0;
|
|
465 }
|
|
466
|
|
467 #endif
|
|
468
|
0
|
469 if (ngx_buf_in_memory(src)) {
|
22
|
470 ngx_memcpy(dst->pos, src->pos, (size_t) size);
|
|
471 src->pos += (size_t) size;
|
|
472 dst->last += (size_t) size;
|
0
|
473
|
4
|
474 if (src->in_file) {
|
|
475
|
|
476 if (sendfile) {
|
|
477 dst->in_file = 1;
|
|
478 dst->file = src->file;
|
|
479 dst->file_pos = src->file_pos;
|
|
480 dst->file_last = src->file_pos + size;
|
|
481
|
|
482 } else {
|
|
483 dst->in_file = 0;
|
|
484 }
|
|
485
|
0
|
486 src->file_pos += size;
|
2
|
487
|
|
488 } else {
|
|
489 dst->in_file = 0;
|
0
|
490 }
|
|
491
|
306
|
492 if (src->pos == src->last) {
|
|
493 dst->flush = src->flush;
|
|
494 dst->last_buf = src->last_buf;
|
0
|
495 }
|
|
496
|
|
497 } else {
|
416
|
498
|
|
499 #if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
500
|
|
501 if (ctx->unaligned) {
|
|
502 if (ngx_directio_off(src->file->fd) == -1) {
|
|
503 ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, ngx_errno,
|
|
504 ngx_directio_off_n " \"%s\" failed",
|
|
505 src->file->name.data);
|
|
506 }
|
|
507 }
|
|
508
|
|
509 #endif
|
|
510
|
22
|
511 n = ngx_read_file(src->file, dst->pos, (size_t) size, src->file_pos);
|
0
|
512
|
416
|
513 #if (NGX_HAVE_ALIGNED_DIRECTIO)
|
|
514
|
|
515 if (ctx->unaligned) {
|
|
516 ngx_err_t err;
|
|
517
|
|
518 err = ngx_errno;
|
|
519
|
|
520 if (ngx_directio_on(src->file->fd) == -1) {
|
|
521 ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, ngx_errno,
|
|
522 ngx_directio_on_n " \"%s\" failed",
|
|
523 src->file->name.data);
|
|
524 }
|
|
525
|
|
526 ngx_set_errno(err);
|
|
527
|
|
528 ctx->unaligned = 0;
|
|
529 }
|
|
530
|
|
531 #endif
|
|
532
|
0
|
533 if (n == NGX_ERROR) {
|
66
|
534 return (ngx_int_t) n;
|
0
|
535 }
|
|
536
|
|
537 #if (NGX_FILE_AIO_READ)
|
|
538 if (n == NGX_AGAIN) {
|
66
|
539 return (ngx_int_t) n;
|
0
|
540 }
|
|
541 #endif
|
|
542
|
22
|
543 if (n != size) {
|
412
|
544 ngx_log_error(NGX_LOG_ALERT, ctx->pool->log, 0,
|
416
|
545 ngx_read_file_n " read only %z of %O from \"%s\"",
|
|
546 n, size, src->file->name.data);
|
0
|
547 if (n == 0) {
|
|
548 return NGX_ERROR;
|
|
549 }
|
|
550 }
|
|
551
|
|
552 dst->last += n;
|
|
553
|
2
|
554 if (sendfile) {
|
|
555 dst->in_file = 1;
|
|
556 dst->file = src->file;
|
|
557 dst->file_pos = src->file_pos;
|
4
|
558 dst->file_last = src->file_pos + n;
|
2
|
559
|
|
560 } else {
|
0
|
561 dst->in_file = 0;
|
|
562 }
|
|
563
|
4
|
564 src->file_pos += n;
|
|
565
|
308
|
566 if (src->file_pos == src->file_last) {
|
306
|
567 dst->flush = src->flush;
|
|
568 dst->last_buf = src->last_buf;
|
0
|
569 }
|
|
570 }
|
|
571
|
|
572 return NGX_OK;
|
|
573 }
|
|
574
|
|
575
|
48
|
576 ngx_int_t
|
|
577 ngx_chain_writer(void *data, ngx_chain_t *in)
|
0
|
578 {
|
|
579 ngx_chain_writer_ctx_t *ctx = data;
|
|
580
|
372
|
581 off_t size;
|
|
582 ngx_chain_t *cl;
|
|
583 ngx_connection_t *c;
|
|
584
|
|
585 c = ctx->connection;
|
0
|
586
|
22
|
587 for (size = 0; in; in = in->next) {
|
|
588
|
|
589 #if 1
|
|
590 if (ngx_buf_size(in->buf) == 0 && !ngx_buf_special(in->buf)) {
|
|
591 ngx_debug_point();
|
|
592 }
|
|
593 #endif
|
|
594
|
|
595 size += ngx_buf_size(in->buf);
|
0
|
596
|
372
|
597 ngx_log_debug2(NGX_LOG_DEBUG_CORE, c->log, 0,
|
306
|
598 "chain writer buf fl:%d s:%uO",
|
|
599 in->buf->flush, ngx_buf_size(in->buf));
|
0
|
600
|
50
|
601 cl = ngx_alloc_chain_link(ctx->pool);
|
|
602 if (cl == NULL) {
|
10
|
603 return NGX_ERROR;
|
|
604 }
|
50
|
605
|
10
|
606 cl->buf = in->buf;
|
|
607 cl->next = NULL;
|
0
|
608 *ctx->last = cl;
|
|
609 ctx->last = &cl->next;
|
|
610 }
|
|
611
|
372
|
612 ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
|
10
|
613 "chain writer in: %p", ctx->out);
|
0
|
614
|
22
|
615 for (cl = ctx->out; cl; cl = cl->next) {
|
|
616
|
|
617 #if 1
|
|
618 if (ngx_buf_size(cl->buf) == 0 && !ngx_buf_special(cl->buf)) {
|
|
619 ngx_debug_point();
|
|
620 }
|
|
621
|
|
622 #endif
|
|
623
|
|
624 size += ngx_buf_size(cl->buf);
|
|
625 }
|
|
626
|
372
|
627 if (size == 0 && !c->buffered) {
|
22
|
628 return NGX_OK;
|
|
629 }
|
|
630
|
372
|
631 ctx->out = c->send_chain(c, ctx->out, ctx->limit);
|
0
|
632
|
372
|
633 ngx_log_debug1(NGX_LOG_DEBUG_CORE, c->log, 0,
|
10
|
634 "chain writer out: %p", ctx->out);
|
0
|
635
|
|
636 if (ctx->out == NGX_CHAIN_ERROR) {
|
|
637 return NGX_ERROR;
|
|
638 }
|
|
639
|
|
640 if (ctx->out == NULL) {
|
|
641 ctx->last = &ctx->out;
|
302
|
642
|
372
|
643 if (!c->buffered) {
|
304
|
644 return NGX_OK;
|
|
645 }
|
0
|
646 }
|
|
647
|
|
648 return NGX_AGAIN;
|
|
649 }
|