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