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