2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) Igor Sysoev
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <ngx_config.h>
|
|
|
|
#include <ngx_core.h>
|
|
|
|
#include <ngx_event.h>
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open file cache caches
|
|
|
|
* open file handles with stat() info;
|
|
|
|
* directories stat() info;
|
|
|
|
* files and directories errors: not found, access denied, etc.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
static void ngx_open_file_cache_cleanup(void *data);
|
2007-12-25 07:46:40 -03:00
|
|
|
static ngx_int_t ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of,
|
|
|
|
ngx_log_t *log);
|
|
|
|
static void ngx_open_file_add_event(ngx_open_file_cache_t *cache,
|
|
|
|
ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log);
|
2007-09-01 08:11:21 -04:00
|
|
|
static void ngx_open_file_cleanup(void *data);
|
|
|
|
static void ngx_close_cached_file(ngx_open_file_cache_t *cache,
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log);
|
2007-12-25 07:46:40 -03:00
|
|
|
static void ngx_open_file_del_event(ngx_cached_open_file_t *file);
|
2007-09-01 08:11:21 -04:00
|
|
|
static void ngx_expire_old_cached_files(ngx_open_file_cache_t *cache,
|
|
|
|
ngx_uint_t n, ngx_log_t *log);
|
|
|
|
static void ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
|
2007-12-25 07:46:40 -03:00
|
|
|
static ngx_cached_open_file_t *
|
|
|
|
ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
|
|
|
|
uint32_t hash);
|
2007-09-01 08:11:21 -04:00
|
|
|
static void ngx_open_file_cache_remove(ngx_event_t *ev);
|
|
|
|
|
|
|
|
|
|
|
|
ngx_open_file_cache_t *
|
|
|
|
ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive)
|
|
|
|
{
|
|
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
ngx_open_file_cache_t *cache;
|
|
|
|
|
|
|
|
cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t));
|
|
|
|
if (cache == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-12-20 18:29:52 -03:00
|
|
|
ngx_rbtree_init(&cache->rbtree, &cache->sentinel,
|
2007-12-17 05:52:00 -03:00
|
|
|
ngx_open_file_cache_rbtree_insert_value);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_init(&cache->expire_queue);
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
cache->current = 0;
|
|
|
|
cache->max = max;
|
|
|
|
cache->inactive = inactive;
|
|
|
|
|
|
|
|
cln = ngx_pool_cleanup_add(pool, 0);
|
|
|
|
if (cln == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
cln->handler = ngx_open_file_cache_cleanup;
|
|
|
|
cln->data = cache;
|
|
|
|
|
|
|
|
return cache;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_open_file_cache_cleanup(void *data)
|
|
|
|
{
|
|
|
|
ngx_open_file_cache_t *cache = data;
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_t *q;
|
2007-09-01 08:11:21 -04:00
|
|
|
ngx_cached_open_file_t *file;
|
|
|
|
|
|
|
|
ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
|
|
"open file cache cleanup");
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
if (ngx_queue_empty(&cache->expire_queue)) {
|
2007-12-21 13:19:14 -03:00
|
|
|
break;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
q = ngx_queue_last(&cache->expire_queue);
|
|
|
|
|
|
|
|
file = ngx_queue_data(q, ngx_cached_open_file_t, queue);
|
|
|
|
|
|
|
|
ngx_queue_remove(q);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
ngx_rbtree_delete(&cache->rbtree, &file->node);
|
|
|
|
|
|
|
|
cache->current--;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
|
|
|
|
"delete cached open file: %s", file->name);
|
|
|
|
|
|
|
|
if (!file->err && !file->is_dir) {
|
|
|
|
file->close = 1;
|
|
|
|
file->count = 0;
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_close_cached_file(cache, file, 0, ngx_cycle->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_free(file->name);
|
|
|
|
ngx_free(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cache->current) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
|
|
"%d items still leave in open file cache",
|
|
|
|
cache->current);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cache->rbtree.root != cache->rbtree.sentinel) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
|
|
|
|
"rbtree still is not empty in open file cache");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ngx_int_t
|
|
|
|
ngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,
|
|
|
|
ngx_open_file_info_t *of, ngx_pool_t *pool)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
uint32_t hash;
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_pool_cleanup_t *cln;
|
|
|
|
ngx_cached_open_file_t *file;
|
|
|
|
ngx_pool_cleanup_file_t *clnf;
|
|
|
|
ngx_open_file_cache_cleanup_t *ofcln;
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
of->fd = NGX_INVALID_FILE;
|
2007-09-01 08:11:21 -04:00
|
|
|
of->err = 0;
|
|
|
|
|
|
|
|
if (cache == NULL) {
|
|
|
|
|
|
|
|
cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
|
|
|
|
if (cln == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ngx_open_and_stat_file(name->data, of, pool->log);
|
|
|
|
|
|
|
|
if (rc == NGX_OK && !of->is_dir) {
|
|
|
|
cln->handler = ngx_pool_cleanup_file;
|
|
|
|
clnf = cln->data;
|
|
|
|
|
|
|
|
clnf->fd = of->fd;
|
|
|
|
clnf->name = name->data;
|
|
|
|
clnf->log = pool->log;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));
|
|
|
|
if (cln == NULL) {
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
now = ngx_time();
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
hash = ngx_crc32_long(name->data, name->len);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
file = ngx_open_file_lookup(cache, name, hash);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
file->uses++;
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2008-04-29 14:14:45 -04:00
|
|
|
ngx_queue_remove(&file->queue);
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/* file was not used often enough to keep open */
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
rc = ngx_open_and_stat_file(name->data, of, pool->log);
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
goto add_event;
|
|
|
|
}
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if ((file->event && file->use_event)
|
2008-06-23 09:35:34 -04:00
|
|
|
|| (file->event == NULL
|
|
|
|
&& (of->uniq == 0 || of->uniq == file->uniq)
|
|
|
|
&& now - file->created < of->valid))
|
2007-12-25 07:46:40 -03:00
|
|
|
{
|
|
|
|
if (file->err == 0) {
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
of->fd = file->fd;
|
|
|
|
of->uniq = file->uniq;
|
|
|
|
of->mtime = file->mtime;
|
|
|
|
of->size = file->size;
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
of->is_dir = file->is_dir;
|
|
|
|
of->is_file = file->is_file;
|
|
|
|
of->is_link = file->is_link;
|
|
|
|
of->is_exec = file->is_exec;
|
2007-12-22 10:19:39 -03:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (!file->is_dir) {
|
|
|
|
file->count++;
|
|
|
|
ngx_open_file_add_event(cache, file, of, pool->log);
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
} else {
|
|
|
|
of->err = file->err;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
goto found;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,
|
|
|
|
"retest open file: %s, fd:%d, c:%d, e:%d",
|
|
|
|
file->name, file->fd, file->count, file->err);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->is_dir) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/*
|
|
|
|
* chances that directory became file are very small
|
|
|
|
* so test_dir flag allows to use a single syscall
|
|
|
|
* in ngx_file_info() instead of three syscalls
|
|
|
|
*/
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
of->test_dir = 1;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
of->fd = file->fd;
|
|
|
|
of->uniq = file->uniq;
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
rc = ngx_open_and_stat_file(name->data, of, pool->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (of->is_dir) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->is_dir || file->err) {
|
|
|
|
goto update;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/* file became directory */
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
} else if (of->err == 0) { /* file */
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->is_dir || file->err) {
|
|
|
|
goto add_event;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (of->uniq == file->uniq
|
|
|
|
&& of->mtime == file->mtime
|
|
|
|
&& of->size == file->size)
|
|
|
|
{
|
2008-06-26 12:10:13 -04:00
|
|
|
if (of->fd != file->fd) {
|
|
|
|
if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed",
|
|
|
|
name->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
of->fd = file->fd;
|
2007-12-25 07:46:40 -03:00
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
file->count++;
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->event) {
|
|
|
|
file->use_event = 1;
|
|
|
|
goto renew;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_open_file_add_event(cache, file, of, pool->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
goto renew;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/* file was changed */
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
} else { /* error to cache */
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->err || file->is_dir) {
|
|
|
|
goto update;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/* file was removed, etc. */
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (file->count == 0) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_open_file_del_event(file);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed",
|
|
|
|
name->data);
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
goto add_event;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_rbtree_delete(&cache->rbtree, &file->node);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
cache->current--;
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
file->close = 1;
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
goto create;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* not found */
|
|
|
|
|
|
|
|
rc = ngx_open_and_stat_file(name->data, of, pool->log);
|
|
|
|
|
|
|
|
if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
create:
|
|
|
|
|
|
|
|
if (cache->current >= cache->max) {
|
|
|
|
ngx_expire_old_cached_files(cache, 0, pool->log);
|
|
|
|
}
|
|
|
|
|
|
|
|
file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);
|
|
|
|
|
|
|
|
if (file == NULL) {
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->name = ngx_alloc(name->len + 1, pool->log);
|
|
|
|
|
|
|
|
if (file->name == NULL) {
|
|
|
|
ngx_free(file);
|
|
|
|
file = NULL;
|
|
|
|
goto failed;
|
|
|
|
}
|
|
|
|
|
|
|
|
ngx_cpystrn(file->name, name->data, name->len + 1);
|
|
|
|
|
|
|
|
file->node.key = hash;
|
|
|
|
|
|
|
|
ngx_rbtree_insert(&cache->rbtree, &file->node);
|
|
|
|
|
|
|
|
cache->current++;
|
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
file->uses = 1;
|
2007-12-25 07:46:40 -03:00
|
|
|
file->count = 0;
|
|
|
|
file->use_event = 0;
|
|
|
|
file->event = NULL;
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
add_event:
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_open_file_add_event(cache, file, of, pool->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
update:
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
file->fd = of->fd;
|
|
|
|
file->err = of->err;
|
|
|
|
|
|
|
|
if (of->err == 0) {
|
|
|
|
file->uniq = of->uniq;
|
|
|
|
file->mtime = of->mtime;
|
|
|
|
file->size = of->size;
|
|
|
|
|
|
|
|
file->close = 0;
|
|
|
|
|
|
|
|
file->is_dir = of->is_dir;
|
|
|
|
file->is_file = of->is_file;
|
|
|
|
file->is_link = of->is_link;
|
|
|
|
file->is_exec = of->is_exec;
|
|
|
|
|
|
|
|
if (!of->is_dir) {
|
|
|
|
file->count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
renew:
|
|
|
|
|
|
|
|
file->created = now;
|
|
|
|
|
|
|
|
found:
|
|
|
|
|
|
|
|
file->accessed = now;
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_insert_head(&cache->expire_queue, &file->queue);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0,
|
|
|
|
"cached open file: %s, fd:%d, c:%d, e:%d, u:%d",
|
|
|
|
file->name, file->fd, file->count, file->err, file->uses);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
if (of->err == 0) {
|
|
|
|
|
|
|
|
if (!of->is_dir) {
|
|
|
|
cln->handler = ngx_open_file_cleanup;
|
|
|
|
ofcln = cln->data;
|
|
|
|
|
|
|
|
ofcln->cache = cache;
|
|
|
|
ofcln->file = file;
|
2007-12-22 10:19:39 -03:00
|
|
|
ofcln->min_uses = of->min_uses;
|
2007-09-01 08:11:21 -04:00
|
|
|
ofcln->log = pool->log;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
|
|
|
|
failed:
|
|
|
|
|
2008-04-29 14:15:23 -04:00
|
|
|
if (file) {
|
2007-09-01 08:11:21 -04:00
|
|
|
ngx_rbtree_delete(&cache->rbtree, &file->node);
|
|
|
|
|
|
|
|
cache->current--;
|
|
|
|
|
2008-04-29 14:15:23 -04:00
|
|
|
if (file->count == 0) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2008-05-14 03:54:52 -04:00
|
|
|
if (file->fd != NGX_INVALID_FILE) {
|
|
|
|
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed",
|
2008-04-29 14:15:23 -04:00
|
|
|
file->name);
|
2008-05-14 03:54:52 -04:00
|
|
|
}
|
|
|
|
}
|
2008-04-29 14:15:23 -04:00
|
|
|
|
|
|
|
ngx_free(file->name);
|
|
|
|
ngx_free(file);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
file->close = 1;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (of->fd != NGX_INVALID_FILE) {
|
|
|
|
if (ngx_close_file(of->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name->data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static ngx_int_t
|
|
|
|
ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
ngx_fd_t fd;
|
|
|
|
ngx_file_info_t fi;
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
if (of->fd != NGX_INVALID_FILE || of->test_dir) {
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
if (ngx_file_info(name, &fi) == -1) {
|
2008-06-26 12:10:13 -04:00
|
|
|
goto failed;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
if (of->fd != NGX_INVALID_FILE && of->uniq == ngx_file_uniq(&fi)) {
|
|
|
|
goto done;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
if (of->test_dir && of->is_dir) {
|
|
|
|
goto done;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0);
|
|
|
|
|
|
|
|
if (fd == NGX_INVALID_FILE) {
|
2008-06-26 12:10:13 -04:00
|
|
|
goto failed;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_CRIT, log, ngx_errno,
|
|
|
|
ngx_fd_info_n " \"%s\" failed", name);
|
|
|
|
|
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name);
|
|
|
|
}
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
of->fd = NGX_INVALID_FILE;
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
return NGX_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ngx_is_dir(&fi)) {
|
|
|
|
if (ngx_close_file(fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", name);
|
|
|
|
}
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
of->fd = NGX_INVALID_FILE;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
of->fd = fd;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
2008-06-26 12:10:13 -04:00
|
|
|
done:
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
of->uniq = ngx_file_uniq(&fi);
|
|
|
|
of->mtime = ngx_file_mtime(&fi);
|
|
|
|
of->size = ngx_file_size(&fi);
|
|
|
|
of->is_dir = ngx_is_dir(&fi);
|
|
|
|
of->is_file = ngx_is_file(&fi);
|
|
|
|
of->is_link = ngx_is_link(&fi);
|
|
|
|
of->is_exec = ngx_is_exec(&fi);
|
|
|
|
|
|
|
|
return NGX_OK;
|
2008-06-26 12:10:13 -04:00
|
|
|
|
|
|
|
failed:
|
|
|
|
|
|
|
|
of->fd = NGX_INVALID_FILE;
|
|
|
|
of->err = ngx_errno;
|
|
|
|
|
|
|
|
return NGX_ERROR;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
/*
|
|
|
|
* we ignore any possible event setting error and
|
|
|
|
* fallback to usual periodic file retests
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_open_file_add_event(ngx_open_file_cache_t *cache,
|
|
|
|
ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log)
|
|
|
|
{
|
|
|
|
ngx_open_file_cache_event_t *fev;
|
|
|
|
|
|
|
|
if (!(ngx_event_flags & NGX_USE_VNODE_EVENT)
|
|
|
|
|| !of->events
|
|
|
|
|| file->event
|
|
|
|
|| of->fd == NGX_INVALID_FILE
|
|
|
|
|| file->uses < of->min_uses)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
file->event = ngx_calloc(sizeof(ngx_event_t), log);
|
|
|
|
if (file->event== NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log);
|
|
|
|
if (fev == NULL) {
|
|
|
|
ngx_free(file->event);
|
|
|
|
file->event = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
fev->fd = of->fd;
|
|
|
|
fev->file = file;
|
|
|
|
fev->cache = cache;
|
|
|
|
|
|
|
|
file->event->handler = ngx_open_file_cache_remove;
|
|
|
|
file->event->data = fev;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* although vnode event may be called while ngx_cycle->poll
|
|
|
|
* destruction, however, cleanup procedures are run before any
|
|
|
|
* memory freeing and events will be canceled.
|
|
|
|
*/
|
|
|
|
|
|
|
|
file->event->log = ngx_cycle->log;
|
|
|
|
|
|
|
|
if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT)
|
|
|
|
!= NGX_OK)
|
|
|
|
{
|
|
|
|
ngx_free(file->event->data);
|
|
|
|
ngx_free(file->event);
|
|
|
|
file->event = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* we do not file->use_event here because there may be a race
|
|
|
|
* condition between opening file and adding event, so we rely
|
|
|
|
* upon event notification only after first file revalidation
|
|
|
|
*/
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
static void
|
|
|
|
ngx_open_file_cleanup(void *data)
|
|
|
|
{
|
|
|
|
ngx_open_file_cache_cleanup_t *c = data;
|
|
|
|
|
|
|
|
c->file->count--;
|
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
/* drop one or two expired open files */
|
|
|
|
ngx_expire_old_cached_files(c->cache, 1, c->log);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_close_cached_file(ngx_open_file_cache_t *cache,
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log)
|
2007-09-01 08:11:21 -04:00
|
|
|
{
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0,
|
|
|
|
"close cached open file: %s, fd:%d, c:%d, u:%d, %d",
|
|
|
|
file->name, file->fd, file->count, file->uses, file->close);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
if (!file->close) {
|
|
|
|
|
|
|
|
file->accessed = ngx_time();
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_remove(&file->queue);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_insert_head(&cache->expire_queue, &file->queue);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
if (file->uses >= min_uses || file->count) {
|
|
|
|
return;
|
|
|
|
}
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
ngx_open_file_del_event(file);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
if (file->count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
if (file->fd != NGX_INVALID_FILE) {
|
|
|
|
|
|
|
|
if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
|
|
|
|
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
|
|
|
|
ngx_close_file_n " \"%s\" failed", file->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
file->fd = NGX_INVALID_FILE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file->close) {
|
|
|
|
return;
|
2007-09-01 08:11:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ngx_free(file->name);
|
|
|
|
ngx_free(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
static void
|
|
|
|
ngx_open_file_del_event(ngx_cached_open_file_t *file)
|
|
|
|
{
|
|
|
|
if (file->event == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(void) ngx_del_event(file->event, NGX_VNODE_EVENT,
|
|
|
|
file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT);
|
|
|
|
|
|
|
|
ngx_free(file->event->data);
|
|
|
|
ngx_free(file->event);
|
|
|
|
file->event = NULL;
|
|
|
|
file->use_event = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
static void
|
|
|
|
ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n,
|
|
|
|
ngx_log_t *log)
|
|
|
|
{
|
|
|
|
time_t now;
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_t *q;
|
2007-09-01 08:11:21 -04:00
|
|
|
ngx_cached_open_file_t *file;
|
|
|
|
|
|
|
|
now = ngx_time();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* n == 1 deletes one or two inactive files
|
|
|
|
* n == 0 deletes least recently used file by force
|
|
|
|
* and one or two inactive files
|
|
|
|
*/
|
|
|
|
|
|
|
|
while (n < 3) {
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
if (ngx_queue_empty(&cache->expire_queue)) {
|
2007-09-01 08:11:21 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
q = ngx_queue_last(&cache->expire_queue);
|
|
|
|
|
|
|
|
file = ngx_queue_data(q, ngx_cached_open_file_t, queue);
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
if (n++ != 0 && now - file->accessed <= cache->inactive) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_remove(q);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
ngx_rbtree_delete(&cache->rbtree, &file->node);
|
|
|
|
|
|
|
|
cache->current--;
|
|
|
|
|
|
|
|
ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0,
|
|
|
|
"expire cached open file: %s", file->name);
|
|
|
|
|
|
|
|
if (!file->err && !file->is_dir) {
|
|
|
|
file->close = 1;
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_close_cached_file(cache, file, 0, log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
} else {
|
|
|
|
ngx_free(file->name);
|
|
|
|
ngx_free(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
|
|
|
|
ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
|
|
|
|
{
|
|
|
|
ngx_rbtree_node_t **p;
|
|
|
|
ngx_cached_open_file_t *file, *file_temp;
|
|
|
|
|
|
|
|
for ( ;; ) {
|
|
|
|
|
|
|
|
if (node->key < temp->key) {
|
|
|
|
|
|
|
|
p = &temp->left;
|
|
|
|
|
|
|
|
} else if (node->key > temp->key) {
|
|
|
|
|
|
|
|
p = &temp->right;
|
|
|
|
|
|
|
|
} else { /* node->key == temp->key */
|
|
|
|
|
|
|
|
file = (ngx_cached_open_file_t *) node;
|
|
|
|
file_temp = (ngx_cached_open_file_t *) temp;
|
|
|
|
|
|
|
|
p = (ngx_strcmp(file->name, file_temp->name) < 0)
|
|
|
|
? &temp->left : &temp->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*p == sentinel) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
temp = *p;
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = node;
|
|
|
|
node->parent = temp;
|
|
|
|
node->left = sentinel;
|
|
|
|
node->right = sentinel;
|
|
|
|
ngx_rbt_red(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-12-25 07:46:40 -03:00
|
|
|
static ngx_cached_open_file_t *
|
|
|
|
ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
|
|
|
|
uint32_t hash)
|
|
|
|
{
|
|
|
|
ngx_int_t rc;
|
|
|
|
ngx_rbtree_node_t *node, *sentinel;
|
|
|
|
ngx_cached_open_file_t *file;
|
|
|
|
|
|
|
|
node = cache->rbtree.root;
|
|
|
|
sentinel = cache->rbtree.sentinel;
|
|
|
|
|
|
|
|
while (node != sentinel) {
|
|
|
|
|
|
|
|
if (hash < node->key) {
|
|
|
|
node = node->left;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hash > node->key) {
|
|
|
|
node = node->right;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* hash == node->key */
|
|
|
|
|
|
|
|
do {
|
|
|
|
file = (ngx_cached_open_file_t *) node;
|
|
|
|
|
|
|
|
rc = ngx_strcmp(name->data, file->name);
|
|
|
|
|
|
|
|
if (rc == 0) {
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = (rc < 0) ? node->left : node->right;
|
|
|
|
|
|
|
|
} while (node != sentinel && hash == node->key);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-09-01 08:11:21 -04:00
|
|
|
static void
|
|
|
|
ngx_open_file_cache_remove(ngx_event_t *ev)
|
|
|
|
{
|
|
|
|
ngx_cached_open_file_t *file;
|
|
|
|
ngx_open_file_cache_event_t *fev;
|
|
|
|
|
|
|
|
fev = ev->data;
|
|
|
|
file = fev->file;
|
|
|
|
|
2007-12-21 12:33:15 -03:00
|
|
|
ngx_queue_remove(&file->queue);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
ngx_rbtree_delete(&fev->cache->rbtree, &file->node);
|
|
|
|
|
|
|
|
fev->cache->current--;
|
|
|
|
|
|
|
|
/* NGX_ONESHOT_EVENT was already deleted */
|
|
|
|
file->event = NULL;
|
|
|
|
|
|
|
|
file->close = 1;
|
|
|
|
|
2007-12-22 10:19:39 -03:00
|
|
|
ngx_close_cached_file(fev->cache, file, 0, ev->log);
|
2007-09-01 08:11:21 -04:00
|
|
|
|
|
|
|
/* free memory only when fev->cache and fev->file are already not needed */
|
|
|
|
|
|
|
|
ngx_free(ev->data);
|
|
|
|
ngx_free(ev);
|
|
|
|
}
|