Mercurial > hg > nginx-quic
annotate src/core/ngx_open_file_cache.c @ 2941:2f731201b00e stable-0.7
merge r2935:
initialize use_event field in open file cache
the bug had been introduced in r2071
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Mon, 15 Jun 2009 08:58:16 +0000 |
parents | 87c088e6956a |
children | b1b1775698d5 |
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; |
2941 | 363 file->use_event = 0; |
1775 | 364 file->event = NULL; |
365 | |
366 add_event: | |
367 | |
368 ngx_open_file_add_event(cache, file, of, pool->log); | |
1453 | 369 |
370 update: | |
371 | |
372 file->fd = of->fd; | |
373 file->err = of->err; | |
374 | |
375 if (of->err == 0) { | |
376 file->uniq = of->uniq; | |
377 file->mtime = of->mtime; | |
378 file->size = of->size; | |
379 | |
380 file->close = 0; | |
381 | |
382 file->is_dir = of->is_dir; | |
383 file->is_file = of->is_file; | |
384 file->is_link = of->is_link; | |
385 file->is_exec = of->is_exec; | |
2246
987831d73bd8
cache directio flag in open file cache
Igor Sysoev <igor@sysoev.ru>
parents:
2231
diff
changeset
|
386 file->is_directio = of->is_directio; |
1453 | 387 |
388 if (!of->is_dir) { | |
389 file->count++; | |
390 } | |
391 } | |
392 | |
393 renew: | |
394 | |
395 file->created = now; | |
396 | |
397 found: | |
398 | |
399 file->accessed = now; | |
400 | |
1765 | 401 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 402 |
1772 | 403 ngx_log_debug5(NGX_LOG_DEBUG_CORE, pool->log, 0, |
404 "cached open file: %s, fd:%d, c:%d, e:%d, u:%d", | |
405 file->name, file->fd, file->count, file->err, file->uses); | |
1453 | 406 |
407 if (of->err == 0) { | |
408 | |
409 if (!of->is_dir) { | |
410 cln->handler = ngx_open_file_cleanup; | |
411 ofcln = cln->data; | |
412 | |
413 ofcln->cache = cache; | |
414 ofcln->file = file; | |
1772 | 415 ofcln->min_uses = of->min_uses; |
1453 | 416 ofcln->log = pool->log; |
417 } | |
418 | |
419 return NGX_OK; | |
420 } | |
421 | |
422 return NGX_ERROR; | |
423 | |
424 failed: | |
425 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
426 if (file) { |
1453 | 427 ngx_rbtree_delete(&cache->rbtree, &file->node); |
428 | |
429 cache->current--; | |
430 | |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
431 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
|
432 |
2006
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
433 if (file->fd != NGX_INVALID_FILE) { |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
434 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
|
435 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
|
436 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
|
437 file->name); |
2006
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
438 } |
b52cb9bf2064
style fix: remove tabs and trailing spaces
Igor Sysoev <igor@sysoev.ru>
parents:
1988
diff
changeset
|
439 } |
1988
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
440 |
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->name); |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
442 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
|
443 |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
444 } else { |
34051c712e41
fix segfault when file is deleted and open_file_cache_errors is off
Igor Sysoev <igor@sysoev.ru>
parents:
1987
diff
changeset
|
445 file->close = 1; |
1453 | 446 } |
447 } | |
448 | |
449 if (of->fd != NGX_INVALID_FILE) { | |
450 if (ngx_close_file(of->fd) == NGX_FILE_ERROR) { | |
451 ngx_log_error(NGX_LOG_ALERT, pool->log, ngx_errno, | |
452 ngx_close_file_n " \"%s\" failed", name->data); | |
453 } | |
454 } | |
455 | |
456 return NGX_ERROR; | |
457 } | |
458 | |
459 | |
460 static ngx_int_t | |
461 ngx_open_and_stat_file(u_char *name, ngx_open_file_info_t *of, ngx_log_t *log) | |
462 { | |
463 ngx_fd_t fd; | |
464 ngx_file_info_t fi; | |
465 | |
2070 | 466 if (of->fd != NGX_INVALID_FILE) { |
1453 | 467 |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
468 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
|
469 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
|
470 goto failed; |
1453 | 471 } |
472 | |
2070 | 473 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
|
474 goto done; |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
475 } |
1453 | 476 |
2070 | 477 } else if (of->test_dir) { |
478 | |
2782
4bd7825fab80
uniform ngx_file_info() interface with ngx_fd_info()
Igor Sysoev <igor@sysoev.ru>
parents:
2756
diff
changeset
|
479 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
|
480 of->failed = ngx_file_info_n; |
2070 | 481 goto failed; |
482 } | |
483 | |
2460
225fa4abd76f
test ngx_file_info() result, the bug has been introduced in r2070
Igor Sysoev <igor@sysoev.ru>
parents:
2246
diff
changeset
|
484 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
|
485 goto done; |
1453 | 486 } |
487 } | |
488 | |
2072 | 489 if (!of->log) { |
490 fd = ngx_open_file(name, NGX_FILE_RDONLY, NGX_FILE_OPEN, 0); | |
491 | |
492 } else { | |
2629
367b29612a00
Win32 appends synchronized if only FILE_APPEND_DATA and SYNCHRONIZE are set
Igor Sysoev <igor@sysoev.ru>
parents:
2628
diff
changeset
|
493 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
|
494 NGX_FILE_DEFAULT_ACCESS); |
2072 | 495 } |
1453 | 496 |
497 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
|
498 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
|
499 goto failed; |
1453 | 500 } |
501 | |
502 if (ngx_fd_info(fd, &fi) == NGX_FILE_ERROR) { | |
503 ngx_log_error(NGX_LOG_CRIT, log, ngx_errno, | |
504 ngx_fd_info_n " \"%s\" failed", name); | |
505 | |
506 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
507 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
508 ngx_close_file_n " \"%s\" failed", name); | |
509 } | |
510 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
511 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
|
512 |
1453 | 513 return NGX_ERROR; |
514 } | |
515 | |
516 if (ngx_is_dir(&fi)) { | |
517 if (ngx_close_file(fd) == NGX_FILE_ERROR) { | |
518 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
519 ngx_close_file_n " \"%s\" failed", name); | |
520 } | |
521 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
522 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
|
523 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
524 } else { |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
525 of->fd = fd; |
2129 | 526 |
527 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
|
528 if (ngx_directio_on(fd) == -1) { |
2129 | 529 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
|
530 ngx_directio_on_n " \"%s\" failed", name); |
2231
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
531 |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
532 } else { |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
533 of->is_directio = 1; |
2129 | 534 } |
535 } | |
1453 | 536 } |
537 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
538 done: |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
539 |
1453 | 540 of->uniq = ngx_file_uniq(&fi); |
541 of->mtime = ngx_file_mtime(&fi); | |
542 of->size = ngx_file_size(&fi); | |
543 of->is_dir = ngx_is_dir(&fi); | |
544 of->is_file = ngx_is_file(&fi); | |
545 of->is_link = ngx_is_link(&fi); | |
546 of->is_exec = ngx_is_exec(&fi); | |
547 | |
548 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
|
549 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
550 failed: |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
551 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
552 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
|
553 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
|
554 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
555 return NGX_ERROR; |
1453 | 556 } |
557 | |
558 | |
1775 | 559 /* |
560 * we ignore any possible event setting error and | |
561 * fallback to usual periodic file retests | |
562 */ | |
563 | |
564 static void | |
565 ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
566 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log) | |
567 { | |
568 ngx_open_file_cache_event_t *fev; | |
569 | |
570 if (!(ngx_event_flags & NGX_USE_VNODE_EVENT) | |
571 || !of->events | |
572 || file->event | |
573 || of->fd == NGX_INVALID_FILE | |
574 || file->uses < of->min_uses) | |
575 { | |
576 return; | |
577 } | |
578 | |
2070 | 579 file->use_event = 0; |
580 | |
1775 | 581 file->event = ngx_calloc(sizeof(ngx_event_t), log); |
582 if (file->event== NULL) { | |
583 return; | |
584 } | |
585 | |
586 fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log); | |
587 if (fev == NULL) { | |
588 ngx_free(file->event); | |
589 file->event = NULL; | |
590 return; | |
591 } | |
592 | |
593 fev->fd = of->fd; | |
594 fev->file = file; | |
595 fev->cache = cache; | |
596 | |
597 file->event->handler = ngx_open_file_cache_remove; | |
598 file->event->data = fev; | |
599 | |
600 /* | |
601 * although vnode event may be called while ngx_cycle->poll | |
602 * destruction, however, cleanup procedures are run before any | |
603 * memory freeing and events will be canceled. | |
604 */ | |
605 | |
606 file->event->log = ngx_cycle->log; | |
607 | |
608 if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT) | |
609 != NGX_OK) | |
610 { | |
611 ngx_free(file->event->data); | |
612 ngx_free(file->event); | |
613 file->event = NULL; | |
614 return; | |
615 } | |
616 | |
617 /* | |
2071 | 618 * we do not set file->use_event here because there may be a race |
619 * condition: a file may be deleted between opening the file and | |
620 * adding event, so we rely upon event notification only after | |
621 * one file revalidation on next file access | |
1775 | 622 */ |
623 | |
624 return; | |
625 } | |
626 | |
627 | |
1453 | 628 static void |
629 ngx_open_file_cleanup(void *data) | |
630 { | |
631 ngx_open_file_cache_cleanup_t *c = data; | |
632 | |
633 c->file->count--; | |
634 | |
1772 | 635 ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log); |
1453 | 636 |
637 /* drop one or two expired open files */ | |
638 ngx_expire_old_cached_files(c->cache, 1, c->log); | |
639 } | |
640 | |
641 | |
642 static void | |
643 ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 644 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log) |
1453 | 645 { |
1772 | 646 ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0, |
647 "close cached open file: %s, fd:%d, c:%d, u:%d, %d", | |
648 file->name, file->fd, file->count, file->uses, file->close); | |
1453 | 649 |
650 if (!file->close) { | |
651 | |
652 file->accessed = ngx_time(); | |
653 | |
1765 | 654 ngx_queue_remove(&file->queue); |
1453 | 655 |
1765 | 656 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 657 |
1772 | 658 if (file->uses >= min_uses || file->count) { |
659 return; | |
660 } | |
1453 | 661 } |
662 | |
1775 | 663 ngx_open_file_del_event(file); |
1453 | 664 |
665 if (file->count) { | |
666 return; | |
667 } | |
668 | |
1772 | 669 if (file->fd != NGX_INVALID_FILE) { |
670 | |
671 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
672 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
673 ngx_close_file_n " \"%s\" failed", file->name); | |
674 } | |
675 | |
676 file->fd = NGX_INVALID_FILE; | |
677 } | |
678 | |
679 if (!file->close) { | |
680 return; | |
1453 | 681 } |
682 | |
683 ngx_free(file->name); | |
684 ngx_free(file); | |
685 } | |
686 | |
687 | |
688 static void | |
1775 | 689 ngx_open_file_del_event(ngx_cached_open_file_t *file) |
690 { | |
691 if (file->event == NULL) { | |
692 return; | |
693 } | |
694 | |
695 (void) ngx_del_event(file->event, NGX_VNODE_EVENT, | |
696 file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT); | |
697 | |
698 ngx_free(file->event->data); | |
699 ngx_free(file->event); | |
700 file->event = NULL; | |
701 file->use_event = 0; | |
702 } | |
703 | |
704 | |
705 static void | |
1453 | 706 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n, |
707 ngx_log_t *log) | |
708 { | |
709 time_t now; | |
1765 | 710 ngx_queue_t *q; |
1453 | 711 ngx_cached_open_file_t *file; |
712 | |
713 now = ngx_time(); | |
714 | |
715 /* | |
716 * n == 1 deletes one or two inactive files | |
717 * n == 0 deletes least recently used file by force | |
718 * and one or two inactive files | |
719 */ | |
720 | |
721 while (n < 3) { | |
722 | |
1765 | 723 if (ngx_queue_empty(&cache->expire_queue)) { |
1453 | 724 return; |
725 } | |
726 | |
1765 | 727 q = ngx_queue_last(&cache->expire_queue); |
728 | |
729 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
730 | |
1453 | 731 if (n++ != 0 && now - file->accessed <= cache->inactive) { |
732 return; | |
733 } | |
734 | |
1765 | 735 ngx_queue_remove(q); |
1453 | 736 |
737 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
738 | |
739 cache->current--; | |
740 | |
741 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, | |
742 "expire cached open file: %s", file->name); | |
743 | |
744 if (!file->err && !file->is_dir) { | |
745 file->close = 1; | |
1772 | 746 ngx_close_cached_file(cache, file, 0, log); |
1453 | 747 |
748 } else { | |
749 ngx_free(file->name); | |
750 ngx_free(file); | |
751 } | |
752 } | |
753 } | |
754 | |
755 | |
756 static void | |
757 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
758 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
759 { | |
760 ngx_rbtree_node_t **p; | |
761 ngx_cached_open_file_t *file, *file_temp; | |
762 | |
763 for ( ;; ) { | |
764 | |
765 if (node->key < temp->key) { | |
766 | |
767 p = &temp->left; | |
768 | |
769 } else if (node->key > temp->key) { | |
770 | |
771 p = &temp->right; | |
772 | |
773 } else { /* node->key == temp->key */ | |
774 | |
775 file = (ngx_cached_open_file_t *) node; | |
776 file_temp = (ngx_cached_open_file_t *) temp; | |
777 | |
778 p = (ngx_strcmp(file->name, file_temp->name) < 0) | |
779 ? &temp->left : &temp->right; | |
780 } | |
781 | |
782 if (*p == sentinel) { | |
783 break; | |
784 } | |
785 | |
786 temp = *p; | |
787 } | |
788 | |
789 *p = node; | |
790 node->parent = temp; | |
791 node->left = sentinel; | |
792 node->right = sentinel; | |
793 ngx_rbt_red(node); | |
794 } | |
795 | |
796 | |
1775 | 797 static ngx_cached_open_file_t * |
798 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
799 uint32_t hash) | |
800 { | |
801 ngx_int_t rc; | |
802 ngx_rbtree_node_t *node, *sentinel; | |
803 ngx_cached_open_file_t *file; | |
804 | |
805 node = cache->rbtree.root; | |
806 sentinel = cache->rbtree.sentinel; | |
807 | |
808 while (node != sentinel) { | |
809 | |
810 if (hash < node->key) { | |
811 node = node->left; | |
812 continue; | |
813 } | |
814 | |
815 if (hash > node->key) { | |
816 node = node->right; | |
817 continue; | |
818 } | |
819 | |
820 /* hash == node->key */ | |
821 | |
822 do { | |
823 file = (ngx_cached_open_file_t *) node; | |
824 | |
825 rc = ngx_strcmp(name->data, file->name); | |
826 | |
827 if (rc == 0) { | |
828 return file; | |
829 } | |
830 | |
831 node = (rc < 0) ? node->left : node->right; | |
832 | |
833 } while (node != sentinel && hash == node->key); | |
834 | |
835 break; | |
836 } | |
837 | |
838 return NULL; | |
839 } | |
840 | |
841 | |
1453 | 842 static void |
843 ngx_open_file_cache_remove(ngx_event_t *ev) | |
844 { | |
845 ngx_cached_open_file_t *file; | |
846 ngx_open_file_cache_event_t *fev; | |
847 | |
848 fev = ev->data; | |
849 file = fev->file; | |
850 | |
1765 | 851 ngx_queue_remove(&file->queue); |
1453 | 852 |
853 ngx_rbtree_delete(&fev->cache->rbtree, &file->node); | |
854 | |
855 fev->cache->current--; | |
856 | |
857 /* NGX_ONESHOT_EVENT was already deleted */ | |
858 file->event = NULL; | |
2070 | 859 file->use_event = 0; |
1453 | 860 |
861 file->close = 1; | |
862 | |
1772 | 863 ngx_close_cached_file(fev->cache, file, 0, ev->log); |
1453 | 864 |
865 /* free memory only when fev->cache and fev->file are already not needed */ | |
866 | |
867 ngx_free(ev->data); | |
868 ngx_free(ev); | |
869 } |