Mercurial > hg > nginx-quic
annotate src/core/ngx_open_file_cache.c @ 2922:f0604d0b28a6
*) exit if no workers could not be started
*) do not quit old workers if no new workers could not be started
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Sat, 06 Jun 2009 12:36:44 +0000 |
parents | 87c088e6956a |
children | b6d588fa3ee9 |
rev | line source |
---|---|
1453 | 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 | |
12 /* | |
13 * open file cache caches | |
14 * open file handles with stat() info; | |
15 * directories stat() info; | |
16 * files and directories errors: not found, access denied, etc. | |
17 */ | |
18 | |
19 | |
20 static void ngx_open_file_cache_cleanup(void *data); | |
1775 | 21 static ngx_int_t ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, |
22 ngx_log_t *log); | |
23 static void ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
24 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log); | |
1453 | 25 static void ngx_open_file_cleanup(void *data); |
26 static void ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 27 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log); |
1775 | 28 static void ngx_open_file_del_event(ngx_cached_open_file_t *file); |
1453 | 29 static void ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, |
30 ngx_uint_t n, ngx_log_t *log); | |
31 static void ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
32 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel); | |
1775 | 33 static ngx_cached_open_file_t * |
34 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
35 uint32_t hash); | |
1453 | 36 static void ngx_open_file_cache_remove(ngx_event_t *ev); |
37 | |
38 | |
39 ngx_open_file_cache_t * | |
40 ngx_open_file_cache_init(ngx_pool_t *pool, ngx_uint_t max, time_t inactive) | |
41 { | |
42 ngx_pool_cleanup_t *cln; | |
43 ngx_open_file_cache_t *cache; | |
44 | |
45 cache = ngx_palloc(pool, sizeof(ngx_open_file_cache_t)); | |
46 if (cache == NULL) { | |
47 return NULL; | |
48 } | |
49 | |
1761 | 50 ngx_rbtree_init(&cache->rbtree, &cache->sentinel, |
1743
4fc402c3ec73
optimize rbtree initialization and insert
Igor Sysoev <igor@sysoev.ru>
parents:
1458
diff
changeset
|
51 ngx_open_file_cache_rbtree_insert_value); |
1453 | 52 |
1765 | 53 ngx_queue_init(&cache->expire_queue); |
54 | |
1453 | 55 cache->current = 0; |
56 cache->max = max; | |
57 cache->inactive = inactive; | |
58 | |
59 cln = ngx_pool_cleanup_add(pool, 0); | |
60 if (cln == NULL) { | |
61 return NULL; | |
62 } | |
63 | |
64 cln->handler = ngx_open_file_cache_cleanup; | |
65 cln->data = cache; | |
66 | |
67 return cache; | |
68 } | |
69 | |
70 | |
71 static void | |
72 ngx_open_file_cache_cleanup(void *data) | |
73 { | |
74 ngx_open_file_cache_t *cache = data; | |
75 | |
1765 | 76 ngx_queue_t *q; |
1453 | 77 ngx_cached_open_file_t *file; |
78 | |
79 ngx_log_debug0(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
80 "open file cache cleanup"); | |
81 | |
82 for ( ;; ) { | |
83 | |
1765 | 84 if (ngx_queue_empty(&cache->expire_queue)) { |
1766 | 85 break; |
1453 | 86 } |
87 | |
1765 | 88 q = ngx_queue_last(&cache->expire_queue); |
89 | |
90 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
91 | |
92 ngx_queue_remove(q); | |
1453 | 93 |
94 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
95 | |
96 cache->current--; | |
97 | |
98 ngx_log_debug1(NGX_LOG_DEBUG_CORE, ngx_cycle->log, 0, | |
99 "delete cached open file: %s", file->name); | |
100 | |
101 if (!file->err && !file->is_dir) { | |
102 file->close = 1; | |
103 file->count = 0; | |
1772 | 104 ngx_close_cached_file(cache, file, 0, ngx_cycle->log); |
1453 | 105 |
106 } else { | |
107 ngx_free(file->name); | |
108 ngx_free(file); | |
109 } | |
110 } | |
111 | |
112 if (cache->current) { | |
113 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, | |
114 "%d items still leave in open file cache", | |
115 cache->current); | |
116 } | |
117 | |
118 if (cache->rbtree.root != cache->rbtree.sentinel) { | |
119 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0, | |
120 "rbtree still is not empty in open file cache"); | |
121 | |
122 } | |
123 } | |
124 | |
125 | |
126 ngx_int_t | |
127 ngx_open_cached_file(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
128 ngx_open_file_info_t *of, ngx_pool_t *pool) | |
129 { | |
130 time_t now; | |
131 uint32_t hash; | |
132 ngx_int_t rc; | |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
133 ngx_file_info_t fi; |
1453 | 134 ngx_pool_cleanup_t *cln; |
135 ngx_cached_open_file_t *file; | |
136 ngx_pool_cleanup_file_t *clnf; | |
137 ngx_open_file_cache_cleanup_t *ofcln; | |
138 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
139 of->fd = NGX_INVALID_FILE; |
1453 | 140 of->err = 0; |
141 | |
142 if (cache == NULL) { | |
143 | |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
144 if (of->test_only) { |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
145 |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
146 if (ngx_file_info(name->data, &fi) == NGX_FILE_ERROR) { |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
147 of->err = ngx_errno; |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
148 of->failed = ngx_file_info_n; |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
149 return NGX_ERROR; |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
150 } |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
151 |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
152 of->uniq = ngx_file_uniq(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
153 of->mtime = ngx_file_mtime(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
154 of->size = ngx_file_size(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
155 of->is_dir = ngx_is_dir(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
156 of->is_file = ngx_is_file(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
157 of->is_link = ngx_is_link(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
158 of->is_exec = ngx_is_exec(&fi); |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
159 |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
160 return NGX_OK; |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
161 } |
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
162 |
1453 | 163 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); |
164 if (cln == NULL) { | |
165 return NGX_ERROR; | |
166 } | |
167 | |
168 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
169 | |
170 if (rc == NGX_OK && !of->is_dir) { | |
171 cln->handler = ngx_pool_cleanup_file; | |
172 clnf = cln->data; | |
173 | |
174 clnf->fd = of->fd; | |
175 clnf->name = name->data; | |
176 clnf->log = pool->log; | |
177 } | |
178 | |
179 return rc; | |
180 } | |
181 | |
182 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t)); | |
183 if (cln == NULL) { | |
184 return NGX_ERROR; | |
185 } | |
186 | |
187 now = ngx_time(); | |
188 | |
1775 | 189 hash = ngx_crc32_long(name->data, name->len); |
190 | |
191 file = ngx_open_file_lookup(cache, name, hash); | |
192 | |
193 if (file) { | |
194 | |
195 file->uses++; | |
196 | |
1987 | 197 ngx_queue_remove(&file->queue); |
198 | |
1775 | 199 if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) { |
200 | |
201 /* file was not used often enough to keep open */ | |
202 | |
203 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
1453 | 204 |
1775 | 205 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
206 goto failed; | |
207 } | |
208 | |
209 goto add_event; | |
1453 | 210 } |
211 | |
2070 | 212 if (file->use_event |
2063
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2006
diff
changeset
|
213 || (file->event == NULL |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2006
diff
changeset
|
214 && (of->uniq == 0 || of->uniq == file->uniq) |
67a29af877ed
initialize of.uniq in ngx_open_cached_file()
Igor Sysoev <igor@sysoev.ru>
parents:
2006
diff
changeset
|
215 && now - file->created < of->valid)) |
1775 | 216 { |
217 if (file->err == 0) { | |
1772 | 218 |
1775 | 219 of->fd = file->fd; |
220 of->uniq = file->uniq; | |
221 of->mtime = file->mtime; | |
222 of->size = file->size; | |
1453 | 223 |
1775 | 224 of->is_dir = file->is_dir; |
225 of->is_file = file->is_file; | |
226 of->is_link = file->is_link; | |
227 of->is_exec = file->is_exec; | |
2246
987831d73bd8
cache directio flag in open file cache
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
228 of->is_directio = file->is_directio; |
1453 | 229 |
1775 | 230 if (!file->is_dir) { |
231 file->count++; | |
232 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 233 } |
234 | |
1775 | 235 } else { |
236 of->err = file->err; | |
2783
87c088e6956a
set of.failed for cached error, the bug has been introduced in r2757
Igor Sysoev <igor@sysoev.ru>
parents:
2782
diff
changeset
|
237 of->failed = ngx_open_file_n; |
1775 | 238 } |
239 | |
240 goto found; | |
241 } | |
242 | |
243 ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0, | |
244 "retest open file: %s, fd:%d, c:%d, e:%d", | |
245 file->name, file->fd, file->count, file->err); | |
1453 | 246 |
1775 | 247 if (file->is_dir) { |
248 | |
249 /* | |
250 * chances that directory became file are very small | |
251 * so test_dir flag allows to use a single syscall | |
252 * in ngx_file_info() instead of three syscalls | |
253 */ | |
254 | |
255 of->test_dir = 1; | |
256 } | |
257 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
258 of->fd = file->fd; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
259 of->uniq = file->uniq; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
260 |
1775 | 261 rc = ngx_open_and_stat_file(name->data, of, pool->log); |
1453 | 262 |
1775 | 263 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
264 goto failed; | |
265 } | |
266 | |
267 if (of->is_dir) { | |
268 | |
269 if (file->is_dir || file->err) { | |
270 goto update; | |
271 } | |
272 | |
273 /* file became directory */ | |
1453 | 274 |
1775 | 275 } else if (of->err == 0) { /* file */ |
276 | |
277 if (file->is_dir || file->err) { | |
278 goto add_event; | |
279 } | |
1453 | 280 |
2070 | 281 if (of->uniq == file->uniq) { |
1453 | 282 |
1775 | 283 file->count++; |
1453 | 284 |
1775 | 285 if (file->event) { |
286 file->use_event = 1; | |
1453 | 287 } |
288 | |
1775 | 289 goto renew; |
290 } | |
1453 | 291 |
1775 | 292 /* file was changed */ |
1453 | 293 |
1775 | 294 } else { /* error to cache */ |
1453 | 295 |
1775 | 296 if (file->err || file->is_dir) { |
297 goto update; | |
1453 | 298 } |
299 | |
1775 | 300 /* file was removed, etc. */ |
301 } | |
302 | |
303 if (file->count == 0) { | |
304 | |
305 ngx_open_file_del_event(file); | |
1453 | 306 |
1775 | 307 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
308 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
309 ngx_close_file_n " \"%s\" failed", | |
310 name->data); | |
311 } | |
1453 | 312 |
1775 | 313 goto add_event; |
314 } | |
315 | |
316 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
317 | |
318 cache->current--; | |
319 | |
320 file->close = 1; | |
321 | |
322 goto create; | |
1453 | 323 } |
324 | |
325 /* not found */ | |
326 | |
327 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
328 | |
329 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | |
330 goto failed; | |
331 } | |
332 | |
333 create: | |
334 | |
335 if (cache->current >= cache->max) { | |
336 ngx_expire_old_cached_files(cache, 0, pool->log); | |
337 } | |
338 | |
339 file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log); | |
340 | |
341 if (file == NULL) { | |
342 goto failed; | |
343 } | |
344 | |
345 file->name = ngx_alloc(name->len + 1, pool->log); | |
346 | |
347 if (file->name == NULL) { | |
348 ngx_free(file); | |
349 file = NULL; | |
350 goto failed; | |
351 } | |
352 | |
353 ngx_cpystrn(file->name, name->data, name->len + 1); | |
354 | |
355 file->node.key = hash; | |
356 | |
357 ngx_rbtree_insert(&cache->rbtree, &file->node); | |
358 | |
359 cache->current++; | |
360 | |
1775 | 361 file->uses = 1; |
1453 | 362 file->count = 0; |
1775 | 363 file->event = NULL; |
364 | |
365 add_event: | |
366 | |
367 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 368 |
369 update: | |
370 | |
371 file->fd = of->fd; | |
372 file->err = of->err; | |
373 | |
374 if (of->err == 0) { | |
375 file->uniq = of->uniq; | |
376 file->mtime = of->mtime; | |
377 file->size = of->size; | |
378 | |
379 file->close = 0; | |
380 | |
381 file->is_dir = of->is_dir; | |
382 file->is_file = of->is_file; | |
383 file->is_link = of->is_link; | |
384 file->is_exec = of->is_exec; | |
2246
987831d73bd8
cache directio flag in open file cache
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
385 file->is_directio = of->is_directio; |
1453 | 386 |
387 if (!of->is_dir) { | |
388 file->count++; | |
389 } | |
390 } | |
391 | |
392 renew: | |
393 | |
394 file->created = now; | |
395 | |
396 found: | |
397 | |
398 file->accessed = now; | |
399 | |
1765 | 400 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 401 |
1772 | 402 ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0, |
403 "cached open file: %s, fd:%d, c:%d, e:%d, u:%d", | |
404 file->name, file->fd, file->count, file->err, file->uses); | |
1453 | 405 |
406 if (of->err == 0) { | |
407 | |
408 if (!of->is_dir) { | |
409 cln->handler = ngx_open_file_cleanup; | |
410 ofcln = cln->data; | |
411 | |
412 ofcln->cache = cache; | |
413 ofcln->file = file; | |
1772 | 414 ofcln->min_uses = of->min_uses; |
1453 | 415 ofcln->log = pool->log; |
416 } | |
417 | |
418 return NGX_OK; | |
419 } | |
420 | |
421 return NGX_ERROR; | |
422 | |
423 failed: | |
424 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
425 if (file) { |
1453 | 426 ngx_rbtree_delete(&cache->rbtree, &file->node); |
427 | |
428 cache->current--; | |
429 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
430 if (file->count == 0) { |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
431 |
2006
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
432 if (file->fd != NGX_INVALID_FILE) { |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
433 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
434 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
435 ngx_close_file_n " \"%s\" failed", |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
436 file->name); |
2006
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
437 } |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
438 } |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
439 |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
440 ngx_free(file->name); |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
441 ngx_free(file); |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
442 |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
443 } else { |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
444 file->close = 1; |
1453 | 445 } |
446 } | |
447 | |
448 if (of->fd != NGX_INVALID_FILE) { | |
449 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
450 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
451 ngx_close_file_n " \"%s\" failed", name->data); | |
452 } | |
453 } | |
454 | |
455 return NGX_ERROR; | |
456 } | |
457 | |
458 | |
459 static ngx_int_t | |
460 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) | |
461 { | |
462 ngx_fd_t fd; | |
463 ngx_file_info_t fi; | |
464 | |
2070 | 465 if (of->fd != NGX_INVALID_FILE) { |
1453 | 466 |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
467 if (ngx_file_info(name, &fi) == NGX_FILE_ERROR) { |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
468 of->failed = ngx_file_info_n; |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
469 goto failed; |
1453 | 470 } |
471 | |
2070 | 472 if (of->uniq == ngx_file_uniq(&fi)) { |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
473 goto done; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
474 } |
1453 | 475 |
2070 | 476 } else if (of->test_dir) { |
477 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
478 if (ngx_file_info(name, &fi) == NGX_FILE_ERROR) { |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
479 of->failed = ngx_file_info_n; |
2070 | 480 goto failed; |
481 } | |
482 | |
2460
225fa4abd76f
test ngx_file_info() result, the bug has been introduced in r2070
Igor Sysoev <igor@sysoev.ru>
parents:
2246
diff
changeset
|
483 if (ngx_is_dir(&fi)) { |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
484 goto done; |
1453 | 485 } |
486 } | |
487 | |
2072 | 488 if (!of->log) { |
489 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
490 | |
491 } else { | |
2629
367b29612a00
Win32 appends synchronized if only FILE_APPEND_DATA and SYNCHRONIZE are set
Igor Sysoev <igor@sysoev.ru>
parents:
2628
diff
changeset
|
492 fd = ngx_open_file(name, NGX_FILE_APPEND, NGX_FILE_CREATE_OR_OPEN, |
367b29612a00
Win32 appends synchronized if only FILE_APPEND_DATA and SYNCHRONIZE are set
Igor Sysoev <igor@sysoev.ru>
parents:
2628
diff
changeset
|
493 NGX_FILE_DEFAULT_ACCESS); |
2072 | 494 } |
1453 | 495 |
496 if (fd == NGX_INVALID_FILE) { | |
2756
09cab3f8d92e
*) of.test_only to not open file if only stat() is enough
Igor Sysoev <igor@sysoev.ru>
parents:
2629
diff
changeset
|
497 of->failed = ngx_open_file_n; |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
498 goto failed; |
1453 | 499 } |
500 | |
501 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
502 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
503 ngx_fd_info_n " \"%s\" failed", name); | |
504 | |
505 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
506 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
507 ngx_close_file_n " \"%s\" failed", name); | |
508 } | |
509 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
510 of->fd = NGX_INVALID_FILE; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
511 |
1453 | 512 return NGX_ERROR; |
513 } | |
514 | |
515 if (ngx_is_dir(&fi)) { | |
516 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
517 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
518 ngx_close_file_n " \"%s\" failed", name); | |
519 } | |
520 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
521 of->fd = NGX_INVALID_FILE; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
522 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
523 } else { |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
524 of->fd = fd; |
2129 | 525 |
526 if (of->directio <= ngx_file_size(&fi)) { | |
2246
987831d73bd8
cache directio flag in open file cache
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
527 if (ngx_directio_on(fd) == -1) { |
2129 | 528 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, |
2246
987831d73bd8
cache directio flag in open file cache
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
529 ngx_directio_on_n " \"%s\" failed", name); |
2231
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
530 |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
531 } else { |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
532 of->is_directio = 1; |
2129 | 533 } |
534 } | |
1453 | 535 } |
536 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
537 done: |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
538 |
1453 | 539 of->uniq = ngx_file_uniq(&fi); |
540 of->mtime = ngx_file_mtime(&fi); | |
541 of->size = ngx_file_size(&fi); | |
542 of->is_dir = ngx_is_dir(&fi); | |
543 of->is_file = ngx_is_file(&fi); | |
544 of->is_link = ngx_is_link(&fi); | |
545 of->is_exec = ngx_is_exec(&fi); | |
546 | |
547 return NGX_OK; | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
548 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
549 failed: |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
550 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
551 of->fd = NGX_INVALID_FILE; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
552 of->err = ngx_errno; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
553 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
554 return NGX_ERROR; |
1453 | 555 } |
556 | |
557 | |
1775 | 558 /* |
559 * we ignore any possible event setting error and | |
560 * fallback to usual periodic file retests | |
561 */ | |
562 | |
563 static void | |
564 ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
565 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log) | |
566 { | |
567 ngx_open_file_cache_event_t *fev; | |
568 | |
569 if (!(ngx_event_flags & NGX_USE_VNODE_EVENT) | |
570 || !of->events | |
571 || file->event | |
572 || of->fd == NGX_INVALID_FILE | |
573 || file->uses < of->min_uses) | |
574 { | |
575 return; | |
576 } | |
577 | |
2070 | 578 file->use_event = 0; |
579 | |
1775 | 580 file->event = ngx_calloc(sizeof(ngx_event_t), log); |
581 if (file->event== NULL) { | |
582 return; | |
583 } | |
584 | |
585 fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log); | |
586 if (fev == NULL) { | |
587 ngx_free(file->event); | |
588 file->event = NULL; | |
589 return; | |
590 } | |
591 | |
592 fev->fd = of->fd; | |
593 fev->file = file; | |
594 fev->cache = cache; | |
595 | |
596 file->event->handler = ngx_open_file_cache_remove; | |
597 file->event->data = fev; | |
598 | |
599 /* | |
600 * although vnode event may be called while ngx_cycle->poll | |
601 * destruction, however, cleanup procedures are run before any | |
602 * memory freeing and events will be canceled. | |
603 */ | |
604 | |
605 file->event->log = ngx_cycle->log; | |
606 | |
607 if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT) | |
608 != NGX_OK) | |
609 { | |
610 ngx_free(file->event->data); | |
611 ngx_free(file->event); | |
612 file->event = NULL; | |
613 return; | |
614 } | |
615 | |
616 /* | |
2071 | 617 * we do not set file->use_event here because there may be a race |
618 * condition: a file may be deleted between opening the file and | |
619 * adding event, so we rely upon event notification only after | |
620 * one file revalidation on next file access | |
1775 | 621 */ |
622 | |
623 return; | |
624 } | |
625 | |
626 | |
1453 | 627 static void |
628 ngx_open_file_cleanup(void *data) | |
629 { | |
630 ngx_open_file_cache_cleanup_t *c = data; | |
631 | |
632 c->file->count--; | |
633 | |
1772 | 634 ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log); |
1453 | 635 |
636 /* drop one or two expired open files */ | |
637 ngx_expire_old_cached_files(c->cache, 1, c->log); | |
638 } | |
639 | |
640 | |
641 static void | |
642 ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 643 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log) |
1453 | 644 { |
1772 | 645 ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0, |
646 "close cached open file: %s, fd:%d, c:%d, u:%d, %d", | |
647 file->name, file->fd, file->count, file->uses, file->close); | |
1453 | 648 |
649 if (!file->close) { | |
650 | |
651 file->accessed = ngx_time(); | |
652 | |
1765 | 653 ngx_queue_remove(&file->queue); |
1453 | 654 |
1765 | 655 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 656 |
1772 | 657 if (file->uses >= min_uses || file->count) { |
658 return; | |
659 } | |
1453 | 660 } |
661 | |
1775 | 662 ngx_open_file_del_event(file); |
1453 | 663 |
664 if (file->count) { | |
665 return; | |
666 } | |
667 | |
1772 | 668 if (file->fd != NGX_INVALID_FILE) { |
669 | |
670 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
671 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
672 ngx_close_file_n " \"%s\" failed", file->name); | |
673 } | |
674 | |
675 file->fd = NGX_INVALID_FILE; | |
676 } | |
677 | |
678 if (!file->close) { | |
679 return; | |
1453 | 680 } |
681 | |
682 ngx_free(file->name); | |
683 ngx_free(file); | |
684 } | |
685 | |
686 | |
687 static void | |
1775 | 688 ngx_open_file_del_event(ngx_cached_open_file_t *file) |
689 { | |
690 if (file->event == NULL) { | |
691 return; | |
692 } | |
693 | |
694 (void) ngx_del_event(file->event, NGX_VNODE_EVENT, | |
695 file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT); | |
696 | |
697 ngx_free(file->event->data); | |
698 ngx_free(file->event); | |
699 file->event = NULL; | |
700 file->use_event = 0; | |
701 } | |
702 | |
703 | |
704 static void | |
1453 | 705 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n, |
706 ngx_log_t *log) | |
707 { | |
708 time_t now; | |
1765 | 709 ngx_queue_t *q; |
1453 | 710 ngx_cached_open_file_t *file; |
711 | |
712 now = ngx_time(); | |
713 | |
714 /* | |
715 * n == 1 deletes one or two inactive files | |
716 * n == 0 deletes least recently used file by force | |
717 * and one or two inactive files | |
718 */ | |
719 | |
720 while (n < 3) { | |
721 | |
1765 | 722 if (ngx_queue_empty(&cache->expire_queue)) { |
1453 | 723 return; |
724 } | |
725 | |
1765 | 726 q = ngx_queue_last(&cache->expire_queue); |
727 | |
728 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
729 | |
1453 | 730 if (n++ != 0 && now - file->accessed <= cache->inactive) { |
731 return; | |
732 } | |
733 | |
1765 | 734 ngx_queue_remove(q); |
1453 | 735 |
736 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
737 | |
738 cache->current--; | |
739 | |
740 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, | |
741 "expire cached open file: %s", file->name); | |
742 | |
743 if (!file->err && !file->is_dir) { | |
744 file->close = 1; | |
1772 | 745 ngx_close_cached_file(cache, file, 0, log); |
1453 | 746 |
747 } else { | |
748 ngx_free(file->name); | |
749 ngx_free(file); | |
750 } | |
751 } | |
752 } | |
753 | |
754 | |
755 static void | |
756 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
757 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
758 { | |
759 ngx_rbtree_node_t **p; | |
760 ngx_cached_open_file_t *file, *file_temp; | |
761 | |
762 for ( ;; ) { | |
763 | |
764 if (node->key < temp->key) { | |
765 | |
766 p = &temp->left; | |
767 | |
768 } else if (node->key > temp->key) { | |
769 | |
770 p = &temp->right; | |
771 | |
772 } else { /* node->key == temp->key */ | |
773 | |
774 file = (ngx_cached_open_file_t *) node; | |
775 file_temp = (ngx_cached_open_file_t *) temp; | |
776 | |
777 p = (ngx_strcmp(file->name, file_temp->name) < 0) | |
778 ? &temp->left : &temp->right; | |
779 } | |
780 | |
781 if (*p == sentinel) { | |
782 break; | |
783 } | |
784 | |
785 temp = *p; | |
786 } | |
787 | |
788 *p = node; | |
789 node->parent = temp; | |
790 node->left = sentinel; | |
791 node->right = sentinel; | |
792 ngx_rbt_red(node); | |
793 } | |
794 | |
795 | |
1775 | 796 static ngx_cached_open_file_t * |
797 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
798 uint32_t hash) | |
799 { | |
800 ngx_int_t rc; | |
801 ngx_rbtree_node_t *node, *sentinel; | |
802 ngx_cached_open_file_t *file; | |
803 | |
804 node = cache->rbtree.root; | |
805 sentinel = cache->rbtree.sentinel; | |
806 | |
807 while (node != sentinel) { | |
808 | |
809 if (hash < node->key) { | |
810 node = node->left; | |
811 continue; | |
812 } | |
813 | |
814 if (hash > node->key) { | |
815 node = node->right; | |
816 continue; | |
817 } | |
818 | |
819 /* hash == node->key */ | |
820 | |
821 do { | |
822 file = (ngx_cached_open_file_t *) node; | |
823 | |
824 rc = ngx_strcmp(name->data, file->name); | |
825 | |
826 if (rc == 0) { | |
827 return file; | |
828 } | |
829 | |
830 node = (rc < 0) ? node->left : node->right; | |
831 | |
832 } while (node != sentinel && hash == node->key); | |
833 | |
834 break; | |
835 } | |
836 | |
837 return NULL; | |
838 } | |
839 | |
840 | |
1453 | 841 static void |
842 ngx_open_file_cache_remove(ngx_event_t *ev) | |
843 { | |
844 ngx_cached_open_file_t *file; | |
845 ngx_open_file_cache_event_t *fev; | |
846 | |
847 fev = ev->data; | |
848 file = fev->file; | |
849 | |
1765 | 850 ngx_queue_remove(&file->queue); |
1453 | 851 |
852 ngx_rbtree_delete(&fev->cache->rbtree, &file->node); | |
853 | |
854 fev->cache->current--; | |
855 | |
856 /* NGX_ONESHOT_EVENT was already deleted */ | |
857 file->event = NULL; | |
2070 | 858 file->use_event = 0; |
1453 | 859 |
860 file->close = 1; | |
861 | |
1772 | 862 ngx_close_cached_file(fev->cache, file, 0, ev->log); |
1453 | 863 |
864 /* free memory only when fev->cache and fev->file are already not needed */ | |
865 | |
866 ngx_free(ev->data); | |
867 ngx_free(ev); | |
868 } |