Mercurial > hg > nginx
annotate src/core/ngx_open_file_cache.c @ 2655:87e1fcd9b604 stable-0.6
r2202, r2408, r2425, r2454, r2459, r2482, r2504, r2542, r2565, r2579,
r2580, r2585, r2586, r2587, r2591, r2626 merge:
try_files
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 02 Apr 2009 06:44:45 +0000 |
parents | 9f15e5f8ff9e |
children | 67a29af877ed |
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; | |
133 ngx_pool_cleanup_t *cln; | |
134 ngx_cached_open_file_t *file; | |
135 ngx_pool_cleanup_file_t *clnf; | |
136 ngx_open_file_cache_cleanup_t *ofcln; | |
137 | |
138 of->err = 0; | |
139 | |
140 if (cache == NULL) { | |
141 | |
142 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_pool_cleanup_file_t)); | |
143 if (cln == NULL) { | |
144 return NGX_ERROR; | |
145 } | |
146 | |
147 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
148 | |
149 if (rc == NGX_OK && !of->is_dir) { | |
150 cln->handler = ngx_pool_cleanup_file; | |
151 clnf = cln->data; | |
152 | |
153 clnf->fd = of->fd; | |
154 clnf->name = name->data; | |
155 clnf->log = pool->log; | |
156 } | |
157 | |
158 return rc; | |
159 } | |
160 | |
161 cln = ngx_pool_cleanup_add(pool, sizeof(ngx_open_file_cache_cleanup_t)); | |
162 if (cln == NULL) { | |
163 return NGX_ERROR; | |
164 } | |
165 | |
166 now = ngx_time(); | |
167 | |
1775 | 168 hash = ngx_crc32_long(name->data, name->len); |
169 | |
170 file = ngx_open_file_lookup(cache, name, hash); | |
171 | |
172 if (file) { | |
173 | |
174 file->uses++; | |
175 | |
1987 | 176 ngx_queue_remove(&file->queue); |
177 | |
1775 | 178 if (file->fd == NGX_INVALID_FILE && file->err == 0 && !file->is_dir) { |
179 | |
180 /* file was not used often enough to keep open */ | |
181 | |
182 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
1453 | 183 |
1775 | 184 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
185 goto failed; | |
186 } | |
187 | |
188 goto add_event; | |
1453 | 189 } |
190 | |
1775 | 191 if ((file->event && file->use_event) |
192 || (file->event == NULL && now - file->created < of->valid)) | |
193 { | |
194 if (file->err == 0) { | |
1772 | 195 |
1775 | 196 of->fd = file->fd; |
197 of->uniq = file->uniq; | |
198 of->mtime = file->mtime; | |
199 of->size = file->size; | |
1453 | 200 |
1775 | 201 of->is_dir = file->is_dir; |
202 of->is_file = file->is_file; | |
203 of->is_link = file->is_link; | |
204 of->is_exec = file->is_exec; | |
1453 | 205 |
1775 | 206 if (!file->is_dir) { |
207 file->count++; | |
208 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 209 } |
210 | |
1775 | 211 } else { |
212 of->err = file->err; | |
213 } | |
214 | |
215 goto found; | |
216 } | |
217 | |
218 ngx_log_debug4(NGX_LOG_DEBUG_CORE, pool->log, 0, | |
219 "retest open file: %s, fd:%d, c:%d, e:%d", | |
220 file->name, file->fd, file->count, file->err); | |
1453 | 221 |
1775 | 222 if (file->is_dir) { |
223 | |
224 /* | |
225 * chances that directory became file are very small | |
226 * so test_dir flag allows to use a single syscall | |
227 * in ngx_file_info() instead of three syscalls | |
228 */ | |
229 | |
230 of->test_dir = 1; | |
231 } | |
232 | |
233 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
1453 | 234 |
1775 | 235 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { |
236 goto failed; | |
237 } | |
238 | |
239 if (of->is_dir) { | |
240 | |
241 if (file->is_dir || file->err) { | |
242 goto update; | |
243 } | |
244 | |
245 /* file became directory */ | |
1453 | 246 |
1775 | 247 } else if (of->err == 0) { /* file */ |
248 | |
249 if (file->is_dir || file->err) { | |
250 goto add_event; | |
251 } | |
1453 | 252 |
1775 | 253 if (of->uniq == file->uniq |
254 && of->mtime == file->mtime | |
255 && of->size == file->size) | |
256 { | |
257 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
258 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
259 ngx_close_file_n " \"%s\" failed", | |
260 name->data); | |
1453 | 261 } |
262 | |
1775 | 263 of->fd = file->fd; |
264 file->count++; | |
1453 | 265 |
1775 | 266 if (file->event) { |
267 file->use_event = 1; | |
268 goto renew; | |
1453 | 269 } |
270 | |
1775 | 271 ngx_open_file_add_event(cache, file, of, pool->log); |
272 | |
273 goto renew; | |
274 } | |
1453 | 275 |
1775 | 276 /* file was changed */ |
1453 | 277 |
1775 | 278 } else { /* error to cache */ |
1453 | 279 |
1775 | 280 if (file->err || file->is_dir) { |
281 goto update; | |
1453 | 282 } |
283 | |
1775 | 284 /* file was removed, etc. */ |
285 } | |
286 | |
287 if (file->count == 0) { | |
288 | |
289 ngx_open_file_del_event(file); | |
1453 | 290 |
1775 | 291 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { |
292 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
293 ngx_close_file_n " \"%s\" failed", | |
294 name->data); | |
295 } | |
1453 | 296 |
1775 | 297 goto add_event; |
298 } | |
299 | |
300 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
301 | |
302 cache->current--; | |
303 | |
304 file->close = 1; | |
305 | |
306 goto create; | |
1453 | 307 } |
308 | |
309 /* not found */ | |
310 | |
311 rc = ngx_open_and_stat_file(name->data, of, pool->log); | |
312 | |
313 if (rc != NGX_OK && (of->err == 0 || !of->errors)) { | |
314 goto failed; | |
315 } | |
316 | |
317 create: | |
318 | |
319 if (cache->current >= cache->max) { | |
320 ngx_expire_old_cached_files(cache, 0, pool->log); | |
321 } | |
322 | |
323 file = ngx_alloc(sizeof(ngx_cached_open_file_t), pool->log); | |
324 | |
325 if (file == NULL) { | |
326 goto failed; | |
327 } | |
328 | |
329 file->name = ngx_alloc(name->len + 1, pool->log); | |
330 | |
331 if (file->name == NULL) { | |
332 ngx_free(file); | |
333 file = NULL; | |
334 goto failed; | |
335 } | |
336 | |
337 ngx_cpystrn(file->name, name->data, name->len + 1); | |
338 | |
339 file->node.key = hash; | |
340 | |
341 ngx_rbtree_insert(&cache->rbtree, &file->node); | |
342 | |
343 cache->current++; | |
344 | |
1775 | 345 file->uses = 1; |
1453 | 346 file->count = 0; |
1775 | 347 file->use_event = 0; |
348 file->event = NULL; | |
349 | |
350 add_event: | |
351 | |
352 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 353 |
354 update: | |
355 | |
356 file->fd = of->fd; | |
357 file->err = of->err; | |
358 | |
359 if (of->err == 0) { | |
360 file->uniq = of->uniq; | |
361 file->mtime = of->mtime; | |
362 file->size = of->size; | |
363 | |
364 file->close = 0; | |
365 | |
366 file->is_dir = of->is_dir; | |
367 file->is_file = of->is_file; | |
368 file->is_link = of->is_link; | |
369 file->is_exec = of->is_exec; | |
370 | |
371 if (!of->is_dir) { | |
372 file->count++; | |
373 } | |
374 } | |
375 | |
376 renew: | |
377 | |
378 file->created = now; | |
379 | |
380 found: | |
381 | |
382 file->accessed = now; | |
383 | |
1765 | 384 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 385 |
1772 | 386 ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0, |
387 "cached open file: %s, fd:%d, c:%d, e:%d, u:%d", | |
388 file->name, file->fd, file->count, file->err, file->uses); | |
1453 | 389 |
390 if (of->err == 0) { | |
391 | |
392 if (!of->is_dir) { | |
393 cln->handler = ngx_open_file_cleanup; | |
394 ofcln = cln->data; | |
395 | |
396 ofcln->cache = cache; | |
397 ofcln->file = file; | |
1772 | 398 ofcln->min_uses = of->min_uses; |
1453 | 399 ofcln->log = pool->log; |
400 } | |
401 | |
402 return NGX_OK; | |
403 } | |
404 | |
405 return NGX_ERROR; | |
406 | |
407 failed: | |
408 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
409 if (file) { |
1453 | 410 ngx_rbtree_delete(&cache->rbtree, &file->node); |
411 | |
412 cache->current--; | |
413 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
414 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
|
415 |
2091 | 416 if (file->fd != NGX_INVALID_FILE) { |
417 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
418 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
419 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
|
420 file->name); |
2091 | 421 } |
422 } | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
423 |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
424 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
|
425 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
|
426 |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
427 } else { |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
428 file->close = 1; |
1453 | 429 } |
430 } | |
431 | |
432 if (of->fd != NGX_INVALID_FILE) { | |
433 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
434 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
435 ngx_close_file_n " \"%s\" failed", name->data); | |
436 } | |
437 } | |
438 | |
439 return NGX_ERROR; | |
440 } | |
441 | |
442 | |
443 static ngx_int_t | |
444 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) | |
445 { | |
446 ngx_fd_t fd; | |
447 ngx_file_info_t fi; | |
448 | |
449 of->fd = NGX_INVALID_FILE; | |
450 | |
451 if (of->test_dir) { | |
452 | |
453 if (ngx_file_info(name, &fi) == -1) { | |
454 of->err = ngx_errno; | |
455 | |
456 return NGX_ERROR; | |
457 } | |
458 | |
459 of->uniq = ngx_file_uniq(&fi); | |
460 of->mtime = ngx_file_mtime(&fi); | |
461 of->size = ngx_file_size(&fi); | |
462 of->is_dir = ngx_is_dir(&fi); | |
463 of->is_file = ngx_is_file(&fi); | |
464 of->is_link = ngx_is_link(&fi); | |
465 of->is_exec = ngx_is_exec(&fi); | |
466 | |
467 if (of->is_dir) { | |
468 return NGX_OK; | |
469 } | |
470 } | |
471 | |
472 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
473 | |
474 if (fd == NGX_INVALID_FILE) { | |
475 of->err = ngx_errno; | |
476 return NGX_ERROR; | |
477 } | |
478 | |
479 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
480 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
481 ngx_fd_info_n " \"%s\" failed", name); | |
482 | |
483 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
484 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
485 ngx_close_file_n " \"%s\" failed", name); | |
486 } | |
487 | |
488 return NGX_ERROR; | |
489 } | |
490 | |
491 if (ngx_is_dir(&fi)) { | |
492 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
493 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
494 ngx_close_file_n " \"%s\" failed", name); | |
495 } | |
496 | |
497 fd = NGX_INVALID_FILE; | |
498 } | |
499 | |
500 of->fd = fd; | |
501 of->uniq = ngx_file_uniq(&fi); | |
502 of->mtime = ngx_file_mtime(&fi); | |
503 of->size = ngx_file_size(&fi); | |
504 of->is_dir = ngx_is_dir(&fi); | |
505 of->is_file = ngx_is_file(&fi); | |
506 of->is_link = ngx_is_link(&fi); | |
507 of->is_exec = ngx_is_exec(&fi); | |
508 | |
509 return NGX_OK; | |
510 } | |
511 | |
512 | |
1775 | 513 /* |
514 * we ignore any possible event setting error and | |
515 * fallback to usual periodic file retests | |
516 */ | |
517 | |
518 static void | |
519 ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
520 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log) | |
521 { | |
522 ngx_open_file_cache_event_t *fev; | |
523 | |
524 if (!(ngx_event_flags & NGX_USE_VNODE_EVENT) | |
525 || !of->events | |
526 || file->event | |
527 || of->fd == NGX_INVALID_FILE | |
528 || file->uses < of->min_uses) | |
529 { | |
530 return; | |
531 } | |
532 | |
533 file->event = ngx_calloc(sizeof(ngx_event_t), log); | |
534 if (file->event== NULL) { | |
535 return; | |
536 } | |
537 | |
538 fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log); | |
539 if (fev == NULL) { | |
540 ngx_free(file->event); | |
541 file->event = NULL; | |
542 return; | |
543 } | |
544 | |
545 fev->fd = of->fd; | |
546 fev->file = file; | |
547 fev->cache = cache; | |
548 | |
549 file->event->handler = ngx_open_file_cache_remove; | |
550 file->event->data = fev; | |
551 | |
552 /* | |
553 * although vnode event may be called while ngx_cycle->poll | |
554 * destruction, however, cleanup procedures are run before any | |
555 * memory freeing and events will be canceled. | |
556 */ | |
557 | |
558 file->event->log = ngx_cycle->log; | |
559 | |
560 if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT) | |
561 != NGX_OK) | |
562 { | |
563 ngx_free(file->event->data); | |
564 ngx_free(file->event); | |
565 file->event = NULL; | |
566 return; | |
567 } | |
568 | |
569 /* | |
570 * we do not file->use_event here because there may be a race | |
571 * condition between opening file and adding event, so we rely | |
572 * upon event notification only after first file revalidation | |
573 */ | |
574 | |
575 return; | |
576 } | |
577 | |
578 | |
1453 | 579 static void |
580 ngx_open_file_cleanup(void *data) | |
581 { | |
582 ngx_open_file_cache_cleanup_t *c = data; | |
583 | |
584 c->file->count--; | |
585 | |
1772 | 586 ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log); |
1453 | 587 |
588 /* drop one or two expired open files */ | |
589 ngx_expire_old_cached_files(c->cache, 1, c->log); | |
590 } | |
591 | |
592 | |
593 static void | |
594 ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 595 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log) |
1453 | 596 { |
1772 | 597 ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0, |
598 "close cached open file: %s, fd:%d, c:%d, u:%d, %d", | |
599 file->name, file->fd, file->count, file->uses, file->close); | |
1453 | 600 |
601 if (!file->close) { | |
602 | |
603 file->accessed = ngx_time(); | |
604 | |
1765 | 605 ngx_queue_remove(&file->queue); |
1453 | 606 |
1765 | 607 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 608 |
1772 | 609 if (file->uses >= min_uses || file->count) { |
610 return; | |
611 } | |
1453 | 612 } |
613 | |
1775 | 614 ngx_open_file_del_event(file); |
1453 | 615 |
616 if (file->count) { | |
617 return; | |
618 } | |
619 | |
1772 | 620 if (file->fd != NGX_INVALID_FILE) { |
621 | |
622 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
623 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
624 ngx_close_file_n " \"%s\" failed", file->name); | |
625 } | |
626 | |
627 file->fd = NGX_INVALID_FILE; | |
628 } | |
629 | |
630 if (!file->close) { | |
631 return; | |
1453 | 632 } |
633 | |
634 ngx_free(file->name); | |
635 ngx_free(file); | |
636 } | |
637 | |
638 | |
639 static void | |
1775 | 640 ngx_open_file_del_event(ngx_cached_open_file_t *file) |
641 { | |
642 if (file->event == NULL) { | |
643 return; | |
644 } | |
645 | |
646 (void) ngx_del_event(file->event, NGX_VNODE_EVENT, | |
647 file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT); | |
648 | |
649 ngx_free(file->event->data); | |
650 ngx_free(file->event); | |
651 file->event = NULL; | |
652 file->use_event = 0; | |
653 } | |
654 | |
655 | |
656 static void | |
1453 | 657 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n, |
658 ngx_log_t *log) | |
659 { | |
660 time_t now; | |
1765 | 661 ngx_queue_t *q; |
1453 | 662 ngx_cached_open_file_t *file; |
663 | |
664 now = ngx_time(); | |
665 | |
666 /* | |
667 * n == 1 deletes one or two inactive files | |
668 * n == 0 deletes least recently used file by force | |
669 * and one or two inactive files | |
670 */ | |
671 | |
672 while (n < 3) { | |
673 | |
1765 | 674 if (ngx_queue_empty(&cache->expire_queue)) { |
1453 | 675 return; |
676 } | |
677 | |
1765 | 678 q = ngx_queue_last(&cache->expire_queue); |
679 | |
680 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
681 | |
1453 | 682 if (n++ != 0 && now - file->accessed <= cache->inactive) { |
683 return; | |
684 } | |
685 | |
1765 | 686 ngx_queue_remove(q); |
1453 | 687 |
688 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
689 | |
690 cache->current--; | |
691 | |
692 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, | |
693 "expire cached open file: %s", file->name); | |
694 | |
695 if (!file->err && !file->is_dir) { | |
696 file->close = 1; | |
1772 | 697 ngx_close_cached_file(cache, file, 0, log); |
1453 | 698 |
699 } else { | |
700 ngx_free(file->name); | |
701 ngx_free(file); | |
702 } | |
703 } | |
704 } | |
705 | |
706 | |
707 static void | |
708 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
709 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
710 { | |
711 ngx_rbtree_node_t **p; | |
712 ngx_cached_open_file_t *file, *file_temp; | |
713 | |
714 for ( ;; ) { | |
715 | |
716 if (node->key < temp->key) { | |
717 | |
718 p = &temp->left; | |
719 | |
720 } else if (node->key > temp->key) { | |
721 | |
722 p = &temp->right; | |
723 | |
724 } else { /* node->key == temp->key */ | |
725 | |
726 file = (ngx_cached_open_file_t *) node; | |
727 file_temp = (ngx_cached_open_file_t *) temp; | |
728 | |
729 p = (ngx_strcmp(file->name, file_temp->name) < 0) | |
730 ? &temp->left : &temp->right; | |
731 } | |
732 | |
733 if (*p == sentinel) { | |
734 break; | |
735 } | |
736 | |
737 temp = *p; | |
738 } | |
739 | |
740 *p = node; | |
741 node->parent = temp; | |
742 node->left = sentinel; | |
743 node->right = sentinel; | |
744 ngx_rbt_red(node); | |
745 } | |
746 | |
747 | |
1775 | 748 static ngx_cached_open_file_t * |
749 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
750 uint32_t hash) | |
751 { | |
752 ngx_int_t rc; | |
753 ngx_rbtree_node_t *node, *sentinel; | |
754 ngx_cached_open_file_t *file; | |
755 | |
756 node = cache->rbtree.root; | |
757 sentinel = cache->rbtree.sentinel; | |
758 | |
759 while (node != sentinel) { | |
760 | |
761 if (hash < node->key) { | |
762 node = node->left; | |
763 continue; | |
764 } | |
765 | |
766 if (hash > node->key) { | |
767 node = node->right; | |
768 continue; | |
769 } | |
770 | |
771 /* hash == node->key */ | |
772 | |
773 do { | |
774 file = (ngx_cached_open_file_t *) node; | |
775 | |
776 rc = ngx_strcmp(name->data, file->name); | |
777 | |
778 if (rc == 0) { | |
779 return file; | |
780 } | |
781 | |
782 node = (rc < 0) ? node->left : node->right; | |
783 | |
784 } while (node != sentinel && hash == node->key); | |
785 | |
786 break; | |
787 } | |
788 | |
789 return NULL; | |
790 } | |
791 | |
792 | |
1453 | 793 static void |
794 ngx_open_file_cache_remove(ngx_event_t *ev) | |
795 { | |
796 ngx_cached_open_file_t *file; | |
797 ngx_open_file_cache_event_t *fev; | |
798 | |
799 fev = ev->data; | |
800 file = fev->file; | |
801 | |
1765 | 802 ngx_queue_remove(&file->queue); |
1453 | 803 |
804 ngx_rbtree_delete(&fev->cache->rbtree, &file->node); | |
805 | |
806 fev->cache->current--; | |
807 | |
808 /* NGX_ONESHOT_EVENT was already deleted */ | |
809 file->event = NULL; | |
810 | |
811 file->close = 1; | |
812 | |
1772 | 813 ngx_close_cached_file(fev->cache, file, 0, ev->log); |
1453 | 814 |
815 /* free memory only when fev->cache and fev->file are already not needed */ | |
816 | |
817 ngx_free(ev->data); | |
818 ngx_free(ev); | |
819 } |