comparison src/http/ngx_http_file_cache.c @ 578:f3a9e57d2e17

Merge with current.
author Maxim Dounin <mdounin@mdounin.ru>
date Thu, 11 Mar 2010 21:27:17 +0300
parents da3c99095432
children be4f34123024
comparison
equal deleted inserted replaced
539:5f4de8cf0d9d 578:f3a9e57d2e17
9 #include <ngx_http.h> 9 #include <ngx_http.h>
10 #include <ngx_md5.h> 10 #include <ngx_md5.h>
11 11
12 12
13 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r, 13 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
14 ngx_http_cache_t *c);
15 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r,
14 ngx_http_cache_t *c); 16 ngx_http_cache_t *c);
15 #if (NGX_HAVE_FILE_AIO) 17 #if (NGX_HAVE_FILE_AIO)
16 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev); 18 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev);
17 #endif 19 #endif
18 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, 20 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache,
279 of.uniq = c->uniq; 281 of.uniq = c->uniq;
280 of.valid = clcf->open_file_cache_valid; 282 of.valid = clcf->open_file_cache_valid;
281 of.min_uses = clcf->open_file_cache_min_uses; 283 of.min_uses = clcf->open_file_cache_min_uses;
282 of.events = clcf->open_file_cache_events; 284 of.events = clcf->open_file_cache_events;
283 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF; 285 of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
286 of.read_ahead = clcf->read_ahead;
284 287
285 if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool) 288 if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
286 != NGX_OK) 289 != NGX_OK)
287 { 290 {
288 switch (of.err) { 291 switch (of.err) {
327 ngx_http_file_cache_t *cache; 330 ngx_http_file_cache_t *cache;
328 ngx_http_file_cache_header_t *h; 331 ngx_http_file_cache_header_t *h;
329 332
330 c = r->cache; 333 c = r->cache;
331 334
332 #if (NGX_HAVE_FILE_AIO) 335 n = ngx_http_file_cache_aio_read(r, c);
333 { 336
334 ngx_http_core_loc_conf_t *clcf; 337 if (n < 0) {
335
336 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
337
338 if (clcf->aio) {
339 n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
340
341 if (n == NGX_AGAIN) {
342 c->file.aio->data = r;
343 c->file.aio->handler = ngx_http_cache_aio_event_handler;
344
345 r->main->blocked++;
346 r->aio = 1;
347
348 return NGX_AGAIN;
349 }
350
351 } else {
352 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
353 }
354 }
355 #else
356
357 n = ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
358
359 #endif
360
361 if (n == NGX_ERROR) {
362 return n; 338 return n;
363 } 339 }
364 340
365 if ((size_t) n <= c->header_start) { 341 if ((size_t) n < c->header_start) {
366 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, 342 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
367 "cache file \"%s\" is too small", c->file.name.data); 343 "cache file \"%s\" is too small", c->file.name.data);
368 return NGX_ERROR; 344 return NGX_ERROR;
369 } 345 }
370 346
371 h = (ngx_http_file_cache_header_t *) c->buf->pos; 347 h = (ngx_http_file_cache_header_t *) c->buf->pos;
372 348
373 if (h->crc32 != c->crc32 || (size_t) h->header_start != c->header_start) { 349 if (h->crc32 != c->crc32) {
374 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0, 350 ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
375 "cache file \"%s\" has md5 collision", c->file.name.data); 351 "cache file \"%s\" has md5 collision", c->file.name.data);
376 return NGX_DECLINED; 352 return NGX_DECLINED;
377 } 353 }
378 354
380 356
381 c->valid_sec = h->valid_sec; 357 c->valid_sec = h->valid_sec;
382 c->last_modified = h->last_modified; 358 c->last_modified = h->last_modified;
383 c->date = h->date; 359 c->date = h->date;
384 c->valid_msec = h->valid_msec; 360 c->valid_msec = h->valid_msec;
361 c->header_start = h->header_start;
385 c->body_start = h->body_start; 362 c->body_start = h->body_start;
386 363
387 r->cached = 1; 364 r->cached = 1;
388 365
389 cache = c->file_cache; 366 cache = c->file_cache;
429 406
430 return NGX_OK; 407 return NGX_OK;
431 } 408 }
432 409
433 410
411 static ssize_t
412 ngx_http_file_cache_aio_read(ngx_http_request_t *r, ngx_http_cache_t *c)
413 {
434 #if (NGX_HAVE_FILE_AIO) 414 #if (NGX_HAVE_FILE_AIO)
435 415 ssize_t n;
416 ngx_http_core_loc_conf_t *clcf;
417
418 if (!ngx_file_aio) {
419 goto noaio;
420 }
421
422 clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
423
424 if (!clcf->aio) {
425 goto noaio;
426 }
427
428 n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
429
430 if (n != NGX_AGAIN) {
431 return n;
432 }
433
434 c->file.aio->data = r;
435 c->file.aio->handler = ngx_http_cache_aio_event_handler;
436
437 r->main->blocked++;
438 r->aio = 1;
439
440 return NGX_AGAIN;
441
442 noaio:
443
444 #endif
445
446 return ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
447 }
448
449
450 #if (NGX_HAVE_FILE_AIO)
436 451
437 static void 452 static void
438 ngx_http_cache_aio_event_handler(ngx_event_t *ev) 453 ngx_http_cache_aio_event_handler(ngx_event_t *ev)
439 { 454 {
440 ngx_event_aio_t *aio; 455 ngx_event_aio_t *aio;
689 704
690 705
691 void 706 void
692 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf) 707 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
693 { 708 {
694 off_t size; 709 off_t size, length;
695 ngx_int_t rc; 710 ngx_int_t rc;
696 ngx_file_uniq_t uniq; 711 ngx_file_uniq_t uniq;
697 ngx_file_info_t fi; 712 ngx_file_info_t fi;
698 ngx_http_cache_t *c; 713 ngx_http_cache_t *c;
699 ngx_ext_rename_file_t ext; 714 ngx_ext_rename_file_t ext;
711 c->updated = 1; 726 c->updated = 1;
712 727
713 cache = c->file_cache; 728 cache = c->file_cache;
714 729
715 uniq = 0; 730 uniq = 0;
731 length = 0;
716 732
717 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0, 733 ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
718 "http file cache rename: \"%s\" to \"%s\"", 734 "http file cache rename: \"%s\" to \"%s\"",
719 tf->file.name.data, c->file.name.data); 735 tf->file.name.data, c->file.name.data);
720 736
735 751
736 rc = NGX_ERROR; 752 rc = NGX_ERROR;
737 753
738 } else { 754 } else {
739 uniq = ngx_file_uniq(&fi); 755 uniq = ngx_file_uniq(&fi);
740 } 756 length = ngx_file_size(&fi);
741 } 757 }
742 758 }
743 size = (c->length + cache->bsize - 1) / cache->bsize; 759
760 size = (length + cache->bsize - 1) / cache->bsize;
744 761
745 ngx_shmtx_lock(&cache->shpool->mutex); 762 ngx_shmtx_lock(&cache->shpool->mutex);
746 763
747 c->node->count--; 764 c->node->count--;
748 c->node->uniq = uniq; 765 c->node->uniq = uniq;
749 c->node->body_start = c->body_start; 766 c->node->body_start = c->body_start;
750 767
751 size = size - (c->node->length + cache->bsize - 1) / cache->bsize; 768 size = size - (c->node->length + cache->bsize - 1) / cache->bsize;
752 769
753 c->node->length = c->length; 770 c->node->length = length;
754 771
755 cache->sh->size += size; 772 cache->sh->size += size;
756 773
757 if (rc == NGX_OK) { 774 if (rc == NGX_OK) {
758 c->node->exists = 1; 775 c->node->exists = 1;
787 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t)); 804 b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
788 if (b->file == NULL) { 805 if (b->file == NULL) {
789 return NGX_HTTP_INTERNAL_SERVER_ERROR; 806 return NGX_HTTP_INTERNAL_SERVER_ERROR;
790 } 807 }
791 808
809 r->header_only = (c->length - c->body_start) == 0;
810
792 rc = ngx_http_send_header(r); 811 rc = ngx_http_send_header(r);
793 812
794 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { 813 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
795 return rc; 814 return rc;
796 } 815 }
797 816
798 b->file_pos = c->body_start; 817 b->file_pos = c->body_start;
799 b->file_last = c->length; 818 b->file_last = c->length;
800 819
801 b->in_file = (c->length - c->body_start) ? 1: 0; 820 b->in_file = 1;
802 b->last_buf = (r == r->main) ? 1: 0; 821 b->last_buf = (r == r->main) ? 1: 0;
803 b->last_in_chain = 1; 822 b->last_in_chain = 1;
804 823
805 b->file->fd = c->file.fd; 824 b->file->fd = c->file.fd;
806 b->file->name = c->file.name; 825 b->file->name = c->file.name;