1 
2 /*
3  * Copyright (C) Igor Sysoev
4  * Copyright (C) Nginx, Inc.
5  */
6 
7 
8 #include <ngx_config.h>
9 #include <ngx_core.h>
10 #include <ngx_http.h>
11 #include <ngx_md5.h>
12 
13 
14 static ngx_int_t ngx_http_file_cache_lock(ngx_http_request_t *r,
15     ngx_http_cache_t *c);
16 static void ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev);
17 static void ngx_http_file_cache_lock_wait(ngx_http_request_t *r,
18     ngx_http_cache_t *c);
19 static ngx_int_t ngx_http_file_cache_read(ngx_http_request_t *r,
20     ngx_http_cache_t *c);
21 static ssize_t ngx_http_file_cache_aio_read(ngx_http_request_t *r,
22     ngx_http_cache_t *c);
23 #if (NGX_HAVE_FILE_AIO)
24 static void ngx_http_cache_aio_event_handler(ngx_event_t *ev);
25 #endif
26 #if (NGX_THREADS)
27 static ngx_int_t ngx_http_cache_thread_handler(ngx_thread_task_t *task,
28     ngx_file_t *file);
29 static void ngx_http_cache_thread_event_handler(ngx_event_t *ev);
30 #endif
31 static ngx_int_t ngx_http_file_cache_exists(ngx_http_file_cache_t *cache,
32     ngx_http_cache_t *c);
33 static ngx_int_t ngx_http_file_cache_name(ngx_http_request_t *r,
34     ngx_path_t *path);
35 static ngx_http_file_cache_node_t *
36     ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key);
37 static void ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
38     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel);
39 static void ngx_http_file_cache_vary(ngx_http_request_t *r, u_char *vary,
40     size_t len, u_char *hash);
41 static void ngx_http_file_cache_vary_header(ngx_http_request_t *r,
42     ngx_md5_t *md5, ngx_str_t *name);
43 static ngx_int_t ngx_http_file_cache_reopen(ngx_http_request_t *r,
44     ngx_http_cache_t *c);
45 static ngx_int_t ngx_http_file_cache_update_variant(ngx_http_request_t *r,
46     ngx_http_cache_t *c);
47 static void ngx_http_file_cache_cleanup(void *data);
48 static time_t ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache);
49 static time_t ngx_http_file_cache_expire(ngx_http_file_cache_t *cache);
50 static void ngx_http_file_cache_delete(ngx_http_file_cache_t *cache,
51     ngx_queue_t *q, u_char *name);
52 static void ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache);
53 static ngx_int_t ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx,
54     ngx_str_t *path);
55 static ngx_int_t ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx,
56     ngx_str_t *path);
57 static ngx_int_t ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx,
58     ngx_str_t *path);
59 static ngx_int_t ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx,
60     ngx_str_t *path);
61 static ngx_int_t ngx_http_file_cache_add(ngx_http_file_cache_t *cache,
62     ngx_http_cache_t *c);
63 static ngx_int_t ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx,
64     ngx_str_t *path);
65 static void ngx_http_file_cache_set_watermark(ngx_http_file_cache_t *cache);
66 
67 
68 ngx_str_t  ngx_http_cache_status[] = {
69     ngx_string("MISS"),
70     ngx_string("BYPASS"),
71     ngx_string("EXPIRED"),
72     ngx_string("STALE"),
73     ngx_string("UPDATING"),
74     ngx_string("REVALIDATED"),
75     ngx_string("HIT")
76 };
77 
78 
79 static u_char  ngx_http_file_cache_key[] = { LF, 'K', 'E', 'Y', ':', ' ' };
80 
81 
82 static ngx_int_t
ngx_http_file_cache_init(ngx_shm_zone_t * shm_zone,void * data)83 ngx_http_file_cache_init(ngx_shm_zone_t *shm_zone, void *data)
84 {
85     ngx_http_file_cache_t  *ocache = data;
86 
87     size_t                  len;
88     ngx_uint_t              n;
89     ngx_http_file_cache_t  *cache;
90 
91     cache = shm_zone->data;
92 
93     if (ocache) {
94         if (ngx_strcmp(cache->path->name.data, ocache->path->name.data) != 0) {
95             ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
96                           "cache \"%V\" uses the \"%V\" cache path "
97                           "while previously it used the \"%V\" cache path",
98                           &shm_zone->shm.name, &cache->path->name,
99                           &ocache->path->name);
100 
101             return NGX_ERROR;
102         }
103 
104         for (n = 0; n < NGX_MAX_PATH_LEVEL; n++) {
105             if (cache->path->level[n] != ocache->path->level[n]) {
106                 ngx_log_error(NGX_LOG_EMERG, shm_zone->shm.log, 0,
107                               "cache \"%V\" had previously different levels",
108                               &shm_zone->shm.name);
109                 return NGX_ERROR;
110             }
111         }
112 
113         cache->sh = ocache->sh;
114 
115         cache->shpool = ocache->shpool;
116         cache->bsize = ocache->bsize;
117 
118         cache->max_size /= cache->bsize;
119 
120         if (!cache->sh->cold || cache->sh->loading) {
121             cache->path->loader = NULL;
122         }
123 
124         return NGX_OK;
125     }
126 
127     cache->shpool = (ngx_slab_pool_t *) shm_zone->shm.addr;
128 
129     if (shm_zone->shm.exists) {
130         cache->sh = cache->shpool->data;
131         cache->bsize = ngx_fs_bsize(cache->path->name.data);
132         cache->max_size /= cache->bsize;
133 
134         return NGX_OK;
135     }
136 
137     cache->sh = ngx_slab_alloc(cache->shpool, sizeof(ngx_http_file_cache_sh_t));
138     if (cache->sh == NULL) {
139         return NGX_ERROR;
140     }
141 
142     cache->shpool->data = cache->sh;
143 
144     ngx_rbtree_init(&cache->sh->rbtree, &cache->sh->sentinel,
145                     ngx_http_file_cache_rbtree_insert_value);
146 
147     ngx_queue_init(&cache->sh->queue);
148 
149     cache->sh->cold = 1;
150     cache->sh->loading = 0;
151     cache->sh->size = 0;
152     cache->sh->count = 0;
153     cache->sh->watermark = (ngx_uint_t) -1;
154 
155     cache->bsize = ngx_fs_bsize(cache->path->name.data);
156 
157     cache->max_size /= cache->bsize;
158 
159     len = sizeof(" in cache keys zone \"\"") + shm_zone->shm.name.len;
160 
161     cache->shpool->log_ctx = ngx_slab_alloc(cache->shpool, len);
162     if (cache->shpool->log_ctx == NULL) {
163         return NGX_ERROR;
164     }
165 
166     ngx_sprintf(cache->shpool->log_ctx, " in cache keys zone \"%V\"%Z",
167                 &shm_zone->shm.name);
168 
169     cache->shpool->log_nomem = 0;
170 
171     return NGX_OK;
172 }
173 
174 
175 ngx_int_t
ngx_http_file_cache_new(ngx_http_request_t * r)176 ngx_http_file_cache_new(ngx_http_request_t *r)
177 {
178     ngx_http_cache_t  *c;
179 
180     c = ngx_pcalloc(r->pool, sizeof(ngx_http_cache_t));
181     if (c == NULL) {
182         return NGX_ERROR;
183     }
184 
185     if (ngx_array_init(&c->keys, r->pool, 4, sizeof(ngx_str_t)) != NGX_OK) {
186         return NGX_ERROR;
187     }
188 
189     r->cache = c;
190     c->file.log = r->connection->log;
191     c->file.fd = NGX_INVALID_FILE;
192 
193     return NGX_OK;
194 }
195 
196 
197 ngx_int_t
ngx_http_file_cache_create(ngx_http_request_t * r)198 ngx_http_file_cache_create(ngx_http_request_t *r)
199 {
200     ngx_http_cache_t       *c;
201     ngx_pool_cleanup_t     *cln;
202     ngx_http_file_cache_t  *cache;
203 
204     c = r->cache;
205     cache = c->file_cache;
206 
207     cln = ngx_pool_cleanup_add(r->pool, 0);
208     if (cln == NULL) {
209         return NGX_ERROR;
210     }
211 
212     cln->handler = ngx_http_file_cache_cleanup;
213     cln->data = c;
214 
215     if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
216         return NGX_ERROR;
217     }
218 
219     if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
220         return NGX_ERROR;
221     }
222 
223     return NGX_OK;
224 }
225 
226 
227 void
ngx_http_file_cache_create_key(ngx_http_request_t * r)228 ngx_http_file_cache_create_key(ngx_http_request_t *r)
229 {
230     size_t             len;
231     ngx_str_t         *key;
232     ngx_uint_t         i;
233     ngx_md5_t          md5;
234     ngx_http_cache_t  *c;
235 
236     c = r->cache;
237 
238     len = 0;
239 
240     ngx_crc32_init(c->crc32);
241     ngx_md5_init(&md5);
242 
243     key = c->keys.elts;
244     for (i = 0; i < c->keys.nelts; i++) {
245         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
246                        "http cache key: \"%V\"", &key[i]);
247 
248         len += key[i].len;
249 
250         ngx_crc32_update(&c->crc32, key[i].data, key[i].len);
251         ngx_md5_update(&md5, key[i].data, key[i].len);
252     }
253 
254     c->header_start = sizeof(ngx_http_file_cache_header_t)
255                       + sizeof(ngx_http_file_cache_key) + len + 1;
256 
257     ngx_crc32_final(c->crc32);
258     ngx_md5_final(c->key, &md5);
259 
260     ngx_memcpy(c->main, c->key, NGX_HTTP_CACHE_KEY_LEN);
261 }
262 
263 
264 ngx_int_t
ngx_http_file_cache_open(ngx_http_request_t * r)265 ngx_http_file_cache_open(ngx_http_request_t *r)
266 {
267     ngx_int_t                  rc, rv;
268     ngx_uint_t                 test;
269     ngx_http_cache_t          *c;
270     ngx_pool_cleanup_t        *cln;
271     ngx_open_file_info_t       of;
272     ngx_http_file_cache_t     *cache;
273     ngx_http_core_loc_conf_t  *clcf;
274 
275     c = r->cache;
276 
277     if (c->waiting) {
278         return NGX_AGAIN;
279     }
280 
281     if (c->reading) {
282         return ngx_http_file_cache_read(r, c);
283     }
284 
285     cache = c->file_cache;
286 
287     if (c->node == NULL) {
288         cln = ngx_pool_cleanup_add(r->pool, 0);
289         if (cln == NULL) {
290             return NGX_ERROR;
291         }
292 
293         cln->handler = ngx_http_file_cache_cleanup;
294         cln->data = c;
295     }
296 
297     c->buffer_size = c->body_start;
298 
299     rc = ngx_http_file_cache_exists(cache, c);
300 
301     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
302                    "http file cache exists: %i e:%d", rc, c->exists);
303 
304     if (rc == NGX_ERROR) {
305         return rc;
306     }
307 
308     if (rc == NGX_AGAIN) {
309         return NGX_HTTP_CACHE_SCARCE;
310     }
311 
312     if (rc == NGX_OK) {
313 
314         if (c->error) {
315             return c->error;
316         }
317 
318         c->temp_file = 1;
319         test = c->exists ? 1 : 0;
320         rv = NGX_DECLINED;
321 
322     } else { /* rc == NGX_DECLINED */
323 
324         test = cache->sh->cold ? 1 : 0;
325 
326         if (c->min_uses > 1) {
327 
328             if (!test) {
329                 return NGX_HTTP_CACHE_SCARCE;
330             }
331 
332             rv = NGX_HTTP_CACHE_SCARCE;
333 
334         } else {
335             c->temp_file = 1;
336             rv = NGX_DECLINED;
337         }
338     }
339 
340     if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
341         return NGX_ERROR;
342     }
343 
344     if (!test) {
345         goto done;
346     }
347 
348     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
349 
350     ngx_memzero(&of, sizeof(ngx_open_file_info_t));
351 
352     of.uniq = c->uniq;
353     of.valid = clcf->open_file_cache_valid;
354     of.min_uses = clcf->open_file_cache_min_uses;
355     of.events = clcf->open_file_cache_events;
356     of.directio = NGX_OPEN_FILE_DIRECTIO_OFF;
357     of.read_ahead = clcf->read_ahead;
358 
359     if (ngx_open_cached_file(clcf->open_file_cache, &c->file.name, &of, r->pool)
360         != NGX_OK)
361     {
362         switch (of.err) {
363 
364         case 0:
365             return NGX_ERROR;
366 
367         case NGX_ENOENT:
368         case NGX_ENOTDIR:
369             goto done;
370 
371         default:
372             ngx_log_error(NGX_LOG_CRIT, r->connection->log, of.err,
373                           ngx_open_file_n " \"%s\" failed", c->file.name.data);
374             return NGX_ERROR;
375         }
376     }
377 
378     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
379                    "http file cache fd: %d", of.fd);
380 
381     c->file.fd = of.fd;
382     c->file.log = r->connection->log;
383     c->uniq = of.uniq;
384     c->length = of.size;
385     c->fs_size = (of.fs_size + cache->bsize - 1) / cache->bsize;
386 
387     c->buf = ngx_create_temp_buf(r->pool, c->body_start);
388     if (c->buf == NULL) {
389         return NGX_ERROR;
390     }
391 
392     return ngx_http_file_cache_read(r, c);
393 
394 done:
395 
396     if (rv == NGX_DECLINED) {
397         return ngx_http_file_cache_lock(r, c);
398     }
399 
400     return rv;
401 }
402 
403 
404 static ngx_int_t
ngx_http_file_cache_lock(ngx_http_request_t * r,ngx_http_cache_t * c)405 ngx_http_file_cache_lock(ngx_http_request_t *r, ngx_http_cache_t *c)
406 {
407     ngx_msec_t                 now, timer;
408     ngx_http_file_cache_t     *cache;
409 
410     if (!c->lock) {
411         return NGX_DECLINED;
412     }
413 
414     now = ngx_current_msec;
415 
416     cache = c->file_cache;
417 
418     ngx_shmtx_lock(&cache->shpool->mutex);
419 
420     timer = c->node->lock_time - now;
421 
422     if (!c->node->updating || (ngx_msec_int_t) timer <= 0) {
423         c->node->updating = 1;
424         c->node->lock_time = now + c->lock_age;
425         c->updating = 1;
426         c->lock_time = c->node->lock_time;
427     }
428 
429     ngx_shmtx_unlock(&cache->shpool->mutex);
430 
431     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
432                    "http file cache lock u:%d wt:%M",
433                    c->updating, c->wait_time);
434 
435     if (c->updating) {
436         return NGX_DECLINED;
437     }
438 
439     if (c->lock_timeout == 0) {
440         return NGX_HTTP_CACHE_SCARCE;
441     }
442 
443     c->waiting = 1;
444 
445     if (c->wait_time == 0) {
446         c->wait_time = now + c->lock_timeout;
447 
448         c->wait_event.handler = ngx_http_file_cache_lock_wait_handler;
449         c->wait_event.data = r;
450         c->wait_event.log = r->connection->log;
451     }
452 
453     timer = c->wait_time - now;
454 
455     ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer);
456 
457     r->main->blocked++;
458 
459     return NGX_AGAIN;
460 }
461 
462 
463 static void
ngx_http_file_cache_lock_wait_handler(ngx_event_t * ev)464 ngx_http_file_cache_lock_wait_handler(ngx_event_t *ev)
465 {
466     ngx_connection_t    *c;
467     ngx_http_request_t  *r;
468 
469     r = ev->data;
470     c = r->connection;
471 
472     ngx_http_set_log_request(c->log, r);
473 
474     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
475                    "http file cache wait: \"%V?%V\"", &r->uri, &r->args);
476 
477     ngx_http_file_cache_lock_wait(r, r->cache);
478 
479     ngx_http_run_posted_requests(c);
480 }
481 
482 
483 static void
ngx_http_file_cache_lock_wait(ngx_http_request_t * r,ngx_http_cache_t * c)484 ngx_http_file_cache_lock_wait(ngx_http_request_t *r, ngx_http_cache_t *c)
485 {
486     ngx_uint_t              wait;
487     ngx_msec_t              now, timer;
488     ngx_http_file_cache_t  *cache;
489 
490     now = ngx_current_msec;
491 
492     timer = c->wait_time - now;
493 
494     if ((ngx_msec_int_t) timer <= 0) {
495         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
496                       "cache lock timeout");
497         c->lock_timeout = 0;
498         goto wakeup;
499     }
500 
501     cache = c->file_cache;
502     wait = 0;
503 
504     ngx_shmtx_lock(&cache->shpool->mutex);
505 
506     timer = c->node->lock_time - now;
507 
508     if (c->node->updating && (ngx_msec_int_t) timer > 0) {
509         wait = 1;
510     }
511 
512     ngx_shmtx_unlock(&cache->shpool->mutex);
513 
514     if (wait) {
515         ngx_add_timer(&c->wait_event, (timer > 500) ? 500 : timer);
516         return;
517     }
518 
519 wakeup:
520 
521     c->waiting = 0;
522     r->main->blocked--;
523     r->write_event_handler(r);
524 }
525 
526 
527 static ngx_int_t
ngx_http_file_cache_read(ngx_http_request_t * r,ngx_http_cache_t * c)528 ngx_http_file_cache_read(ngx_http_request_t *r, ngx_http_cache_t *c)
529 {
530     u_char                        *p;
531     time_t                         now;
532     ssize_t                        n;
533     ngx_str_t                     *key;
534     ngx_int_t                      rc;
535     ngx_uint_t                     i;
536     ngx_http_file_cache_t         *cache;
537     ngx_http_file_cache_header_t  *h;
538 
539     n = ngx_http_file_cache_aio_read(r, c);
540 
541     if (n < 0) {
542         return n;
543     }
544 
545     if ((size_t) n < c->header_start) {
546         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
547                       "cache file \"%s\" is too small", c->file.name.data);
548         return NGX_DECLINED;
549     }
550 
551     h = (ngx_http_file_cache_header_t *) c->buf->pos;
552 
553     if (h->version != NGX_HTTP_CACHE_VERSION) {
554         ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
555                       "cache file \"%s\" version mismatch", c->file.name.data);
556         return NGX_DECLINED;
557     }
558 
559     if (h->crc32 != c->crc32 || (size_t) h->header_start != c->header_start) {
560         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
561                       "cache file \"%s\" has md5 collision", c->file.name.data);
562         return NGX_DECLINED;
563     }
564 
565     p = c->buf->pos + sizeof(ngx_http_file_cache_header_t)
566         + sizeof(ngx_http_file_cache_key);
567 
568     key = c->keys.elts;
569     for (i = 0; i < c->keys.nelts; i++) {
570         if (ngx_memcmp(p, key[i].data, key[i].len) != 0) {
571             ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
572                           "cache file \"%s\" has md5 collision",
573                           c->file.name.data);
574             return NGX_DECLINED;
575         }
576 
577         p += key[i].len;
578     }
579 
580     if ((size_t) h->body_start > c->body_start) {
581         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
582                       "cache file \"%s\" has too long header",
583                       c->file.name.data);
584         return NGX_DECLINED;
585     }
586 
587     if (h->vary_len > NGX_HTTP_CACHE_VARY_LEN) {
588         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
589                       "cache file \"%s\" has incorrect vary length",
590                       c->file.name.data);
591         return NGX_DECLINED;
592     }
593 
594     if (h->vary_len) {
595         ngx_http_file_cache_vary(r, h->vary, h->vary_len, c->variant);
596 
597         if (ngx_memcmp(c->variant, h->variant, NGX_HTTP_CACHE_KEY_LEN) != 0) {
598             ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
599                            "http file cache vary mismatch");
600             return ngx_http_file_cache_reopen(r, c);
601         }
602     }
603 
604     c->buf->last += n;
605 
606     c->valid_sec = h->valid_sec;
607     c->updating_sec = h->updating_sec;
608     c->error_sec = h->error_sec;
609     c->last_modified = h->last_modified;
610     c->date = h->date;
611     c->valid_msec = h->valid_msec;
612     c->body_start = h->body_start;
613     c->etag.len = h->etag_len;
614     c->etag.data = h->etag;
615 
616     r->cached = 1;
617 
618     cache = c->file_cache;
619 
620     if (cache->sh->cold) {
621 
622         ngx_shmtx_lock(&cache->shpool->mutex);
623 
624         if (!c->node->exists) {
625             c->node->uses = 1;
626             c->node->body_start = c->body_start;
627             c->node->exists = 1;
628             c->node->uniq = c->uniq;
629             c->node->fs_size = c->fs_size;
630 
631             cache->sh->size += c->fs_size;
632         }
633 
634         ngx_shmtx_unlock(&cache->shpool->mutex);
635     }
636 
637     now = ngx_time();
638 
639     if (c->valid_sec < now) {
640         c->stale_updating = c->valid_sec + c->updating_sec >= now;
641         c->stale_error = c->valid_sec + c->error_sec >= now;
642 
643         ngx_shmtx_lock(&cache->shpool->mutex);
644 
645         if (c->node->updating) {
646             rc = NGX_HTTP_CACHE_UPDATING;
647 
648         } else {
649             c->node->updating = 1;
650             c->updating = 1;
651             c->lock_time = c->node->lock_time;
652             rc = NGX_HTTP_CACHE_STALE;
653         }
654 
655         ngx_shmtx_unlock(&cache->shpool->mutex);
656 
657         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
658                        "http file cache expired: %i %T %T",
659                        rc, c->valid_sec, now);
660 
661         return rc;
662     }
663 
664     return NGX_OK;
665 }
666 
667 
668 static ssize_t
ngx_http_file_cache_aio_read(ngx_http_request_t * r,ngx_http_cache_t * c)669 ngx_http_file_cache_aio_read(ngx_http_request_t *r, ngx_http_cache_t *c)
670 {
671 #if (NGX_HAVE_FILE_AIO || NGX_THREADS)
672     ssize_t                    n;
673     ngx_http_core_loc_conf_t  *clcf;
674 
675     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
676 #endif
677 
678 #if (NGX_HAVE_FILE_AIO)
679 
680     if (clcf->aio == NGX_HTTP_AIO_ON && ngx_file_aio) {
681         n = ngx_file_aio_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
682 
683         if (n != NGX_AGAIN) {
684             c->reading = 0;
685             return n;
686         }
687 
688         c->reading = 1;
689 
690         c->file.aio->data = r;
691         c->file.aio->handler = ngx_http_cache_aio_event_handler;
692 
693         r->main->blocked++;
694         r->aio = 1;
695 
696         return NGX_AGAIN;
697     }
698 
699 #endif
700 
701 #if (NGX_THREADS)
702 
703     if (clcf->aio == NGX_HTTP_AIO_THREADS) {
704         c->file.thread_task = c->thread_task;
705         c->file.thread_handler = ngx_http_cache_thread_handler;
706         c->file.thread_ctx = r;
707 
708         n = ngx_thread_read(&c->file, c->buf->pos, c->body_start, 0, r->pool);
709 
710         c->thread_task = c->file.thread_task;
711         c->reading = (n == NGX_AGAIN);
712 
713         return n;
714     }
715 
716 #endif
717 
718     return ngx_read_file(&c->file, c->buf->pos, c->body_start, 0);
719 }
720 
721 
722 #if (NGX_HAVE_FILE_AIO)
723 
724 static void
ngx_http_cache_aio_event_handler(ngx_event_t * ev)725 ngx_http_cache_aio_event_handler(ngx_event_t *ev)
726 {
727     ngx_event_aio_t     *aio;
728     ngx_connection_t    *c;
729     ngx_http_request_t  *r;
730 
731     aio = ev->data;
732     r = aio->data;
733     c = r->connection;
734 
735     ngx_http_set_log_request(c->log, r);
736 
737     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
738                    "http file cache aio: \"%V?%V\"", &r->uri, &r->args);
739 
740     r->main->blocked--;
741     r->aio = 0;
742 
743     r->write_event_handler(r);
744 
745     ngx_http_run_posted_requests(c);
746 }
747 
748 #endif
749 
750 
751 #if (NGX_THREADS)
752 
753 static ngx_int_t
ngx_http_cache_thread_handler(ngx_thread_task_t * task,ngx_file_t * file)754 ngx_http_cache_thread_handler(ngx_thread_task_t *task, ngx_file_t *file)
755 {
756     ngx_str_t                  name;
757     ngx_thread_pool_t         *tp;
758     ngx_http_request_t        *r;
759     ngx_http_core_loc_conf_t  *clcf;
760 
761     r = file->thread_ctx;
762 
763     clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
764     tp = clcf->thread_pool;
765 
766     if (tp == NULL) {
767         if (ngx_http_complex_value(r, clcf->thread_pool_value, &name)
768             != NGX_OK)
769         {
770             return NGX_ERROR;
771         }
772 
773         tp = ngx_thread_pool_get((ngx_cycle_t *) ngx_cycle, &name);
774 
775         if (tp == NULL) {
776             ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
777                           "thread pool \"%V\" not found", &name);
778             return NGX_ERROR;
779         }
780     }
781 
782     task->event.data = r;
783     task->event.handler = ngx_http_cache_thread_event_handler;
784 
785     if (ngx_thread_task_post(tp, task) != NGX_OK) {
786         return NGX_ERROR;
787     }
788 
789     r->main->blocked++;
790     r->aio = 1;
791 
792     return NGX_OK;
793 }
794 
795 
796 static void
ngx_http_cache_thread_event_handler(ngx_event_t * ev)797 ngx_http_cache_thread_event_handler(ngx_event_t *ev)
798 {
799     ngx_connection_t    *c;
800     ngx_http_request_t  *r;
801 
802     r = ev->data;
803     c = r->connection;
804 
805     ngx_http_set_log_request(c->log, r);
806 
807     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
808                    "http file cache thread: \"%V?%V\"", &r->uri, &r->args);
809 
810     r->main->blocked--;
811     r->aio = 0;
812 
813     r->write_event_handler(r);
814 
815     ngx_http_run_posted_requests(c);
816 }
817 
818 #endif
819 
820 
821 static ngx_int_t
ngx_http_file_cache_exists(ngx_http_file_cache_t * cache,ngx_http_cache_t * c)822 ngx_http_file_cache_exists(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
823 {
824     ngx_int_t                    rc;
825     ngx_http_file_cache_node_t  *fcn;
826 
827     ngx_shmtx_lock(&cache->shpool->mutex);
828 
829     fcn = c->node;
830 
831     if (fcn == NULL) {
832         fcn = ngx_http_file_cache_lookup(cache, c->key);
833     }
834 
835     if (fcn) {
836         ngx_queue_remove(&fcn->queue);
837 
838         if (c->node == NULL) {
839             fcn->uses++;
840             fcn->count++;
841         }
842 
843         if (fcn->error) {
844 
845             if (fcn->valid_sec < ngx_time()) {
846                 goto renew;
847             }
848 
849             rc = NGX_OK;
850 
851             goto done;
852         }
853 
854         if (fcn->exists || fcn->uses >= c->min_uses) {
855 
856             c->exists = fcn->exists;
857             if (fcn->body_start && !c->update_variant) {
858                 c->body_start = fcn->body_start;
859             }
860 
861             rc = NGX_OK;
862 
863             goto done;
864         }
865 
866         rc = NGX_AGAIN;
867 
868         goto done;
869     }
870 
871     fcn = ngx_slab_calloc_locked(cache->shpool,
872                                  sizeof(ngx_http_file_cache_node_t));
873     if (fcn == NULL) {
874         ngx_http_file_cache_set_watermark(cache);
875 
876         ngx_shmtx_unlock(&cache->shpool->mutex);
877 
878         (void) ngx_http_file_cache_forced_expire(cache);
879 
880         ngx_shmtx_lock(&cache->shpool->mutex);
881 
882         fcn = ngx_slab_calloc_locked(cache->shpool,
883                                      sizeof(ngx_http_file_cache_node_t));
884         if (fcn == NULL) {
885             ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
886                           "could not allocate node%s", cache->shpool->log_ctx);
887             rc = NGX_ERROR;
888             goto failed;
889         }
890     }
891 
892     cache->sh->count++;
893 
894     ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
895 
896     ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
897                NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
898 
899     ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
900 
901     fcn->uses = 1;
902     fcn->count = 1;
903 
904 renew:
905 
906     rc = NGX_DECLINED;
907 
908     fcn->valid_msec = 0;
909     fcn->error = 0;
910     fcn->exists = 0;
911     fcn->valid_sec = 0;
912     fcn->uniq = 0;
913     fcn->body_start = 0;
914     fcn->fs_size = 0;
915 
916 done:
917 
918     fcn->expire = ngx_time() + cache->inactive;
919 
920     ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
921 
922     c->uniq = fcn->uniq;
923     c->error = fcn->error;
924     c->node = fcn;
925 
926 failed:
927 
928     ngx_shmtx_unlock(&cache->shpool->mutex);
929 
930     return rc;
931 }
932 
933 
934 static ngx_int_t
ngx_http_file_cache_name(ngx_http_request_t * r,ngx_path_t * path)935 ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
936 {
937     u_char            *p;
938     ngx_http_cache_t  *c;
939 
940     c = r->cache;
941 
942     if (c->file.name.len) {
943         return NGX_OK;
944     }
945 
946     c->file.name.len = path->name.len + 1 + path->len
947                        + 2 * NGX_HTTP_CACHE_KEY_LEN;
948 
949     c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
950     if (c->file.name.data == NULL) {
951         return NGX_ERROR;
952     }
953 
954     ngx_memcpy(c->file.name.data, path->name.data, path->name.len);
955 
956     p = c->file.name.data + path->name.len + 1 + path->len;
957     p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
958     *p = '\0';
959 
960     ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
961 
962     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
963                    "cache file: \"%s\"", c->file.name.data);
964 
965     return NGX_OK;
966 }
967 
968 
969 static ngx_http_file_cache_node_t *
ngx_http_file_cache_lookup(ngx_http_file_cache_t * cache,u_char * key)970 ngx_http_file_cache_lookup(ngx_http_file_cache_t *cache, u_char *key)
971 {
972     ngx_int_t                    rc;
973     ngx_rbtree_key_t             node_key;
974     ngx_rbtree_node_t           *node, *sentinel;
975     ngx_http_file_cache_node_t  *fcn;
976 
977     ngx_memcpy((u_char *) &node_key, key, sizeof(ngx_rbtree_key_t));
978 
979     node = cache->sh->rbtree.root;
980     sentinel = cache->sh->rbtree.sentinel;
981 
982     while (node != sentinel) {
983 
984         if (node_key < node->key) {
985             node = node->left;
986             continue;
987         }
988 
989         if (node_key > node->key) {
990             node = node->right;
991             continue;
992         }
993 
994         /* node_key == node->key */
995 
996         fcn = (ngx_http_file_cache_node_t *) node;
997 
998         rc = ngx_memcmp(&key[sizeof(ngx_rbtree_key_t)], fcn->key,
999                         NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
1000 
1001         if (rc == 0) {
1002             return fcn;
1003         }
1004 
1005         node = (rc < 0) ? node->left : node->right;
1006     }
1007 
1008     /* not found */
1009 
1010     return NULL;
1011 }
1012 
1013 
1014 static void
ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t * temp,ngx_rbtree_node_t * node,ngx_rbtree_node_t * sentinel)1015 ngx_http_file_cache_rbtree_insert_value(ngx_rbtree_node_t *temp,
1016     ngx_rbtree_node_t *node, ngx_rbtree_node_t *sentinel)
1017 {
1018     ngx_rbtree_node_t           **p;
1019     ngx_http_file_cache_node_t   *cn, *cnt;
1020 
1021     for ( ;; ) {
1022 
1023         if (node->key < temp->key) {
1024 
1025             p = &temp->left;
1026 
1027         } else if (node->key > temp->key) {
1028 
1029             p = &temp->right;
1030 
1031         } else { /* node->key == temp->key */
1032 
1033             cn = (ngx_http_file_cache_node_t *) node;
1034             cnt = (ngx_http_file_cache_node_t *) temp;
1035 
1036             p = (ngx_memcmp(cn->key, cnt->key,
1037                             NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t))
1038                  < 0)
1039                     ? &temp->left : &temp->right;
1040         }
1041 
1042         if (*p == sentinel) {
1043             break;
1044         }
1045 
1046         temp = *p;
1047     }
1048 
1049     *p = node;
1050     node->parent = temp;
1051     node->left = sentinel;
1052     node->right = sentinel;
1053     ngx_rbt_red(node);
1054 }
1055 
1056 
1057 static void
ngx_http_file_cache_vary(ngx_http_request_t * r,u_char * vary,size_t len,u_char * hash)1058 ngx_http_file_cache_vary(ngx_http_request_t *r, u_char *vary, size_t len,
1059     u_char *hash)
1060 {
1061     u_char     *p, *last;
1062     ngx_str_t   name;
1063     ngx_md5_t   md5;
1064     u_char      buf[NGX_HTTP_CACHE_VARY_LEN];
1065 
1066     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1067                    "http file cache vary: \"%*s\"", len, vary);
1068 
1069     ngx_md5_init(&md5);
1070     ngx_md5_update(&md5, r->cache->main, NGX_HTTP_CACHE_KEY_LEN);
1071 
1072     ngx_strlow(buf, vary, len);
1073 
1074     p = buf;
1075     last = buf + len;
1076 
1077     while (p < last) {
1078 
1079         while (p < last && (*p == ' ' || *p == ',')) { p++; }
1080 
1081         name.data = p;
1082 
1083         while (p < last && *p != ',' && *p != ' ') { p++; }
1084 
1085         name.len = p - name.data;
1086 
1087         if (name.len == 0) {
1088             break;
1089         }
1090 
1091         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1092                        "http file cache vary: %V", &name);
1093 
1094         ngx_md5_update(&md5, name.data, name.len);
1095         ngx_md5_update(&md5, (u_char *) ":", sizeof(":") - 1);
1096 
1097         ngx_http_file_cache_vary_header(r, &md5, &name);
1098 
1099         ngx_md5_update(&md5, (u_char *) CRLF, sizeof(CRLF) - 1);
1100     }
1101 
1102     ngx_md5_final(hash, &md5);
1103 }
1104 
1105 
1106 static void
ngx_http_file_cache_vary_header(ngx_http_request_t * r,ngx_md5_t * md5,ngx_str_t * name)1107 ngx_http_file_cache_vary_header(ngx_http_request_t *r, ngx_md5_t *md5,
1108     ngx_str_t *name)
1109 {
1110     size_t            len;
1111     u_char           *p, *start, *last;
1112     ngx_uint_t        i, multiple, normalize;
1113     ngx_list_part_t  *part;
1114     ngx_table_elt_t  *header;
1115 
1116     multiple = 0;
1117     normalize = 0;
1118 
1119     if (name->len == sizeof("Accept-Charset") - 1
1120         && ngx_strncasecmp(name->data, (u_char *) "Accept-Charset",
1121                            sizeof("Accept-Charset") - 1) == 0)
1122     {
1123         normalize = 1;
1124 
1125     } else if (name->len == sizeof("Accept-Encoding") - 1
1126         && ngx_strncasecmp(name->data, (u_char *) "Accept-Encoding",
1127                            sizeof("Accept-Encoding") - 1) == 0)
1128     {
1129         normalize = 1;
1130 
1131     } else if (name->len == sizeof("Accept-Language") - 1
1132         && ngx_strncasecmp(name->data, (u_char *) "Accept-Language",
1133                            sizeof("Accept-Language") - 1) == 0)
1134     {
1135         normalize = 1;
1136     }
1137 
1138     part = &r->headers_in.headers.part;
1139     header = part->elts;
1140 
1141     for (i = 0; /* void */ ; i++) {
1142 
1143         if (i >= part->nelts) {
1144             if (part->next == NULL) {
1145                 break;
1146             }
1147 
1148             part = part->next;
1149             header = part->elts;
1150             i = 0;
1151         }
1152 
1153         if (header[i].hash == 0) {
1154             continue;
1155         }
1156 
1157         if (header[i].key.len != name->len) {
1158             continue;
1159         }
1160 
1161         if (ngx_strncasecmp(header[i].key.data, name->data, name->len) != 0) {
1162             continue;
1163         }
1164 
1165         if (!normalize) {
1166 
1167             if (multiple) {
1168                 ngx_md5_update(md5, (u_char *) ",", sizeof(",") - 1);
1169             }
1170 
1171             ngx_md5_update(md5, header[i].value.data, header[i].value.len);
1172 
1173             multiple = 1;
1174 
1175             continue;
1176         }
1177 
1178         /* normalize spaces */
1179 
1180         p = header[i].value.data;
1181         last = p + header[i].value.len;
1182 
1183         while (p < last) {
1184 
1185             while (p < last && (*p == ' ' || *p == ',')) { p++; }
1186 
1187             start = p;
1188 
1189             while (p < last && *p != ',' && *p != ' ') { p++; }
1190 
1191             len = p - start;
1192 
1193             if (len == 0) {
1194                 break;
1195             }
1196 
1197             if (multiple) {
1198                 ngx_md5_update(md5, (u_char *) ",", sizeof(",") - 1);
1199             }
1200 
1201             ngx_md5_update(md5, start, len);
1202 
1203             multiple = 1;
1204         }
1205     }
1206 }
1207 
1208 
1209 static ngx_int_t
ngx_http_file_cache_reopen(ngx_http_request_t * r,ngx_http_cache_t * c)1210 ngx_http_file_cache_reopen(ngx_http_request_t *r, ngx_http_cache_t *c)
1211 {
1212     ngx_http_file_cache_t  *cache;
1213 
1214     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
1215                    "http file cache reopen");
1216 
1217     if (c->secondary) {
1218         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
1219                       "cache file \"%s\" has incorrect vary hash",
1220                       c->file.name.data);
1221         return NGX_DECLINED;
1222     }
1223 
1224     cache = c->file_cache;
1225 
1226     ngx_shmtx_lock(&cache->shpool->mutex);
1227 
1228     c->node->count--;
1229     c->node = NULL;
1230 
1231     ngx_shmtx_unlock(&cache->shpool->mutex);
1232 
1233     c->secondary = 1;
1234     c->file.name.len = 0;
1235     c->body_start = c->buffer_size;
1236 
1237     ngx_memcpy(c->key, c->variant, NGX_HTTP_CACHE_KEY_LEN);
1238 
1239     return ngx_http_file_cache_open(r);
1240 }
1241 
1242 
1243 ngx_int_t
ngx_http_file_cache_set_header(ngx_http_request_t * r,u_char * buf)1244 ngx_http_file_cache_set_header(ngx_http_request_t *r, u_char *buf)
1245 {
1246     ngx_http_file_cache_header_t  *h = (ngx_http_file_cache_header_t *) buf;
1247 
1248     u_char            *p;
1249     ngx_str_t         *key;
1250     ngx_uint_t         i;
1251     ngx_http_cache_t  *c;
1252 
1253     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1254                    "http file cache set header");
1255 
1256     c = r->cache;
1257 
1258     ngx_memzero(h, sizeof(ngx_http_file_cache_header_t));
1259 
1260     h->version = NGX_HTTP_CACHE_VERSION;
1261     h->valid_sec = c->valid_sec;
1262     h->updating_sec = c->updating_sec;
1263     h->error_sec = c->error_sec;
1264     h->last_modified = c->last_modified;
1265     h->date = c->date;
1266     h->crc32 = c->crc32;
1267     h->valid_msec = (u_short) c->valid_msec;
1268     h->header_start = (u_short) c->header_start;
1269     h->body_start = (u_short) c->body_start;
1270 
1271     if (c->etag.len <= NGX_HTTP_CACHE_ETAG_LEN) {
1272         h->etag_len = (u_char) c->etag.len;
1273         ngx_memcpy(h->etag, c->etag.data, c->etag.len);
1274     }
1275 
1276     if (c->vary.len) {
1277         if (c->vary.len > NGX_HTTP_CACHE_VARY_LEN) {
1278             /* should not happen */
1279             c->vary.len = NGX_HTTP_CACHE_VARY_LEN;
1280         }
1281 
1282         h->vary_len = (u_char) c->vary.len;
1283         ngx_memcpy(h->vary, c->vary.data, c->vary.len);
1284 
1285         ngx_http_file_cache_vary(r, c->vary.data, c->vary.len, c->variant);
1286         ngx_memcpy(h->variant, c->variant, NGX_HTTP_CACHE_KEY_LEN);
1287     }
1288 
1289     if (ngx_http_file_cache_update_variant(r, c) != NGX_OK) {
1290         return NGX_ERROR;
1291     }
1292 
1293     p = buf + sizeof(ngx_http_file_cache_header_t);
1294 
1295     p = ngx_cpymem(p, ngx_http_file_cache_key, sizeof(ngx_http_file_cache_key));
1296 
1297     key = c->keys.elts;
1298     for (i = 0; i < c->keys.nelts; i++) {
1299         p = ngx_copy(p, key[i].data, key[i].len);
1300     }
1301 
1302     *p = LF;
1303 
1304     return NGX_OK;
1305 }
1306 
1307 
1308 static ngx_int_t
ngx_http_file_cache_update_variant(ngx_http_request_t * r,ngx_http_cache_t * c)1309 ngx_http_file_cache_update_variant(ngx_http_request_t *r, ngx_http_cache_t *c)
1310 {
1311     ngx_http_file_cache_t  *cache;
1312 
1313     if (!c->secondary) {
1314         return NGX_OK;
1315     }
1316 
1317     if (c->vary.len
1318         && ngx_memcmp(c->variant, c->key, NGX_HTTP_CACHE_KEY_LEN) == 0)
1319     {
1320         return NGX_OK;
1321     }
1322 
1323     /*
1324      * if the variant hash doesn't match one we used as a secondary
1325      * cache key, switch back to the original key
1326      */
1327 
1328     cache = c->file_cache;
1329 
1330     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1331                    "http file cache main key");
1332 
1333     ngx_shmtx_lock(&cache->shpool->mutex);
1334 
1335     c->node->count--;
1336     c->node->updating = 0;
1337     c->node = NULL;
1338 
1339     ngx_shmtx_unlock(&cache->shpool->mutex);
1340 
1341     c->file.name.len = 0;
1342     c->update_variant = 1;
1343 
1344     ngx_memcpy(c->key, c->main, NGX_HTTP_CACHE_KEY_LEN);
1345 
1346     if (ngx_http_file_cache_exists(cache, c) == NGX_ERROR) {
1347         return NGX_ERROR;
1348     }
1349 
1350     if (ngx_http_file_cache_name(r, cache->path) != NGX_OK) {
1351         return NGX_ERROR;
1352     }
1353 
1354     return NGX_OK;
1355 }
1356 
1357 
1358 void
ngx_http_file_cache_update(ngx_http_request_t * r,ngx_temp_file_t * tf)1359 ngx_http_file_cache_update(ngx_http_request_t *r, ngx_temp_file_t *tf)
1360 {
1361     off_t                   fs_size;
1362     ngx_int_t               rc;
1363     ngx_file_uniq_t         uniq;
1364     ngx_file_info_t         fi;
1365     ngx_http_cache_t        *c;
1366     ngx_ext_rename_file_t   ext;
1367     ngx_http_file_cache_t  *cache;
1368 
1369     c = r->cache;
1370 
1371     if (c->updated) {
1372         return;
1373     }
1374 
1375     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1376                    "http file cache update");
1377 
1378     cache = c->file_cache;
1379 
1380     c->updated = 1;
1381     c->updating = 0;
1382 
1383     uniq = 0;
1384     fs_size = 0;
1385 
1386     ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1387                    "http file cache rename: \"%s\" to \"%s\"",
1388                    tf->file.name.data, c->file.name.data);
1389 
1390     ext.access = NGX_FILE_OWNER_ACCESS;
1391     ext.path_access = NGX_FILE_OWNER_ACCESS;
1392     ext.time = -1;
1393     ext.create_path = 1;
1394     ext.delete_file = 1;
1395     ext.log = r->connection->log;
1396 
1397     rc = ngx_ext_rename_file(&tf->file.name, &c->file.name, &ext);
1398 
1399     if (rc == NGX_OK) {
1400 
1401         if (ngx_fd_info(tf->file.fd, &fi) == NGX_FILE_ERROR) {
1402             ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
1403                           ngx_fd_info_n " \"%s\" failed", tf->file.name.data);
1404 
1405             rc = NGX_ERROR;
1406 
1407         } else {
1408             uniq = ngx_file_uniq(&fi);
1409             fs_size = (ngx_file_fs_size(&fi) + cache->bsize - 1) / cache->bsize;
1410         }
1411     }
1412 
1413     ngx_shmtx_lock(&cache->shpool->mutex);
1414 
1415     c->node->count--;
1416     c->node->error = 0;
1417     c->node->uniq = uniq;
1418     c->node->body_start = c->body_start;
1419 
1420     cache->sh->size += fs_size - c->node->fs_size;
1421     c->node->fs_size = fs_size;
1422 
1423     if (rc == NGX_OK) {
1424         c->node->exists = 1;
1425     }
1426 
1427     c->node->updating = 0;
1428 
1429     ngx_shmtx_unlock(&cache->shpool->mutex);
1430 }
1431 
1432 
1433 void
ngx_http_file_cache_update_header(ngx_http_request_t * r)1434 ngx_http_file_cache_update_header(ngx_http_request_t *r)
1435 {
1436     ssize_t                        n;
1437     ngx_err_t                      err;
1438     ngx_file_t                     file;
1439     ngx_file_info_t                fi;
1440     ngx_http_cache_t              *c;
1441     ngx_http_file_cache_header_t   h;
1442 
1443     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1444                    "http file cache update header");
1445 
1446     c = r->cache;
1447 
1448     ngx_memzero(&file, sizeof(ngx_file_t));
1449 
1450     file.name = c->file.name;
1451     file.log = r->connection->log;
1452     file.fd = ngx_open_file(file.name.data, NGX_FILE_RDWR, NGX_FILE_OPEN, 0);
1453 
1454     if (file.fd == NGX_INVALID_FILE) {
1455         err = ngx_errno;
1456 
1457         /* cache file may have been deleted */
1458 
1459         if (err == NGX_ENOENT) {
1460             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1461                            "http file cache \"%s\" not found",
1462                            file.name.data);
1463             return;
1464         }
1465 
1466         ngx_log_error(NGX_LOG_CRIT, r->connection->log, err,
1467                       ngx_open_file_n " \"%s\" failed", file.name.data);
1468         return;
1469     }
1470 
1471     /*
1472      * make sure cache file wasn't replaced;
1473      * if it was, do nothing
1474      */
1475 
1476     if (ngx_fd_info(file.fd, &fi) == NGX_FILE_ERROR) {
1477         ngx_log_error(NGX_LOG_CRIT, r->connection->log, ngx_errno,
1478                       ngx_fd_info_n " \"%s\" failed", file.name.data);
1479         goto done;
1480     }
1481 
1482     if (c->uniq != ngx_file_uniq(&fi)
1483         || c->length != ngx_file_size(&fi))
1484     {
1485         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1486                        "http file cache \"%s\" changed",
1487                        file.name.data);
1488         goto done;
1489     }
1490 
1491     n = ngx_read_file(&file, (u_char *) &h,
1492                       sizeof(ngx_http_file_cache_header_t), 0);
1493 
1494     if (n == NGX_ERROR) {
1495         goto done;
1496     }
1497 
1498     if ((size_t) n != sizeof(ngx_http_file_cache_header_t)) {
1499         ngx_log_error(NGX_LOG_CRIT, r->connection->log, 0,
1500                       ngx_read_file_n " read only %z of %z from \"%s\"",
1501                       n, sizeof(ngx_http_file_cache_header_t), file.name.data);
1502         goto done;
1503     }
1504 
1505     if (h.version != NGX_HTTP_CACHE_VERSION
1506         || h.last_modified != c->last_modified
1507         || h.crc32 != c->crc32
1508         || (size_t) h.header_start != c->header_start
1509         || (size_t) h.body_start != c->body_start)
1510     {
1511         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1512                        "http file cache \"%s\" content changed",
1513                        file.name.data);
1514         goto done;
1515     }
1516 
1517     /*
1518      * update cache file header with new data,
1519      * notably h.valid_sec and h.date
1520      */
1521 
1522     ngx_memzero(&h, sizeof(ngx_http_file_cache_header_t));
1523 
1524     h.version = NGX_HTTP_CACHE_VERSION;
1525     h.valid_sec = c->valid_sec;
1526     h.updating_sec = c->updating_sec;
1527     h.error_sec = c->error_sec;
1528     h.last_modified = c->last_modified;
1529     h.date = c->date;
1530     h.crc32 = c->crc32;
1531     h.valid_msec = (u_short) c->valid_msec;
1532     h.header_start = (u_short) c->header_start;
1533     h.body_start = (u_short) c->body_start;
1534 
1535     if (c->etag.len <= NGX_HTTP_CACHE_ETAG_LEN) {
1536         h.etag_len = (u_char) c->etag.len;
1537         ngx_memcpy(h.etag, c->etag.data, c->etag.len);
1538     }
1539 
1540     if (c->vary.len) {
1541         if (c->vary.len > NGX_HTTP_CACHE_VARY_LEN) {
1542             /* should not happen */
1543             c->vary.len = NGX_HTTP_CACHE_VARY_LEN;
1544         }
1545 
1546         h.vary_len = (u_char) c->vary.len;
1547         ngx_memcpy(h.vary, c->vary.data, c->vary.len);
1548 
1549         ngx_http_file_cache_vary(r, c->vary.data, c->vary.len, c->variant);
1550         ngx_memcpy(h.variant, c->variant, NGX_HTTP_CACHE_KEY_LEN);
1551     }
1552 
1553     (void) ngx_write_file(&file, (u_char *) &h,
1554                           sizeof(ngx_http_file_cache_header_t), 0);
1555 
1556 done:
1557 
1558     if (ngx_close_file(file.fd) == NGX_FILE_ERROR) {
1559         ngx_log_error(NGX_LOG_ALERT, r->connection->log, ngx_errno,
1560                       ngx_close_file_n " \"%s\" failed", file.name.data);
1561     }
1562 }
1563 
1564 
1565 ngx_int_t
ngx_http_cache_send(ngx_http_request_t * r)1566 ngx_http_cache_send(ngx_http_request_t *r)
1567 {
1568     ngx_int_t          rc;
1569     ngx_buf_t         *b;
1570     ngx_chain_t        out;
1571     ngx_http_cache_t  *c;
1572 
1573     c = r->cache;
1574 
1575     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
1576                    "http file cache send: %s", c->file.name.data);
1577 
1578     if (r != r->main && c->length - c->body_start == 0) {
1579         return ngx_http_send_header(r);
1580     }
1581 
1582     /* we need to allocate all before the header would be sent */
1583 
1584     b = ngx_calloc_buf(r->pool);
1585     if (b == NULL) {
1586         return NGX_HTTP_INTERNAL_SERVER_ERROR;
1587     }
1588 
1589     b->file = ngx_pcalloc(r->pool, sizeof(ngx_file_t));
1590     if (b->file == NULL) {
1591         return NGX_HTTP_INTERNAL_SERVER_ERROR;
1592     }
1593 
1594     rc = ngx_http_send_header(r);
1595 
1596     if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {
1597         return rc;
1598     }
1599 
1600     b->file_pos = c->body_start;
1601     b->file_last = c->length;
1602 
1603     b->in_file = (c->length - c->body_start) ? 1: 0;
1604     b->last_buf = (r == r->main) ? 1: 0;
1605     b->last_in_chain = 1;
1606 
1607     b->file->fd = c->file.fd;
1608     b->file->name = c->file.name;
1609     b->file->log = r->connection->log;
1610 
1611     out.buf = b;
1612     out.next = NULL;
1613 
1614     return ngx_http_output_filter(r, &out);
1615 }
1616 
1617 
1618 void
ngx_http_file_cache_free(ngx_http_cache_t * c,ngx_temp_file_t * tf)1619 ngx_http_file_cache_free(ngx_http_cache_t *c, ngx_temp_file_t *tf)
1620 {
1621     ngx_http_file_cache_t       *cache;
1622     ngx_http_file_cache_node_t  *fcn;
1623 
1624     if (c->updated || c->node == NULL) {
1625         return;
1626     }
1627 
1628     cache = c->file_cache;
1629 
1630     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
1631                    "http file cache free, fd: %d", c->file.fd);
1632 
1633     ngx_shmtx_lock(&cache->shpool->mutex);
1634 
1635     fcn = c->node;
1636     fcn->count--;
1637 
1638     if (c->updating && fcn->lock_time == c->lock_time) {
1639         fcn->updating = 0;
1640     }
1641 
1642     if (c->error) {
1643         fcn->error = c->error;
1644 
1645         if (c->valid_sec) {
1646             fcn->valid_sec = c->valid_sec;
1647             fcn->valid_msec = c->valid_msec;
1648         }
1649 
1650     } else if (!fcn->exists && fcn->count == 0 && c->min_uses == 1) {
1651         ngx_queue_remove(&fcn->queue);
1652         ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
1653         ngx_slab_free_locked(cache->shpool, fcn);
1654         cache->sh->count--;
1655         c->node = NULL;
1656     }
1657 
1658     ngx_shmtx_unlock(&cache->shpool->mutex);
1659 
1660     c->updated = 1;
1661     c->updating = 0;
1662 
1663     if (c->temp_file) {
1664         if (tf && tf->file.fd != NGX_INVALID_FILE) {
1665             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
1666                            "http file cache incomplete: \"%s\"",
1667                            tf->file.name.data);
1668 
1669             if (ngx_delete_file(tf->file.name.data) == NGX_FILE_ERROR) {
1670                 ngx_log_error(NGX_LOG_CRIT, c->file.log, ngx_errno,
1671                               ngx_delete_file_n " \"%s\" failed",
1672                               tf->file.name.data);
1673             }
1674         }
1675     }
1676 
1677     if (c->wait_event.timer_set) {
1678         ngx_del_timer(&c->wait_event);
1679     }
1680 }
1681 
1682 
1683 static void
ngx_http_file_cache_cleanup(void * data)1684 ngx_http_file_cache_cleanup(void *data)
1685 {
1686     ngx_http_cache_t  *c = data;
1687 
1688     if (c->updated) {
1689         return;
1690     }
1691 
1692     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->file.log, 0,
1693                    "http file cache cleanup");
1694 
1695     if (c->updating && !c->background) {
1696         ngx_log_error(NGX_LOG_ALERT, c->file.log, 0,
1697                       "stalled cache updating, error:%ui", c->error);
1698     }
1699 
1700     ngx_http_file_cache_free(c, NULL);
1701 }
1702 
1703 
1704 static time_t
ngx_http_file_cache_forced_expire(ngx_http_file_cache_t * cache)1705 ngx_http_file_cache_forced_expire(ngx_http_file_cache_t *cache)
1706 {
1707     u_char                      *name, *p;
1708     size_t                       len;
1709     time_t                       wait;
1710     ngx_uint_t                   tries;
1711     ngx_path_t                  *path;
1712     ngx_queue_t                 *q, *sentinel;
1713     ngx_http_file_cache_node_t  *fcn;
1714     u_char                       key[2 * NGX_HTTP_CACHE_KEY_LEN];
1715 
1716     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1717                    "http file cache forced expire");
1718 
1719     path = cache->path;
1720     len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
1721 
1722     name = ngx_alloc(len + 1, ngx_cycle->log);
1723     if (name == NULL) {
1724         return 10;
1725     }
1726 
1727     ngx_memcpy(name, path->name.data, path->name.len);
1728 
1729     wait = 10;
1730     tries = 20;
1731     sentinel = NULL;
1732 
1733     ngx_shmtx_lock(&cache->shpool->mutex);
1734 
1735     for ( ;; ) {
1736         if (ngx_queue_empty(&cache->sh->queue)) {
1737             break;
1738         }
1739 
1740         q = ngx_queue_last(&cache->sh->queue);
1741 
1742         if (q == sentinel) {
1743             break;
1744         }
1745 
1746         fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
1747 
1748         ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1749                   "http file cache forced expire: #%d %d %02xd%02xd%02xd%02xd",
1750                   fcn->count, fcn->exists,
1751                   fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
1752 
1753         if (fcn->count == 0) {
1754             ngx_http_file_cache_delete(cache, q, name);
1755             wait = 0;
1756             break;
1757         }
1758 
1759         p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
1760                          sizeof(ngx_rbtree_key_t));
1761         len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
1762         (void) ngx_hex_dump(p, fcn->key, len);
1763 
1764         /*
1765          * abnormally exited workers may leave locked cache entries,
1766          * and although it may be safe to remove them completely,
1767          * we prefer to just move them to the top of the inactive queue
1768          */
1769 
1770         ngx_queue_remove(q);
1771         fcn->expire = ngx_time() + cache->inactive;
1772         ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
1773 
1774         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
1775                       "ignore long locked inactive cache entry %*s, count:%d",
1776                       (size_t) 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
1777 
1778         if (sentinel == NULL) {
1779             sentinel = q;
1780         }
1781 
1782         if (--tries) {
1783             continue;
1784         }
1785 
1786         wait = 1;
1787         break;
1788     }
1789 
1790     ngx_shmtx_unlock(&cache->shpool->mutex);
1791 
1792     ngx_free(name);
1793 
1794     return wait;
1795 }
1796 
1797 
1798 static time_t
ngx_http_file_cache_expire(ngx_http_file_cache_t * cache)1799 ngx_http_file_cache_expire(ngx_http_file_cache_t *cache)
1800 {
1801     u_char                      *name, *p;
1802     size_t                       len;
1803     time_t                       now, wait;
1804     ngx_path_t                  *path;
1805     ngx_msec_t                   elapsed;
1806     ngx_queue_t                 *q;
1807     ngx_http_file_cache_node_t  *fcn;
1808     u_char                       key[2 * NGX_HTTP_CACHE_KEY_LEN];
1809 
1810     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1811                    "http file cache expire");
1812 
1813     path = cache->path;
1814     len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
1815 
1816     name = ngx_alloc(len + 1, ngx_cycle->log);
1817     if (name == NULL) {
1818         return 10;
1819     }
1820 
1821     ngx_memcpy(name, path->name.data, path->name.len);
1822 
1823     now = ngx_time();
1824 
1825     ngx_shmtx_lock(&cache->shpool->mutex);
1826 
1827     for ( ;; ) {
1828 
1829         if (ngx_quit || ngx_terminate) {
1830             wait = 1;
1831             break;
1832         }
1833 
1834         if (ngx_queue_empty(&cache->sh->queue)) {
1835             wait = 10;
1836             break;
1837         }
1838 
1839         q = ngx_queue_last(&cache->sh->queue);
1840 
1841         fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
1842 
1843         wait = fcn->expire - now;
1844 
1845         if (wait > 0) {
1846             wait = wait > 10 ? 10 : wait;
1847             break;
1848         }
1849 
1850         ngx_log_debug6(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1851                        "http file cache expire: #%d %d %02xd%02xd%02xd%02xd",
1852                        fcn->count, fcn->exists,
1853                        fcn->key[0], fcn->key[1], fcn->key[2], fcn->key[3]);
1854 
1855         if (fcn->count == 0) {
1856             ngx_http_file_cache_delete(cache, q, name);
1857             goto next;
1858         }
1859 
1860         if (fcn->deleting) {
1861             wait = 1;
1862             break;
1863         }
1864 
1865         p = ngx_hex_dump(key, (u_char *) &fcn->node.key,
1866                          sizeof(ngx_rbtree_key_t));
1867         len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
1868         (void) ngx_hex_dump(p, fcn->key, len);
1869 
1870         /*
1871          * abnormally exited workers may leave locked cache entries,
1872          * and although it may be safe to remove them completely,
1873          * we prefer to just move them to the top of the inactive queue
1874          */
1875 
1876         ngx_queue_remove(q);
1877         fcn->expire = ngx_time() + cache->inactive;
1878         ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
1879 
1880         ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
1881                       "ignore long locked inactive cache entry %*s, count:%d",
1882                       (size_t) 2 * NGX_HTTP_CACHE_KEY_LEN, key, fcn->count);
1883 
1884 next:
1885 
1886         if (++cache->files >= cache->manager_files) {
1887             wait = 0;
1888             break;
1889         }
1890 
1891         ngx_time_update();
1892 
1893         elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
1894 
1895         if (elapsed >= cache->manager_threshold) {
1896             wait = 0;
1897             break;
1898         }
1899     }
1900 
1901     ngx_shmtx_unlock(&cache->shpool->mutex);
1902 
1903     ngx_free(name);
1904 
1905     return wait;
1906 }
1907 
1908 
1909 static void
ngx_http_file_cache_delete(ngx_http_file_cache_t * cache,ngx_queue_t * q,u_char * name)1910 ngx_http_file_cache_delete(ngx_http_file_cache_t *cache, ngx_queue_t *q,
1911     u_char *name)
1912 {
1913     u_char                      *p;
1914     size_t                       len;
1915     ngx_path_t                  *path;
1916     ngx_http_file_cache_node_t  *fcn;
1917 
1918     fcn = ngx_queue_data(q, ngx_http_file_cache_node_t, queue);
1919 
1920     if (fcn->exists) {
1921         cache->sh->size -= fcn->fs_size;
1922 
1923         path = cache->path;
1924         p = name + path->name.len + 1 + path->len;
1925         p = ngx_hex_dump(p, (u_char *) &fcn->node.key,
1926                          sizeof(ngx_rbtree_key_t));
1927         len = NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t);
1928         p = ngx_hex_dump(p, fcn->key, len);
1929         *p = '\0';
1930 
1931         fcn->count++;
1932         fcn->deleting = 1;
1933         ngx_shmtx_unlock(&cache->shpool->mutex);
1934 
1935         len = path->name.len + 1 + path->len + 2 * NGX_HTTP_CACHE_KEY_LEN;
1936         ngx_create_hashed_filename(path, name, len);
1937 
1938         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1939                        "http file cache expire: \"%s\"", name);
1940 
1941         if (ngx_delete_file(name) == NGX_FILE_ERROR) {
1942             ngx_log_error(NGX_LOG_CRIT, ngx_cycle->log, ngx_errno,
1943                           ngx_delete_file_n " \"%s\" failed", name);
1944         }
1945 
1946         ngx_shmtx_lock(&cache->shpool->mutex);
1947         fcn->count--;
1948         fcn->deleting = 0;
1949     }
1950 
1951     if (fcn->count == 0) {
1952         ngx_queue_remove(q);
1953         ngx_rbtree_delete(&cache->sh->rbtree, &fcn->node);
1954         ngx_slab_free_locked(cache->shpool, fcn);
1955         cache->sh->count--;
1956     }
1957 }
1958 
1959 
1960 static ngx_msec_t
ngx_http_file_cache_manager(void * data)1961 ngx_http_file_cache_manager(void *data)
1962 {
1963     ngx_http_file_cache_t  *cache = data;
1964 
1965     off_t       size, free;
1966     time_t      wait;
1967     ngx_msec_t  elapsed, next;
1968     ngx_uint_t  count, watermark;
1969 
1970     cache->last = ngx_current_msec;
1971     cache->files = 0;
1972 
1973     next = (ngx_msec_t) ngx_http_file_cache_expire(cache) * 1000;
1974 
1975     if (next == 0) {
1976         next = cache->manager_sleep;
1977         goto done;
1978     }
1979 
1980     for ( ;; ) {
1981         ngx_shmtx_lock(&cache->shpool->mutex);
1982 
1983         size = cache->sh->size;
1984         count = cache->sh->count;
1985         watermark = cache->sh->watermark;
1986 
1987         ngx_shmtx_unlock(&cache->shpool->mutex);
1988 
1989         ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
1990                        "http file cache size: %O c:%ui w:%i",
1991                        size, count, (ngx_int_t) watermark);
1992 
1993         if (size < cache->max_size && count < watermark) {
1994 
1995             if (!cache->min_free) {
1996                 break;
1997             }
1998 
1999             free = ngx_fs_available(cache->path->name.data);
2000 
2001             ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
2002                            "http file cache free: %O", free);
2003 
2004             if (free > cache->min_free) {
2005                 break;
2006             }
2007         }
2008 
2009         wait = ngx_http_file_cache_forced_expire(cache);
2010 
2011         if (wait > 0) {
2012             next = (ngx_msec_t) wait * 1000;
2013             break;
2014         }
2015 
2016         if (ngx_quit || ngx_terminate) {
2017             break;
2018         }
2019 
2020         if (++cache->files >= cache->manager_files) {
2021             next = cache->manager_sleep;
2022             break;
2023         }
2024 
2025         ngx_time_update();
2026 
2027         elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
2028 
2029         if (elapsed >= cache->manager_threshold) {
2030             next = cache->manager_sleep;
2031             break;
2032         }
2033     }
2034 
2035 done:
2036 
2037     elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
2038 
2039     ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
2040                    "http file cache manager: %ui e:%M n:%M",
2041                    cache->files, elapsed, next);
2042 
2043     return next;
2044 }
2045 
2046 
2047 static void
ngx_http_file_cache_loader(void * data)2048 ngx_http_file_cache_loader(void *data)
2049 {
2050     ngx_http_file_cache_t  *cache = data;
2051 
2052     ngx_tree_ctx_t  tree;
2053 
2054     if (!cache->sh->cold || cache->sh->loading) {
2055         return;
2056     }
2057 
2058     if (!ngx_atomic_cmp_set(&cache->sh->loading, 0, ngx_pid)) {
2059         return;
2060     }
2061 
2062     ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
2063                    "http file cache loader");
2064 
2065     tree.init_handler = NULL;
2066     tree.file_handler = ngx_http_file_cache_manage_file;
2067     tree.pre_tree_handler = ngx_http_file_cache_manage_directory;
2068     tree.post_tree_handler = ngx_http_file_cache_noop;
2069     tree.spec_handler = ngx_http_file_cache_delete_file;
2070     tree.data = cache;
2071     tree.alloc = 0;
2072     tree.log = ngx_cycle->log;
2073 
2074     cache->last = ngx_current_msec;
2075     cache->files = 0;
2076 
2077     if (ngx_walk_tree(&tree, &cache->path->name) == NGX_ABORT) {
2078         cache->sh->loading = 0;
2079         return;
2080     }
2081 
2082     cache->sh->cold = 0;
2083     cache->sh->loading = 0;
2084 
2085     ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
2086                   "http file cache: %V %.3fM, bsize: %uz",
2087                   &cache->path->name,
2088                   ((double) cache->sh->size * cache->bsize) / (1024 * 1024),
2089                   cache->bsize);
2090 }
2091 
2092 
2093 static ngx_int_t
ngx_http_file_cache_noop(ngx_tree_ctx_t * ctx,ngx_str_t * path)2094 ngx_http_file_cache_noop(ngx_tree_ctx_t *ctx, ngx_str_t *path)
2095 {
2096     return NGX_OK;
2097 }
2098 
2099 
2100 static ngx_int_t
ngx_http_file_cache_manage_file(ngx_tree_ctx_t * ctx,ngx_str_t * path)2101 ngx_http_file_cache_manage_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
2102 {
2103     ngx_msec_t              elapsed;
2104     ngx_http_file_cache_t  *cache;
2105 
2106     cache = ctx->data;
2107 
2108     if (ngx_http_file_cache_add_file(ctx, path) != NGX_OK) {
2109         (void) ngx_http_file_cache_delete_file(ctx, path);
2110     }
2111 
2112     if (++cache->files >= cache->loader_files) {
2113         ngx_http_file_cache_loader_sleep(cache);
2114 
2115     } else {
2116         ngx_time_update();
2117 
2118         elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));
2119 
2120         ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
2121                        "http file cache loader time elapsed: %M", elapsed);
2122 
2123         if (elapsed >= cache->loader_threshold) {
2124             ngx_http_file_cache_loader_sleep(cache);
2125         }
2126     }
2127 
2128     return (ngx_quit || ngx_terminate) ? NGX_ABORT : NGX_OK;
2129 }
2130 
2131 
2132 static ngx_int_t
ngx_http_file_cache_manage_directory(ngx_tree_ctx_t * ctx,ngx_str_t * path)2133 ngx_http_file_cache_manage_directory(ngx_tree_ctx_t *ctx, ngx_str_t *path)
2134 {
2135     if (path->len >= 5
2136         && ngx_strncmp(path->data + path->len - 5, "/temp", 5) == 0)
2137     {
2138         return NGX_DECLINED;
2139     }
2140 
2141     return NGX_OK;
2142 }
2143 
2144 
2145 static void
ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t * cache)2146 ngx_http_file_cache_loader_sleep(ngx_http_file_cache_t *cache)
2147 {
2148     ngx_msleep(cache->loader_sleep);
2149 
2150     ngx_time_update();
2151 
2152     cache->last = ngx_current_msec;
2153     cache->files = 0;
2154 }
2155 
2156 
2157 static ngx_int_t
ngx_http_file_cache_add_file(ngx_tree_ctx_t * ctx,ngx_str_t * name)2158 ngx_http_file_cache_add_file(ngx_tree_ctx_t *ctx, ngx_str_t *name)
2159 {
2160     u_char                 *p;
2161     ngx_int_t               n;
2162     ngx_uint_t              i;
2163     ngx_http_cache_t        c;
2164     ngx_http_file_cache_t  *cache;
2165 
2166     if (name->len < 2 * NGX_HTTP_CACHE_KEY_LEN) {
2167         return NGX_ERROR;
2168     }
2169 
2170     /*
2171      * Temporary files in cache have a suffix consisting of a dot
2172      * followed by 10 digits.
2173      */
2174 
2175     if (name->len >= 2 * NGX_HTTP_CACHE_KEY_LEN + 1 + 10
2176         && name->data[name->len - 10 - 1] == '.')
2177     {
2178         return NGX_OK;
2179     }
2180 
2181     if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
2182         ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
2183                       "cache file \"%s\" is too small", name->data);
2184         return NGX_ERROR;
2185     }
2186 
2187     ngx_memzero(&c, sizeof(ngx_http_cache_t));
2188     cache = ctx->data;
2189 
2190     c.length = ctx->size;
2191     c.fs_size = (ctx->fs_size + cache->bsize - 1) / cache->bsize;
2192 
2193     p = &name->data[name->len - 2 * NGX_HTTP_CACHE_KEY_LEN];
2194 
2195     for (i = 0; i < NGX_HTTP_CACHE_KEY_LEN; i++) {
2196         n = ngx_hextoi(p, 2);
2197 
2198         if (n == NGX_ERROR) {
2199             return NGX_ERROR;
2200         }
2201 
2202         p += 2;
2203 
2204         c.key[i] = (u_char) n;
2205     }
2206 
2207     return ngx_http_file_cache_add(cache, &c);
2208 }
2209 
2210 
2211 static ngx_int_t
ngx_http_file_cache_add(ngx_http_file_cache_t * cache,ngx_http_cache_t * c)2212 ngx_http_file_cache_add(ngx_http_file_cache_t *cache, ngx_http_cache_t *c)
2213 {
2214     ngx_http_file_cache_node_t  *fcn;
2215 
2216     ngx_shmtx_lock(&cache->shpool->mutex);
2217 
2218     fcn = ngx_http_file_cache_lookup(cache, c->key);
2219 
2220     if (fcn == NULL) {
2221 
2222         fcn = ngx_slab_calloc_locked(cache->shpool,
2223                                      sizeof(ngx_http_file_cache_node_t));
2224         if (fcn == NULL) {
2225             ngx_http_file_cache_set_watermark(cache);
2226 
2227             if (cache->fail_time != ngx_time()) {
2228                 cache->fail_time = ngx_time();
2229                 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, 0,
2230                            "could not allocate node%s", cache->shpool->log_ctx);
2231             }
2232 
2233             ngx_shmtx_unlock(&cache->shpool->mutex);
2234             return NGX_ERROR;
2235         }
2236 
2237         cache->sh->count++;
2238 
2239         ngx_memcpy((u_char *) &fcn->node.key, c->key, sizeof(ngx_rbtree_key_t));
2240 
2241         ngx_memcpy(fcn->key, &c->key[sizeof(ngx_rbtree_key_t)],
2242                    NGX_HTTP_CACHE_KEY_LEN - sizeof(ngx_rbtree_key_t));
2243 
2244         ngx_rbtree_insert(&cache->sh->rbtree, &fcn->node);
2245 
2246         fcn->uses = 1;
2247         fcn->exists = 1;
2248         fcn->fs_size = c->fs_size;
2249 
2250         cache->sh->size += c->fs_size;
2251 
2252     } else {
2253         ngx_queue_remove(&fcn->queue);
2254     }
2255 
2256     fcn->expire = ngx_time() + cache->inactive;
2257 
2258     ngx_queue_insert_head(&cache->sh->queue, &fcn->queue);
2259 
2260     ngx_shmtx_unlock(&cache->shpool->mutex);
2261 
2262     return NGX_OK;
2263 }
2264 
2265 
2266 static ngx_int_t
ngx_http_file_cache_delete_file(ngx_tree_ctx_t * ctx,ngx_str_t * path)2267 ngx_http_file_cache_delete_file(ngx_tree_ctx_t *ctx, ngx_str_t *path)
2268 {
2269     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ctx->log, 0,
2270                    "http file cache delete: \"%s\"", path->data);
2271 
2272     if (ngx_delete_file(path->data) == NGX_FILE_ERROR) {
2273         ngx_log_error(NGX_LOG_CRIT, ctx->log, ngx_errno,
2274                       ngx_delete_file_n " \"%s\" failed", path->data);
2275     }
2276 
2277     return NGX_OK;
2278 }
2279 
2280 
2281 static void
ngx_http_file_cache_set_watermark(ngx_http_file_cache_t * cache)2282 ngx_http_file_cache_set_watermark(ngx_http_file_cache_t *cache)
2283 {
2284     cache->sh->watermark = cache->sh->count - cache->sh->count / 8;
2285 
2286     ngx_log_debug1(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
2287                    "http file cache watermark: %ui", cache->sh->watermark);
2288 }
2289 
2290 
2291 time_t
ngx_http_file_cache_valid(ngx_array_t * cache_valid,ngx_uint_t status)2292 ngx_http_file_cache_valid(ngx_array_t *cache_valid, ngx_uint_t status)
2293 {
2294     ngx_uint_t               i;
2295     ngx_http_cache_valid_t  *valid;
2296 
2297     if (cache_valid == NULL) {
2298         return 0;
2299     }
2300 
2301     valid = cache_valid->elts;
2302     for (i = 0; i < cache_valid->nelts; i++) {
2303 
2304         if (valid[i].status == 0) {
2305             return valid[i].valid;
2306         }
2307 
2308         if (valid[i].status == status) {
2309             return valid[i].valid;
2310         }
2311     }
2312 
2313     return 0;
2314 }
2315 
2316 
2317 char *
ngx_http_file_cache_set_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)2318 ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
2319 {
2320     char  *confp = conf;
2321 
2322     off_t                   max_size, min_free;
2323     u_char                 *last, *p;
2324     time_t                  inactive;
2325     ssize_t                 size;
2326     ngx_str_t               s, name, *value;
2327     ngx_int_t               loader_files, manager_files;
2328     ngx_msec_t              loader_sleep, manager_sleep, loader_threshold,
2329                             manager_threshold;
2330     ngx_uint_t              i, n, use_temp_path;
2331     ngx_array_t            *caches;
2332     ngx_http_file_cache_t  *cache, **ce;
2333 
2334     cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
2335     if (cache == NULL) {
2336         return NGX_CONF_ERROR;
2337     }
2338 
2339     cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
2340     if (cache->path == NULL) {
2341         return NGX_CONF_ERROR;
2342     }
2343 
2344     use_temp_path = 1;
2345 
2346     inactive = 600;
2347 
2348     loader_files = 100;
2349     loader_sleep = 50;
2350     loader_threshold = 200;
2351 
2352     manager_files = 100;
2353     manager_sleep = 50;
2354     manager_threshold = 200;
2355 
2356     name.len = 0;
2357     size = 0;
2358     max_size = NGX_MAX_OFF_T_VALUE;
2359     min_free = 0;
2360 
2361     value = cf->args->elts;
2362 
2363     cache->path->name = value[1];
2364 
2365     if (cache->path->name.data[cache->path->name.len - 1] == '/') {
2366         cache->path->name.len--;
2367     }
2368 
2369     if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
2370         return NGX_CONF_ERROR;
2371     }
2372 
2373     for (i = 2; i < cf->args->nelts; i++) {
2374 
2375         if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {
2376 
2377             p = value[i].data + 7;
2378             last = value[i].data + value[i].len;
2379 
2380             for (n = 0; n < NGX_MAX_PATH_LEVEL && p < last; n++) {
2381 
2382                 if (*p > '0' && *p < '3') {
2383 
2384                     cache->path->level[n] = *p++ - '0';
2385                     cache->path->len += cache->path->level[n] + 1;
2386 
2387                     if (p == last) {
2388                         break;
2389                     }
2390 
2391                     if (*p++ == ':' && n < NGX_MAX_PATH_LEVEL - 1 && p < last) {
2392                         continue;
2393                     }
2394 
2395                     goto invalid_levels;
2396                 }
2397 
2398                 goto invalid_levels;
2399             }
2400 
2401             if (cache->path->len < 10 + NGX_MAX_PATH_LEVEL) {
2402                 continue;
2403             }
2404 
2405         invalid_levels:
2406 
2407             ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2408                                "invalid \"levels\" \"%V\"", &value[i]);
2409             return NGX_CONF_ERROR;
2410         }
2411 
2412         if (ngx_strncmp(value[i].data, "use_temp_path=", 14) == 0) {
2413 
2414             if (ngx_strcmp(&value[i].data[14], "on") == 0) {
2415                 use_temp_path = 1;
2416 
2417             } else if (ngx_strcmp(&value[i].data[14], "off") == 0) {
2418                 use_temp_path = 0;
2419 
2420             } else {
2421                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2422                                    "invalid use_temp_path value \"%V\", "
2423                                    "it must be \"on\" or \"off\"",
2424                                    &value[i]);
2425                 return NGX_CONF_ERROR;
2426             }
2427 
2428             continue;
2429         }
2430 
2431         if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {
2432 
2433             name.data = value[i].data + 10;
2434 
2435             p = (u_char *) ngx_strchr(name.data, ':');
2436 
2437             if (p == NULL) {
2438                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2439                                    "invalid keys zone size \"%V\"", &value[i]);
2440                 return NGX_CONF_ERROR;
2441             }
2442 
2443             name.len = p - name.data;
2444 
2445             s.data = p + 1;
2446             s.len = value[i].data + value[i].len - s.data;
2447 
2448             size = ngx_parse_size(&s);
2449 
2450             if (size == NGX_ERROR) {
2451                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2452                                    "invalid keys zone size \"%V\"", &value[i]);
2453                 return NGX_CONF_ERROR;
2454             }
2455 
2456             if (size < (ssize_t) (2 * ngx_pagesize)) {
2457                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2458                                    "keys zone \"%V\" is too small", &value[i]);
2459                 return NGX_CONF_ERROR;
2460             }
2461 
2462             continue;
2463         }
2464 
2465         if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {
2466 
2467             s.len = value[i].len - 9;
2468             s.data = value[i].data + 9;
2469 
2470             inactive = ngx_parse_time(&s, 1);
2471             if (inactive == (time_t) NGX_ERROR) {
2472                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2473                                    "invalid inactive value \"%V\"", &value[i]);
2474                 return NGX_CONF_ERROR;
2475             }
2476 
2477             continue;
2478         }
2479 
2480         if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {
2481 
2482             s.len = value[i].len - 9;
2483             s.data = value[i].data + 9;
2484 
2485             max_size = ngx_parse_offset(&s);
2486             if (max_size < 0) {
2487                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2488                                    "invalid max_size value \"%V\"", &value[i]);
2489                 return NGX_CONF_ERROR;
2490             }
2491 
2492             continue;
2493         }
2494 
2495         if (ngx_strncmp(value[i].data, "min_free=", 9) == 0) {
2496 
2497 #if (NGX_WIN32 || NGX_HAVE_STATFS || NGX_HAVE_STATVFS)
2498 
2499             s.len = value[i].len - 9;
2500             s.data = value[i].data + 9;
2501 
2502             min_free = ngx_parse_offset(&s);
2503             if (min_free < 0) {
2504                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2505                                    "invalid min_free value \"%V\"", &value[i]);
2506                 return NGX_CONF_ERROR;
2507             }
2508 
2509 #else
2510             ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
2511                                "min_free is not supported "
2512                                "on this platform, ignored");
2513 #endif
2514 
2515             continue;
2516         }
2517 
2518         if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {
2519 
2520             loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
2521             if (loader_files == NGX_ERROR) {
2522                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2523                            "invalid loader_files value \"%V\"", &value[i]);
2524                 return NGX_CONF_ERROR;
2525             }
2526 
2527             continue;
2528         }
2529 
2530         if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {
2531 
2532             s.len = value[i].len - 13;
2533             s.data = value[i].data + 13;
2534 
2535             loader_sleep = ngx_parse_time(&s, 0);
2536             if (loader_sleep == (ngx_msec_t) NGX_ERROR) {
2537                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2538                            "invalid loader_sleep value \"%V\"", &value[i]);
2539                 return NGX_CONF_ERROR;
2540             }
2541 
2542             continue;
2543         }
2544 
2545         if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {
2546 
2547             s.len = value[i].len - 17;
2548             s.data = value[i].data + 17;
2549 
2550             loader_threshold = ngx_parse_time(&s, 0);
2551             if (loader_threshold == (ngx_msec_t) NGX_ERROR) {
2552                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2553                            "invalid loader_threshold value \"%V\"", &value[i]);
2554                 return NGX_CONF_ERROR;
2555             }
2556 
2557             continue;
2558         }
2559 
2560         if (ngx_strncmp(value[i].data, "manager_files=", 14) == 0) {
2561 
2562             manager_files = ngx_atoi(value[i].data + 14, value[i].len - 14);
2563             if (manager_files == NGX_ERROR) {
2564                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2565                            "invalid manager_files value \"%V\"", &value[i]);
2566                 return NGX_CONF_ERROR;
2567             }
2568 
2569             continue;
2570         }
2571 
2572         if (ngx_strncmp(value[i].data, "manager_sleep=", 14) == 0) {
2573 
2574             s.len = value[i].len - 14;
2575             s.data = value[i].data + 14;
2576 
2577             manager_sleep = ngx_parse_time(&s, 0);
2578             if (manager_sleep == (ngx_msec_t) NGX_ERROR) {
2579                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2580                            "invalid manager_sleep value \"%V\"", &value[i]);
2581                 return NGX_CONF_ERROR;
2582             }
2583 
2584             continue;
2585         }
2586 
2587         if (ngx_strncmp(value[i].data, "manager_threshold=", 18) == 0) {
2588 
2589             s.len = value[i].len - 18;
2590             s.data = value[i].data + 18;
2591 
2592             manager_threshold = ngx_parse_time(&s, 0);
2593             if (manager_threshold == (ngx_msec_t) NGX_ERROR) {
2594                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2595                            "invalid manager_threshold value \"%V\"", &value[i]);
2596                 return NGX_CONF_ERROR;
2597             }
2598 
2599             continue;
2600         }
2601 
2602         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2603                            "invalid parameter \"%V\"", &value[i]);
2604         return NGX_CONF_ERROR;
2605     }
2606 
2607     if (name.len == 0 || size == 0) {
2608         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2609                            "\"%V\" must have \"keys_zone\" parameter",
2610                            &cmd->name);
2611         return NGX_CONF_ERROR;
2612     }
2613 
2614     cache->path->manager = ngx_http_file_cache_manager;
2615     cache->path->loader = ngx_http_file_cache_loader;
2616     cache->path->data = cache;
2617     cache->path->conf_file = cf->conf_file->file.name.data;
2618     cache->path->line = cf->conf_file->line;
2619     cache->loader_files = loader_files;
2620     cache->loader_sleep = loader_sleep;
2621     cache->loader_threshold = loader_threshold;
2622     cache->manager_files = manager_files;
2623     cache->manager_sleep = manager_sleep;
2624     cache->manager_threshold = manager_threshold;
2625 
2626     if (ngx_add_path(cf, &cache->path) != NGX_OK) {
2627         return NGX_CONF_ERROR;
2628     }
2629 
2630     cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
2631     if (cache->shm_zone == NULL) {
2632         return NGX_CONF_ERROR;
2633     }
2634 
2635     if (cache->shm_zone->data) {
2636         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2637                            "duplicate zone \"%V\"", &name);
2638         return NGX_CONF_ERROR;
2639     }
2640 
2641 
2642     cache->shm_zone->init = ngx_http_file_cache_init;
2643     cache->shm_zone->data = cache;
2644 
2645     cache->use_temp_path = use_temp_path;
2646 
2647     cache->inactive = inactive;
2648     cache->max_size = max_size;
2649     cache->min_free = min_free;
2650 
2651     caches = (ngx_array_t *) (confp + cmd->offset);
2652 
2653     ce = ngx_array_push(caches);
2654     if (ce == NULL) {
2655         return NGX_CONF_ERROR;
2656     }
2657 
2658     *ce = cache;
2659 
2660     return NGX_CONF_OK;
2661 }
2662 
2663 
2664 char *
ngx_http_file_cache_valid_set_slot(ngx_conf_t * cf,ngx_command_t * cmd,void * conf)2665 ngx_http_file_cache_valid_set_slot(ngx_conf_t *cf, ngx_command_t *cmd,
2666     void *conf)
2667 {
2668     char  *p = conf;
2669 
2670     time_t                    valid;
2671     ngx_str_t                *value;
2672     ngx_int_t                 status;
2673     ngx_uint_t                i, n;
2674     ngx_array_t             **a;
2675     ngx_http_cache_valid_t   *v;
2676     static ngx_uint_t         statuses[] = { 200, 301, 302 };
2677 
2678     a = (ngx_array_t **) (p + cmd->offset);
2679 
2680     if (*a == NGX_CONF_UNSET_PTR) {
2681         *a = ngx_array_create(cf->pool, 1, sizeof(ngx_http_cache_valid_t));
2682         if (*a == NULL) {
2683             return NGX_CONF_ERROR;
2684         }
2685     }
2686 
2687     value = cf->args->elts;
2688     n = cf->args->nelts - 1;
2689 
2690     valid = ngx_parse_time(&value[n], 1);
2691     if (valid == (time_t) NGX_ERROR) {
2692         ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2693                            "invalid time value \"%V\"", &value[n]);
2694         return NGX_CONF_ERROR;
2695     }
2696 
2697     if (n == 1) {
2698 
2699         for (i = 0; i < 3; i++) {
2700             v = ngx_array_push(*a);
2701             if (v == NULL) {
2702                 return NGX_CONF_ERROR;
2703             }
2704 
2705             v->status = statuses[i];
2706             v->valid = valid;
2707         }
2708 
2709         return NGX_CONF_OK;
2710     }
2711 
2712     for (i = 1; i < n; i++) {
2713 
2714         if (ngx_strcmp(value[i].data, "any") == 0) {
2715 
2716             status = 0;
2717 
2718         } else {
2719 
2720             status = ngx_atoi(value[i].data, value[i].len);
2721             if (status < 100 || status > 599) {
2722                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
2723                                    "invalid status \"%V\"", &value[i]);
2724                 return NGX_CONF_ERROR;
2725             }
2726         }
2727 
2728         v = ngx_array_push(*a);
2729         if (v == NULL) {
2730             return NGX_CONF_ERROR;
2731         }
2732 
2733         v->status = status;
2734         v->valid = valid;
2735     }
2736 
2737     return NGX_CONF_OK;
2738 }
2739