Mercurial > hg > nginx-quic
annotate src/core/ngx_open_file_cache.c @ 2233:f62751dab60d
remove unused #include's
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Fri, 05 Sep 2008 15:25:47 +0000 |
parents | 8564129d49b6 |
children | 987831d73bd8 |
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); | |
2231
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
505 |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
506 } else { |
8564129d49b6
*) handle unaligned file part for directio
Igor Sysoev <igor@sysoev.ru>
parents:
2129
diff
changeset
|
507 of->is_directio = 1; |
2129 | 508 } |
509 } | |
1453 | 510 } |
511 | |
2069
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
512 done: |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
513 |
1453 | 514 of->uniq = ngx_file_uniq(&fi); |
515 of->mtime = ngx_file_mtime(&fi); | |
516 of->size = ngx_file_size(&fi); | |
517 of->is_dir = ngx_is_dir(&fi); | |
518 of->is_file = ngx_is_file(&fi); | |
519 of->is_link = ngx_is_link(&fi); | |
520 of->is_exec = ngx_is_exec(&fi); | |
521 | |
522 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
|
523 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
524 failed: |
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 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
|
527 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
|
528 |
23930ccd2642
use ngx_file_info() and test uniq if file is already open
Igor Sysoev <igor@sysoev.ru>
parents:
2063
diff
changeset
|
529 return NGX_ERROR; |
1453 | 530 } |
531 | |
532 | |
1775 | 533 /* |
534 * we ignore any possible event setting error and | |
535 * fallback to usual periodic file retests | |
536 */ | |
537 | |
538 static void | |
539 ngx_open_file_add_event(ngx_open_file_cache_t *cache, | |
540 ngx_cached_open_file_t *file, ngx_open_file_info_t *of, ngx_log_t *log) | |
541 { | |
542 ngx_open_file_cache_event_t *fev; | |
543 | |
544 if (!(ngx_event_flags & NGX_USE_VNODE_EVENT) | |
545 || !of->events | |
546 || file->event | |
547 || of->fd == NGX_INVALID_FILE | |
548 || file->uses < of->min_uses) | |
549 { | |
550 return; | |
551 } | |
552 | |
2070 | 553 file->use_event = 0; |
554 | |
1775 | 555 file->event = ngx_calloc(sizeof(ngx_event_t), log); |
556 if (file->event== NULL) { | |
557 return; | |
558 } | |
559 | |
560 fev = ngx_alloc(sizeof(ngx_open_file_cache_event_t), log); | |
561 if (fev == NULL) { | |
562 ngx_free(file->event); | |
563 file->event = NULL; | |
564 return; | |
565 } | |
566 | |
567 fev->fd = of->fd; | |
568 fev->file = file; | |
569 fev->cache = cache; | |
570 | |
571 file->event->handler = ngx_open_file_cache_remove; | |
572 file->event->data = fev; | |
573 | |
574 /* | |
575 * although vnode event may be called while ngx_cycle->poll | |
576 * destruction, however, cleanup procedures are run before any | |
577 * memory freeing and events will be canceled. | |
578 */ | |
579 | |
580 file->event->log = ngx_cycle->log; | |
581 | |
582 if (ngx_add_event(file->event, NGX_VNODE_EVENT, NGX_ONESHOT_EVENT) | |
583 != NGX_OK) | |
584 { | |
585 ngx_free(file->event->data); | |
586 ngx_free(file->event); | |
587 file->event = NULL; | |
588 return; | |
589 } | |
590 | |
591 /* | |
2071 | 592 * we do not set file->use_event here because there may be a race |
593 * condition: a file may be deleted between opening the file and | |
594 * adding event, so we rely upon event notification only after | |
595 * one file revalidation on next file access | |
1775 | 596 */ |
597 | |
598 return; | |
599 } | |
600 | |
601 | |
1453 | 602 static void |
603 ngx_open_file_cleanup(void *data) | |
604 { | |
605 ngx_open_file_cache_cleanup_t *c = data; | |
606 | |
607 c->file->count--; | |
608 | |
1772 | 609 ngx_close_cached_file(c->cache, c->file, c->min_uses, c->log); |
1453 | 610 |
611 /* drop one or two expired open files */ | |
612 ngx_expire_old_cached_files(c->cache, 1, c->log); | |
613 } | |
614 | |
615 | |
616 static void | |
617 ngx_close_cached_file(ngx_open_file_cache_t *cache, | |
1772 | 618 ngx_cached_open_file_t *file, ngx_uint_t min_uses, ngx_log_t *log) |
1453 | 619 { |
1772 | 620 ngx_log_debug5(NGX_LOG_DEBUG_CORE, log, 0, |
621 "close cached open file: %s, fd:%d, c:%d, u:%d, %d", | |
622 file->name, file->fd, file->count, file->uses, file->close); | |
1453 | 623 |
624 if (!file->close) { | |
625 | |
626 file->accessed = ngx_time(); | |
627 | |
1765 | 628 ngx_queue_remove(&file->queue); |
1453 | 629 |
1765 | 630 ngx_queue_insert_head(&cache->expire_queue, &file->queue); |
1453 | 631 |
1772 | 632 if (file->uses >= min_uses || file->count) { |
633 return; | |
634 } | |
1453 | 635 } |
636 | |
1775 | 637 ngx_open_file_del_event(file); |
1453 | 638 |
639 if (file->count) { | |
640 return; | |
641 } | |
642 | |
1772 | 643 if (file->fd != NGX_INVALID_FILE) { |
644 | |
645 if (ngx_close_file(file->fd) == NGX_FILE_ERROR) { | |
646 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno, | |
647 ngx_close_file_n " \"%s\" failed", file->name); | |
648 } | |
649 | |
650 file->fd = NGX_INVALID_FILE; | |
651 } | |
652 | |
653 if (!file->close) { | |
654 return; | |
1453 | 655 } |
656 | |
657 ngx_free(file->name); | |
658 ngx_free(file); | |
659 } | |
660 | |
661 | |
662 static void | |
1775 | 663 ngx_open_file_del_event(ngx_cached_open_file_t *file) |
664 { | |
665 if (file->event == NULL) { | |
666 return; | |
667 } | |
668 | |
669 (void) ngx_del_event(file->event, NGX_VNODE_EVENT, | |
670 file->count ? NGX_FLUSH_EVENT : NGX_CLOSE_EVENT); | |
671 | |
672 ngx_free(file->event->data); | |
673 ngx_free(file->event); | |
674 file->event = NULL; | |
675 file->use_event = 0; | |
676 } | |
677 | |
678 | |
679 static void | |
1453 | 680 ngx_expire_old_cached_files(ngx_open_file_cache_t *cache, ngx_uint_t n, |
681 ngx_log_t *log) | |
682 { | |
683 time_t now; | |
1765 | 684 ngx_queue_t *q; |
1453 | 685 ngx_cached_open_file_t *file; |
686 | |
687 now = ngx_time(); | |
688 | |
689 /* | |
690 * n == 1 deletes one or two inactive files | |
691 * n == 0 deletes least recently used file by force | |
692 * and one or two inactive files | |
693 */ | |
694 | |
695 while (n < 3) { | |
696 | |
1765 | 697 if (ngx_queue_empty(&cache->expire_queue)) { |
1453 | 698 return; |
699 } | |
700 | |
1765 | 701 q = ngx_queue_last(&cache->expire_queue); |
702 | |
703 file = ngx_queue_data(q, ngx_cached_open_file_t, queue); | |
704 | |
1453 | 705 if (n++ != 0 && now - file->accessed <= cache->inactive) { |
706 return; | |
707 } | |
708 | |
1765 | 709 ngx_queue_remove(q); |
1453 | 710 |
711 ngx_rbtree_delete(&cache->rbtree, &file->node); | |
712 | |
713 cache->current--; | |
714 | |
715 ngx_log_debug1(NGX_LOG_DEBUG_CORE, log, 0, | |
716 "expire cached open file: %s", file->name); | |
717 | |
718 if (!file->err && !file->is_dir) { | |
719 file->close = 1; | |
1772 | 720 ngx_close_cached_file(cache, file, 0, log); |
1453 | 721 |
722 } else { | |
723 ngx_free(file->name); | |
724 ngx_free(file); | |
725 } | |
726 } | |
727 } | |
728 | |
729 | |
730 static void | |
731 ngx_open_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp, | |
732 ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel) | |
733 { | |
734 ngx_rbtree_node_t **p; | |
735 ngx_cached_open_file_t *file, *file_temp; | |
736 | |
737 for ( ;; ) { | |
738 | |
739 if (node->key < temp->key) { | |
740 | |
741 p = &temp->left; | |
742 | |
743 } else if (node->key > temp->key) { | |
744 | |
745 p = &temp->right; | |
746 | |
747 } else { /* node->key == temp->key */ | |
748 | |
749 file = (ngx_cached_open_file_t *) node; | |
750 file_temp = (ngx_cached_open_file_t *) temp; | |
751 | |
752 p = (ngx_strcmp(file->name, file_temp->name) < 0) | |
753 ? &temp->left : &temp->right; | |
754 } | |
755 | |
756 if (*p == sentinel) { | |
757 break; | |
758 } | |
759 | |
760 temp = *p; | |
761 } | |
762 | |
763 *p = node; | |
764 node->parent = temp; | |
765 node->left = sentinel; | |
766 node->right = sentinel; | |
767 ngx_rbt_red(node); | |
768 } | |
769 | |
770 | |
1775 | 771 static ngx_cached_open_file_t * |
772 ngx_open_file_lookup(ngx_open_file_cache_t *cache, ngx_str_t *name, | |
773 uint32_t hash) | |
774 { | |
775 ngx_int_t rc; | |
776 ngx_rbtree_node_t *node, *sentinel; | |
777 ngx_cached_open_file_t *file; | |
778 | |
779 node = cache->rbtree.root; | |
780 sentinel = cache->rbtree.sentinel; | |
781 | |
782 while (node != sentinel) { | |
783 | |
784 if (hash < node->key) { | |
785 node = node->left; | |
786 continue; | |
787 } | |
788 | |
789 if (hash > node->key) { | |
790 node = node->right; | |
791 continue; | |
792 } | |
793 | |
794 /* hash == node->key */ | |
795 | |
796 do { | |
797 file = (ngx_cached_open_file_t *) node; | |
798 | |
799 rc = ngx_strcmp(name->data, file->name); | |
800 | |
801 if (rc == 0) { | |
802 return file; | |
803 } | |
804 | |
805 node = (rc < 0) ? node->left : node->right; | |
806 | |
807 } while (node != sentinel && hash == node->key); | |
808 | |
809 break; | |
810 } | |
811 | |
812 return NULL; | |
813 } | |
814 | |
815 | |
1453 | 816 static void |
817 ngx_open_file_cache_remove(ngx_event_t *ev) | |
818 { | |
819 ngx_cached_open_file_t *file; | |
820 ngx_open_file_cache_event_t *fev; | |
821 | |
822 fev = ev->data; | |
823 file = fev->file; | |
824 | |
1765 | 825 ngx_queue_remove(&file->queue); |
1453 | 826 |
827 ngx_rbtree_delete(&fev->cache->rbtree, &file->node); | |
828 | |
829 fev->cache->current--; | |
830 | |
831 /* NGX_ONESHOT_EVENT was already deleted */ | |
832 file->event = NULL; | |
2070 | 833 file->use_event = 0; |
1453 | 834 |
835 file->close = 1; | |
836 | |
1772 | 837 ngx_close_cached_file(fev->cache, file, 0, ev->log); |
1453 | 838 |
839 /* free memory only when fev->cache and fev->file are already not needed */ | |
840 | |
841 ngx_free(ev->data); | |
842 ngx_free(ev); | |
843 } |