1e18a033bSKonstantin Ananyev
2e18a033bSKonstantin Ananyev/*
3e18a033bSKonstantin Ananyev * Copyright (C) Igor Sysoev
4e18a033bSKonstantin Ananyev * Copyright (C) Nginx, Inc.
5e18a033bSKonstantin Ananyev */
6e18a033bSKonstantin Ananyev
7e18a033bSKonstantin Ananyev
8e18a033bSKonstantin Ananyev#include <ngx_config.h>
9e18a033bSKonstantin Ananyev#include <ngx_core.h>
10e18a033bSKonstantin Ananyev#include <ngx_event.h>
11e18a033bSKonstantin Ananyev
12e18a033bSKonstantin Ananyev
13e18a033bSKonstantin Ananyev/*
14e18a033bSKonstantin Ananyev * open file cache caches
15e18a033bSKonstantin Ananyev *    open file handles with stat() info;
16e18a033bSKonstantin Ananyev *    directories stat() info;
17e18a033bSKonstantin Ananyev *    files and directories errors: not found, access denied, etc.
18e18a033bSKonstantin Ananyev */
19e18a033bSKonstantin Ananyev
20e18a033bSKonstantin Ananyev
21e18a033bSKonstantin Ananyev#define NGX_MIN_READ_AHEAD  (128 * 1024)
22e18a033bSKonstantin Ananyev
23e18a033bSKonstantin Ananyev
24e18a033bSKonstantin Ananyevstatic void ngx_open_file_cache_cleanup(void *data);
25e18a033bSKonstantin Ananyev#if (NGX_HAVE_OPENAT)
26e18a033bSKonstantin Ananyevstatic ngx_fd_t ngx_openat_file_owner(ngx_fd_t at_fd, const u_char *name,
27e18a033bSKonstantin Ananyev    ngx_int_t mode, ngx_int_t create, ngx_int_t access, ngx_log_t *log);
28e18a033bSKonstantin Ananyev#if (NGX_HAVE_O_PATH)
29e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_file_o_path_info(ngx_fd_t fd, ngx_file_info_t *fi,
30e18a033bSKonstantin Ananyev    ngx_log_t *log);
31e18a033bSKonstantin Ananyev#endif
32e18a033bSKonstantin Ananyev#endif
33e18a033bSKonstantin Ananyevstatic ngx_fd_t ngx_open_file_wrapper(ngx_str_t *name,
34e18a033bSKonstantin Ananyev    ngx_open_file_info_t *of, ngx_int_t mode, ngx_int_t create,
35e18a033bSKonstantin Ananyev    ngx_int_t access, ngx_log_t *log);
36e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_file_info_wrapper(ngx_str_t *name,
37e18a033bSKonstantin Ananyev    ngx_open_file_info_t *of, ngx_file_info_t *fi, ngx_log_t *log);
38e18a033bSKonstantin Ananyevstatic ngx_int_t ngx_open_and_stat_file(ngx_str_t *name,
39e18a033bSKonstantin Ananyev    ngx_open_file_info_t *of, ngx_log_t *log);
40e18a033bSKonstantin Ananyevstatic void ngx_open_file_add_event(ngx_open_file_cache_t *cache,
41e18a033bSKonstantin Ananyev    ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log);
42e18a033bSKonstantin Ananyevstatic void ngx_open_file_cleanup(void *data);
43e18a033bSKonstantin Ananyevstatic void ngx_close_cached_file(ngx_open_file_cache_t *cache,
44e18a033bSKonstantin Ananyev    ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log);
45e18a033bSKonstantin Ananyevstatic void ngx_open_file_del_event(ngx_cached_open_file_t *file);
46e18a033bSKonstantin Ananyevstatic void ngx_expire_old_cached_files(ngx_open_file_cache_t *cache,
47e18a033bSKonstantin Ananyev    ngx_uint_t n, ngx_log_t *log);
48e18a033bSKonstantin Ananyevstatic void ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
49e18a033bSKonstantin Ananyev    ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
50e18a033bSKonstantin Ananyevstatic ngx_cached_open_file_t *
51e18a033bSKonstantin Ananyev    ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name,
52e18a033bSKonstantin Ananyev    uint32_t hash);
53e18a033bSKonstantin Ananyevstatic void ngx_open_file_cache_remove(ngx_event_t *ev);
54e18a033bSKonstantin Ananyev
55e18a033bSKonstantin Ananyev
56e18a033bSKonstantin Ananyevngx_open_file_cache_t *
57e18a033bSKonstantin Ananyevngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive)
58e18a033bSKonstantin Ananyev{
59e18a033bSKonstantin Ananyev    ngx_pool_cleanup_t     *cln;
60e18a033bSKonstantin Ananyev    ngx_open_file_cache_t  *cache;
61e18a033bSKonstantin Ananyev
62e18a033bSKonstantin Ananyev    cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t));
63e18a033bSKonstantin Ananyev    if (cache == NULL) {
64e18a033bSKonstantin Ananyev        return NULL;
65e18a033bSKonstantin Ananyev    }
66e18a033bSKonstantin Ananyev
67e18a033bSKonstantin Ananyev    ngx_rbtree_init(&cache->rbtree, &cache->sentinel,
68e18a033bSKonstantin Ananyev                    ngx_open_file_cache_rbtree_insert_value);
69e18a033bSKonstantin Ananyev
70e18a033bSKonstantin Ananyev    ngx_queue_init(&cache->expire_queue);
71e18a033bSKonstantin Ananyev
72e18a033bSKonstantin Ananyev    cache->current = 0;
73e18a033bSKonstantin Ananyev    cache->max = max;
74e18a033bSKonstantin Ananyev    cache->inactive = inactive;
75e18a033bSKonstantin Ananyev
76e18a033bSKonstantin Ananyev    cln = ngx_pool_cleanup_add(pool, 0);
77e18a033bSKonstantin Ananyev    if (cln == NULL) {
78e18a033bSKonstantin Ananyev        return NULL;
79e18a033bSKonstantin Ananyev    }
80e18a033bSKonstantin Ananyev
81e18a033bSKonstantin Ananyev    cln->handler = ngx_open_file_cache_cleanup;
82e18a033bSKonstantin Ananyev    cln->data = cache;
83e18a033bSKonstantin Ananyev
84e18a033bSKonstantin Ananyev    return cache;
85e18a033bSKonstantin Ananyev}
86e18a033bSKonstantin Ananyev
87e18a033bSKonstantin Ananyev
88e18a033bSKonstantin Ananyevstatic void
89e18a033bSKonstantin Ananyevngx_open_file_cache_cleanup(void *data)
90e18a033bSKonstantin Ananyev{
91e18a033bSKonstantin Ananyev    ngx_open_file_cache_t  *cache = data;
92e18a033bSKonstantin Ananyev
93e18a033bSKonstantin Ananyev    ngx_queue_t             *q;
94e18a033bSKonstantin Ananyev    ngx_cached_open_file_t  *file;
95e18a033bSKonstantin Ananyev
96e18a033bSKonstantin Ananyev    ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
97e18a033bSKonstantin Ananyev                   "open file cache cleanup");
98e18a033bSKonstantin Ananyev
99e18a033bSKonstantin Ananyev    for ( ;; ) {
100e18a033bSKonstantin Ananyev
101e18a033bSKonstantin Ananyev        if (ngx_queue_empty(&cache->expire_queue)) {
102e18a033bSKonstantin Ananyev            break;
103e18a033bSKonstantin Ananyev        }
104e18a033bSKonstantin Ananyev
105e18a033bSKonstantin Ananyev        q = ngx_queue_last(&cache->expire_queue);
106e18a033bSKonstantin Ananyev
107e18a033bSKonstantin Ananyev        file = ngx_queue_data(q, ngx_cached_open_file_t, queue);
108e18a033bSKonstantin Ananyev
109e18a033bSKonstantin Ananyev        ngx_queue_remove(q);
110e18a033bSKonstantin Ananyev
111e18a033bSKonstantin Ananyev        ngx_rbtree_delete(&cache->rbtree, &file->node);
112e18a033bSKonstantin Ananyev
113e18a033bSKonstantin Ananyev        cache->current--;
114e18a033bSKonstantin Ananyev
115e18a033bSKonstantin Ananyev        ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0,
116e18a033bSKonstantin Ananyev                       "delete cached open file: %s", file->name);
117e18a033bSKonstantin Ananyev
118e18a033bSKonstantin Ananyev        if (!file->err && !file->is_dir) {
119e18a033bSKonstantin Ananyev            file->close = 1;
120e18a033bSKonstantin Ananyev            file->count = 0;
121e18a033bSKonstantin Ananyev            ngx_close_cached_file(cache, file, 0, ngx_cycle->log);
122e18a033bSKonstantin Ananyev
123e18a033bSKonstantin Ananyev        } else {
124e18a033bSKonstantin Ananyev            ngx_free(file->name);
125e18a033bSKonstantin Ananyev            ngx_free(file);
126e18a033bSKonstantin Ananyev        }
127e18a033bSKonstantin Ananyev    }
128e18a033bSKonstantin Ananyev
129e18a033bSKonstantin Ananyev    if (cache->current) {
130e18a033bSKonstantin Ananyev        ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
131e18a033bSKonstantin Ananyev                      "%ui items still left in open file cache",
132e18a033bSKonstantin Ananyev                      cache->current);
133e18a033bSKonstantin Ananyev    }
134e18a033bSKonstantin Ananyev
135e18a033bSKonstantin Ananyev    if (cache->rbtree.root != cache->rbtree.sentinel) {
136e18a033bSKonstantin Ananyev        ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
137e18a033bSKonstantin Ananyev                      "rbtree still is not empty in open file cache");
138e18a033bSKonstantin Ananyev
139e18a033bSKonstantin Ananyev    }
140e18a033bSKonstantin Ananyev}
141e18a033bSKonstantin Ananyev
142e18a033bSKonstantin Ananyev
143e18a033bSKonstantin Ananyevngx_int_t
144e18a033bSKonstantin Ananyevngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name,
145e18a033bSKonstantin Ananyev    ngx_open_file_info_t *of, ngx_pool_t *pool)
146e18a033bSKonstantin Ananyev{
147e18a033bSKonstantin Ananyev    time_t                          now;
148e18a033bSKonstantin Ananyev    uint32_t                        hash;
149e18a033bSKonstantin Ananyev    ngx_int_t                       rc;
150e18a033bSKonstantin Ananyev    ngx_file_info_t                 fi;
151e18a033bSKonstantin Ananyev    ngx_pool_cleanup_t             *cln;
152e18a033bSKonstantin Ananyev    ngx_cached_open_file_t         *file;
153e18a033bSKonstantin Ananyev    ngx_pool_cleanup_file_t        *clnf;
154e18a033bSKonstantin Ananyev    ngx_open_file_cache_cleanup_t  *ofcln;
155e18a033bSKonstantin Ananyev
156e18a033bSKonstantin Ananyev    of->fd = NGX_INVALID_FILE;
157e18a033bSKonstantin Ananyev    of->err = 0;
158e18a033bSKonstantin Ananyev
159e18a033bSKonstantin Ananyev    if (cache == NULL) {
160e18a033bSKonstantin Ananyev
161e18a033bSKonstantin Ananyev        if (of->test_only) {
162e18a033bSKonstantin Ananyev
163e18a033bSKonstantin Ananyev            if (ngx_file_info_wrapper(name, of, &fi, pool->log)
164e18a033bSKonstantin Ananyev                == NGX_FILE_ERROR)
165e18a033bSKonstantin Ananyev            {
166e18a033bSKonstantin Ananyev                return NGX_ERROR;
167e18a033bSKonstantin Ananyev            }
168e18a033bSKonstantin Ananyev
169e18a033bSKonstantin Ananyev            of->uniq = ngx_file_uniq(&fi);
170e18a033bSKonstantin Ananyev            of->mtime = ngx_file_mtime(&fi);
171e18a033bSKonstantin Ananyev            of->size = ngx_file_size(&fi);
172e18a033bSKonstantin Ananyev            of->fs_size = ngx_file_fs_size(&fi);
173e18a033bSKonstantin Ananyev            of->is_dir = ngx_is_dir(&fi);
174e18a033bSKonstantin Ananyev            of->is_file = ngx_is_file(&fi);
175e18a033bSKonstantin Ananyev            of->is_link = ngx_is_link(&fi);
176e18a033bSKonstantin Ananyev            of->is_exec = ngx_is_exec(&fi);
177e18a033bSKonstantin Ananyev
178e18a033bSKonstantin Ananyev            return NGX_OK;
179e18a033bSKonstantin Ananyev        }
180e18a033bSKonstantin Ananyev
181e18a033bSKonstantin Ananyev        cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t));
182e18a033bSKonstantin Ananyev        if (cln == NULL) {
183e18a033bSKonstantin Ananyev            return NGX_ERROR;
184e18a033bSKonstantin Ananyev        }
185e18a033bSKonstantin Ananyev
186e18a033bSKonstantin Ananyev        rc = ngx_open_and_stat_file(name, of, pool->log);
187e18a033bSKonstantin Ananyev
188e18a033bSKonstantin Ananyev        if (rc == NGX_OK && !of->is_dir) {
189e18a033bSKonstantin Ananyev            cln->handler = ngx_pool_cleanup_file;
190e18a033bSKonstantin Ananyev            clnf = cln->data;
191e18a033bSKonstantin Ananyev
192e18a033bSKonstantin Ananyev            clnf->fd = of->fd;
193e18a033bSKonstantin Ananyev            clnf->name = name->data;
194e18a033bSKonstantin Ananyev            clnf->log = pool->log;
195e18a033bSKonstantin Ananyev        }
196e18a033bSKonstantin Ananyev
197e18a033bSKonstantin Ananyev        return rc;
198e18a033bSKonstantin Ananyev    }
199e18a033bSKonstantin Ananyev
200e18a033bSKonstantin Ananyev    cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t));
201e18a033bSKonstantin Ananyev    if (cln == NULL) {
202e18a033bSKonstantin Ananyev        return NGX_ERROR;
203e18a033bSKonstantin Ananyev    }
204e18a033bSKonstantin Ananyev
205e18a033bSKonstantin Ananyev    now = ngx_time();
206e18a033bSKonstantin Ananyev
207e18a033bSKonstantin Ananyev    hash = ngx_crc32_long(name->data, name->len);
208e18a033bSKonstantin Ananyev
209e18a033bSKonstantin Ananyev    file = ngx_open_file_lookup(cache, name, hash);
210e18a033bSKonstantin Ananyev
211e18a033bSKonstantin Ananyev    if (file) {
212e18a033bSKonstantin Ananyev
213e18a033bSKonstantin Ananyev        file->uses++;
214e18a033bSKonstantin Ananyev
215e18a033bSKonstantin Ananyev        ngx_queue_remove(&file->queue);
216e18a033bSKonstantin Ananyev
217e18a033bSKonstantin Ananyev        if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) {
218e18a033bSKonstantin Ananyev
219e18a033bSKonstantin Ananyev            /* file was not used often enough to keep open */
220e18a033bSKonstantin Ananyev
221e18a033bSKonstantin Ananyev            rc = ngx_open_and_stat_file(name, of, pool->log);
222e18a033bSKonstantin Ananyev
223e18a033bSKonstantin Ananyev            if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
224e18a033bSKonstantin Ananyev                goto failed;
225e18a033bSKonstantin Ananyev            }
226e18a033bSKonstantin Ananyev
227e18a033bSKonstantin Ananyev            goto add_event;
228e18a033bSKonstantin Ananyev        }
229e18a033bSKonstantin Ananyev
230e18a033bSKonstantin Ananyev        if (file->use_event
231e18a033bSKonstantin Ananyev            || (file->event == NULL
232e18a033bSKonstantin Ananyev                && (of->uniq == 0 || of->uniq == file->uniq)
233e18a033bSKonstantin Ananyev                && now - file->created < of->valid
234e18a033bSKonstantin Ananyev#if (NGX_HAVE_OPENAT)
235e18a033bSKonstantin Ananyev                && of->disable_symlinks == file->disable_symlinks
236e18a033bSKonstantin Ananyev                && of->disable_symlinks_from == file->disable_symlinks_from
237e18a033bSKonstantin Ananyev#endif
238e18a033bSKonstantin Ananyev            ))
239e18a033bSKonstantin Ananyev        {
240e18a033bSKonstantin Ananyev            if (file->err == 0) {
241e18a033bSKonstantin Ananyev
242e18a033bSKonstantin Ananyev                of->fd = file->fd;
243e18a033bSKonstantin Ananyev                of->uniq = file->uniq;
244e18a033bSKonstantin Ananyev                of->mtime = file->mtime;
245e18a033bSKonstantin Ananyev                of->size = file->size;
246e18a033bSKonstantin Ananyev
247e18a033bSKonstantin Ananyev                of->is_dir = file->is_dir;
248e18a033bSKonstantin Ananyev                of->is_file = file->is_file;
249e18a033bSKonstantin Ananyev                of->is_link = file->is_link;
250e18a033bSKonstantin Ananyev                of->is_exec = file->is_exec;
251e18a033bSKonstantin Ananyev                of->is_directio = file->is_directio;
252e18a033bSKonstantin Ananyev
253e18a033bSKonstantin Ananyev                if (!file->is_dir) {
254e18a033bSKonstantin Ananyev                    file->count++;
255e18a033bSKonstantin Ananyev                    ngx_open_file_add_event(cache, file, of, pool->log);
256e18a033bSKonstantin Ananyev                }
257e18a033bSKonstantin Ananyev
258e18a033bSKonstantin Ananyev            } else {
259e18a033bSKonstantin Ananyev                of->err = file->err;
260e18a033bSKonstantin Ananyev#if (NGX_HAVE_OPENAT)
261e18a033bSKonstantin Ananyev                of->failed = file->disable_symlinks ? ngx_openat_file_n
262e18a033bSKonstantin Ananyev                                                    : ngx_open_file_n;
263e18a033bSKonstantin Ananyev#else
264e18a033bSKonstantin Ananyev                of->failed = ngx_open_file_n;
265e18a033bSKonstantin Ananyev#endif
266e18a033bSKonstantin Ananyev            }
267e18a033bSKonstantin Ananyev
268e18a033bSKonstantin Ananyev            goto found;
269e18a033bSKonstantin Ananyev        }
270e18a033bSKonstantin Ananyev
271e18a033bSKonstantin Ananyev        ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0,
272e18a033bSKonstantin Ananyev                       "retest open file: %s, fd:%d, c:%d, e:%d",
273e18a033bSKonstantin Ananyev                       file->name, file->fd, file->count, file->err);
274e18a033bSKonstantin Ananyev
275e18a033bSKonstantin Ananyev        if (file->is_dir) {
276e18a033bSKonstantin Ananyev
277e18a033bSKonstantin Ananyev            /*
278e18a033bSKonstantin Ananyev             * chances that directory became file are very small
279e18a033bSKonstantin Ananyev             * so test_dir flag allows to use a single syscall
280e18a033bSKonstantin Ananyev             * in ngx_file_info() instead of three syscalls
281e18a033bSKonstantin Ananyev             */
282e18a033bSKonstantin Ananyev
283e18a033bSKonstantin Ananyev            of->test_dir = 1;
284e18a033bSKonstantin Ananyev        }
285e18a033bSKonstantin Ananyev
286e18a033bSKonstantin Ananyev        of->fd = file->fd;
287e18a033bSKonstantin Ananyev        of->uniq = file->uniq;
288e18a033bSKonstantin Ananyev
289e18a033bSKonstantin Ananyev        rc = ngx_open_and_stat_file(name, of, pool->log);
290e18a033bSKonstantin Ananyev
291e18a033bSKonstantin Ananyev        if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
292e18a033bSKonstantin Ananyev            goto failed;
293e18a033bSKonstantin Ananyev        }
294e18a033bSKonstantin Ananyev
295e18a033bSKonstantin Ananyev        if (of->is_dir) {
296e18a033bSKonstantin Ananyev
297e18a033bSKonstantin Ananyev            if (file->is_dir || file->err) {
298e18a033bSKonstantin Ananyev                goto update;
299e18a033bSKonstantin Ananyev            }
300e18a033bSKonstantin Ananyev
301e18a033bSKonstantin Ananyev            /* file became directory */
302e18a033bSKonstantin Ananyev
303e18a033bSKonstantin Ananyev        } else if (of->err == 0) {  /* file */
304e18a033bSKonstantin Ananyev
305e18a033bSKonstantin Ananyev            if (file->is_dir || file->err) {
306e18a033bSKonstantin Ananyev                goto add_event;
307e18a033bSKonstantin Ananyev            }
308e18a033bSKonstantin Ananyev
309e18a033bSKonstantin Ananyev            if (of->uniq == file->uniq) {
310e18a033bSKonstantin Ananyev
311e18a033bSKonstantin Ananyev                if (file->event) {
312e18a033bSKonstantin Ananyev                    file->use_event = 1;
313e18a033bSKonstantin Ananyev                }
314e18a033bSKonstantin Ananyev
315e18a033bSKonstantin Ananyev                of->is_directio = file->is_directio;
316e18a033bSKonstantin Ananyev
317e18a033bSKonstantin Ananyev                goto update;
318e18a033bSKonstantin Ananyev            }
319e18a033bSKonstantin Ananyev
320e18a033bSKonstantin Ananyev            /* file was changed */
321e18a033bSKonstantin Ananyev
322e18a033bSKonstantin Ananyev        } else { /* error to cache */
323e18a033bSKonstantin Ananyev
324e18a033bSKonstantin Ananyev            if (file->err || file->is_dir) {
325e18a033bSKonstantin Ananyev                goto update;
326e18a033bSKonstantin Ananyev            }
327e18a033bSKonstantin Ananyev
328e18a033bSKonstantin Ananyev            /* file was removed, etc. */
329e18a033bSKonstantin Ananyev        }
330e18a033bSKonstantin Ananyev
331e18a033bSKonstantin Ananyev        if (file->count == 0) {
332e18a033bSKonstantin Ananyev
333e18a033bSKonstantin Ananyev            ngx_open_file_del_event(file);
334e18a033bSKonstantin Ananyev
335e18a033bSKonstantin Ananyev            if (ngx_close_file(file->fd) == NGX_FILE_ERROR) {
336e18a033bSKonstantin Ananyev                ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno,
337e18a033bSKonstantin Ananyev                              ngx_close_file_n " \"%V\" failed", name);
338e18a033bSKonstantin Ananyev            }
339e18a033bSKonstantin Ananyev
340e18a033bSKonstantin Ananyev            goto add_event;
341e18a033bSKonstantin Ananyev        }
342e18a033bSKonstantin Ananyev
343e18a033bSKonstantin Ananyev        ngx_rbtree_delete(&cache->rbtree, &file->node);
344e18a033bSKonstantin Ananyev
345e18a033bSKonstantin Ananyev        cache->current--;
346e18a033bSKonstantin Ananyev
347e18a033bSKonstantin Ananyev        file->close = 1;
348e18a033bSKonstantin Ananyev
349e18a033bSKonstantin Ananyev        goto create;
350e18a033bSKonstantin Ananyev    }
351e18a033bSKonstantin Ananyev
352e18a033bSKonstantin Ananyev    /* not found */
353e18a033bSKonstantin Ananyev
354e18a033bSKonstantin Ananyev    rc = ngx_open_and_stat_file(name, of, pool->log);
355e18a033bSKonstantin Ananyev
356e18a033bSKonstantin Ananyev    if (rc != NGX_OK && (of->err == 0 || !of->errors)) {
357e18a033bSKonstantin Ananyev        goto failed;
358e18a033bSKonstantin Ananyev    }
359e18a033bSKonstantin Ananyev
360e18a033bSKonstantin Ananyevcreate:
361e18a033bSKonstantin Ananyev
362e18a033bSKonstantin Ananyev    if (cache->current >= cache->max) {
363e18a033bSKonstantin Ananyev        ngx_expire_old_cached_files(cache, 0, pool->log);
364e18a033bSKonstantin Ananyev    }
365e18a033bSKonstantin Ananyev
366e18a033bSKonstantin Ananyev    file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log);
367e18a033bSKonstantin Ananyev
368e18a033bSKonstantin Ananyev    if (file == NULL) {
369e18a033bSKonstantin Ananyev        goto failed;
370e18a033bSKonstantin Ananyev    }
371e18a033bSKonstantin Ananyev
372e18a033bSKonstantin Ananyev    file->name = ngx_alloc(name->len + 1, pool->log);
373e18a033bSKonstantin Ananyev
374e18a033bSKonstantin Ananyev    if (file->name == NULL) {
375e18a033bSKonstantin Ananyev        ngx_free(file);
376e18a033bSKonstantin Ananyev        file = NULL;
377e18a033bSKonstantin Ananyev        goto failed;
378e18a033bSKonstantin Ananyev    }
379e18a033bSKonstantin Ananyev
380e18a033bSKonstantin Ananyev    ngx_cpystrn(file->name, name->data, name->len + 1);
381e18a033bSKonstantin Ananyev
382e18a033bSKonstantin Ananyev    file->node.key = hash;
383e18a033bSKonstantin Ananyev
384e18a033bSKonstantin Ananyev    ngx_rbtree_insert(&cache->rbtree, &file->node);
385e18a033bSKonstantin Ananyev
386e18a033bSKonstantin Ananyev    cache->current++;
387e18a033bSKonstantin Ananyev
388e18a033bSKonstantin Ananyev    file->uses = 1;
389e18a033bSKonstantin Ananyev    file->count = 0;
390e18a033bSKonstantin Ananyev    file->use_event = 0;
391e18a033bSKonstantin Ananyev    file->event = NULL;
392e18a033bSKonstantin Ananyev
393e18a033bSKonstantin Ananyevadd_event:
394e18a033bSKonstantin Ananyev
395e18a033bSKonstantin Ananyev    ngx_open_file_add_event(cache, file, of, pool->log);
396e18a033bSKonstantin Ananyev
397e18a033bSKonstantin Ananyevupdate:
398e18a033bSKonstantin Ananyev
399e18a033bSKonstantin Ananyev    file->fd = of->fd;
400e18a033bSKonstantin Ananyev    file->err = of->err;
401e18a033bSKonstantin Ananyev#if (NGX_HAVE_OPENAT)
402e18a033bSKonstantin Ananyev    file->disable_symlinks = of->disable_symlinks;
403e18a033bSKonstantin Ananyev    file->disable_symlinks_from = of->disable_symlinks_from;
404e18a033bSKonstantin Ananyev#endif
405e18a033bSKonstantin Ananyev
406e18a033bSKonstantin Ananyev    if (of->err == 0) {
407e18a033bSKonstantin Ananyev        file->uniq = of->uniq;
408e18a033bSKonstantin Ananyev        file->mtime = of->mtime;
409e18a033bSKonstantin Ananyev        file->size = of->size;
410e18a033bSKonstantin Ananyev
411e18a033bSKonstantin Ananyev        file->close = 0;
412e18a033bSKonstantin Ananyev
413e18a033bSKonstantin Ananyev        file->is_dir = of->is_dir;
414e18a033bSKonstantin Ananyev        file->is_file = of->is_file;
415e18a033b