conscache.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* Copyright (c) 2017, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "or.h"
  4. #include "config.h"
  5. #include "conscache.h"
  6. #include "storagedir.h"
  7. #define CCE_MAGIC 0x17162253
  8. #ifdef _WIN32
  9. /* On Windows, unlink won't work on a file if the file is actively mmap()ed.
  10. * That forces us to be less aggressive about unlinking files, and causes other
  11. * changes throughout our logic.
  12. */
  13. #define MUST_UNMAP_TO_UNLINK
  14. #endif /* defined(_WIN32) */
  15. /**
  16. * A consensus_cache_entry_t is a reference-counted handle to an
  17. * item in a consensus_cache_t. It can be mmapped into RAM, or not,
  18. * depending whether it's currently in use.
  19. */
  20. struct consensus_cache_entry_t {
  21. uint32_t magic; /**< Must be set to CCE_MAGIC */
  22. HANDLE_ENTRY(consensus_cache_entry, consensus_cache_entry_t);
  23. int32_t refcnt; /**< Reference count. */
  24. unsigned can_remove : 1; /**< If true, we want to delete this file. */
  25. /** If true, we intend to unmap this file as soon as we're done with it. */
  26. unsigned release_aggressively : 1;
  27. /** Filename for this object within the storage_dir_t */
  28. char *fname;
  29. /** Labels associated with this object. Immutable once the object
  30. * is created. */
  31. config_line_t *labels;
  32. /** Pointer to the cache that includes this entry (if any). */
  33. consensus_cache_t *in_cache;
  34. /** Since what time has this object been mapped into RAM, but with the cache
  35. * being the only having a reference to it? */
  36. time_t unused_since;
  37. /** mmaped contents of the underlying file. May be NULL */
  38. tor_mmap_t *map;
  39. /** Length of the body within <b>map</b>. */
  40. size_t bodylen;
  41. /** Pointer to the body within <b>map</b>. */
  42. const uint8_t *body;
  43. };
  44. /**
  45. * A consensus_cache_t holds a directory full of labeled items.
  46. */
  47. struct consensus_cache_t {
  48. /** Underling storage_dir_t to handle persistence */
  49. storage_dir_t *dir;
  50. /** List of all the entries in the directory. */
  51. smartlist_t *entries;
  52. /** The maximum number of entries that we'd like to allow in this cache.
  53. * This is the same as the storagedir limit when MUST_UNMAP_TO_UNLINK is
  54. * not defined. */
  55. unsigned max_entries;
  56. };
  57. static void consensus_cache_clear(consensus_cache_t *cache);
  58. static void consensus_cache_rescan(consensus_cache_t *);
  59. static void consensus_cache_entry_map(consensus_cache_t *,
  60. consensus_cache_entry_t *);
  61. static void consensus_cache_entry_unmap(consensus_cache_entry_t *ent);
  62. /**
  63. * Helper: Open a consensus cache in subdirectory <b>subdir</b> of the
  64. * data directory, to hold up to <b>max_entries</b> of data.
  65. */
  66. consensus_cache_t *
  67. consensus_cache_open(const char *subdir, int max_entries)
  68. {
  69. int storagedir_max_entries;
  70. consensus_cache_t *cache = tor_malloc_zero(sizeof(consensus_cache_t));
  71. char *directory = get_cachedir_fname(subdir);
  72. cache->max_entries = max_entries;
  73. #ifdef MUST_UNMAP_TO_UNLINK
  74. /* If we can't unlink the files that we're still using, then we need to
  75. * tell the storagedir backend to allow far more files than this consensus
  76. * cache actually wants, so that it can hold files which, from this cache's
  77. * perspective, have become useless.
  78. */
  79. #define VERY_LARGE_STORAGEDIR_LIMIT (1000*1000)
  80. storagedir_max_entries = VERY_LARGE_STORAGEDIR_LIMIT;
  81. #else /* !(defined(MUST_UNMAP_TO_UNLINK)) */
  82. /* Otherwise, we can just tell the storagedir to use the same limits
  83. * as this cache. */
  84. storagedir_max_entries = max_entries;
  85. #endif /* defined(MUST_UNMAP_TO_UNLINK) */
  86. cache->dir = storage_dir_new(directory, storagedir_max_entries);
  87. tor_free(directory);
  88. if (!cache->dir) {
  89. tor_free(cache);
  90. return NULL;
  91. }
  92. consensus_cache_rescan(cache);
  93. return cache;
  94. }
  95. /** Return true if it's okay to put more entries in this cache than
  96. * its official file limit.
  97. *
  98. * (We need this method on Windows, where we can't unlink files that are still
  99. * in use, and therefore might need to temporarily exceed the file limit until
  100. * the no-longer-wanted files are deletable.)
  101. */
  102. int
  103. consensus_cache_may_overallocate(consensus_cache_t *cache)
  104. {
  105. (void) cache;
  106. #ifdef MUST_UNMAP_TO_UNLINK
  107. return 1;
  108. #else
  109. return 0;
  110. #endif
  111. }
  112. /**
  113. * Tell the sandbox (if any) configured by <b>cfg</b> to allow the
  114. * operations that <b>cache</b> will need.
  115. */
  116. int
  117. consensus_cache_register_with_sandbox(consensus_cache_t *cache,
  118. struct sandbox_cfg_elem **cfg)
  119. {
  120. #ifdef MUST_UNMAP_TO_UNLINK
  121. /* Our Linux sandbox doesn't support huge file lists like the one that would
  122. * be generated by using VERY_LARGE_STORAGEDIR_LIMIT above in
  123. * consensus_cache_open(). Since the Linux sandbox is the only one we have
  124. * right now, we just assert that we never reach this point when we've had
  125. * to use VERY_LARGE_STORAGEDIR_LIMIT.
  126. *
  127. * If at some point in the future we have a different sandbox mechanism that
  128. * can handle huge file lists, we can remove this assertion or make it
  129. * conditional.
  130. */
  131. tor_assert_nonfatal_unreached();
  132. #endif /* defined(MUST_UNMAP_TO_UNLINK) */
  133. return storage_dir_register_with_sandbox(cache->dir, cfg);
  134. }
  135. /**
  136. * Helper: clear all entries from <b>cache</b> (but do not delete
  137. * any that aren't marked for removal
  138. */
  139. static void
  140. consensus_cache_clear(consensus_cache_t *cache)
  141. {
  142. consensus_cache_delete_pending(cache, 0);
  143. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  144. ent->in_cache = NULL;
  145. consensus_cache_entry_decref(ent);
  146. } SMARTLIST_FOREACH_END(ent);
  147. smartlist_free(cache->entries);
  148. cache->entries = NULL;
  149. }
  150. /**
  151. * Drop all storage held by <b>cache</b>.
  152. */
  153. void
  154. consensus_cache_free_(consensus_cache_t *cache)
  155. {
  156. if (! cache)
  157. return;
  158. if (cache->entries) {
  159. consensus_cache_clear(cache);
  160. }
  161. storage_dir_free(cache->dir);
  162. tor_free(cache);
  163. }
  164. /**
  165. * Write <b>datalen</b> bytes of data at <b>data</b> into the <b>cache</b>,
  166. * labeling that data with <b>labels</b>. On failure, return NULL. On
  167. * success, return a newly created consensus_cache_entry_t.
  168. *
  169. * The returned value will be owned by the cache, and you will have a
  170. * reference to it. Call consensus_cache_entry_decref() when you are
  171. * done with it.
  172. *
  173. * The provided <b>labels</b> MUST have distinct keys: if they don't,
  174. * this API does not specify which values (if any) for the duplicate keys
  175. * will be considered.
  176. */
  177. consensus_cache_entry_t *
  178. consensus_cache_add(consensus_cache_t *cache,
  179. const config_line_t *labels,
  180. const uint8_t *data,
  181. size_t datalen)
  182. {
  183. char *fname = NULL;
  184. int r = storage_dir_save_labeled_to_file(cache->dir,
  185. labels, data, datalen, &fname);
  186. if (r < 0 || fname == NULL) {
  187. return NULL;
  188. }
  189. consensus_cache_entry_t *ent =
  190. tor_malloc_zero(sizeof(consensus_cache_entry_t));
  191. ent->magic = CCE_MAGIC;
  192. ent->fname = fname;
  193. ent->labels = config_lines_dup(labels);
  194. ent->in_cache = cache;
  195. ent->unused_since = TIME_MAX;
  196. smartlist_add(cache->entries, ent);
  197. /* Start the reference count at 2: the caller owns one copy, and the
  198. * cache owns another.
  199. */
  200. ent->refcnt = 2;
  201. return ent;
  202. }
  203. /**
  204. * Given a <b>cache</b>, return some entry for which <b>key</b>=<b>value</b>.
  205. * Return NULL if no such entry exists.
  206. *
  207. * Does not adjust reference counts.
  208. */
  209. consensus_cache_entry_t *
  210. consensus_cache_find_first(consensus_cache_t *cache,
  211. const char *key,
  212. const char *value)
  213. {
  214. smartlist_t *tmp = smartlist_new();
  215. consensus_cache_find_all(tmp, cache, key, value);
  216. consensus_cache_entry_t *ent = NULL;
  217. if (smartlist_len(tmp))
  218. ent = smartlist_get(tmp, 0);
  219. smartlist_free(tmp);
  220. return ent;
  221. }
  222. /**
  223. * Given a <b>cache</b>, add every entry to <b>out<b> for which
  224. * <b>key</b>=<b>value</b>. If <b>key</b> is NULL, add every entry.
  225. *
  226. * Do not add any entry that has been marked for removal.
  227. *
  228. * Does not adjust reference counts.
  229. */
  230. void
  231. consensus_cache_find_all(smartlist_t *out,
  232. consensus_cache_t *cache,
  233. const char *key,
  234. const char *value)
  235. {
  236. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  237. if (ent->can_remove == 1) {
  238. /* We want to delete this; pretend it isn't there. */
  239. continue;
  240. }
  241. if (! key) {
  242. smartlist_add(out, ent);
  243. continue;
  244. }
  245. const char *found_val = consensus_cache_entry_get_value(ent, key);
  246. if (found_val && !strcmp(value, found_val)) {
  247. smartlist_add(out, ent);
  248. }
  249. } SMARTLIST_FOREACH_END(ent);
  250. }
  251. /**
  252. * Given a list of consensus_cache_entry_t, remove all those entries
  253. * that do not have <b>key</b>=<b>value</b> in their labels.
  254. *
  255. * Does not adjust reference counts.
  256. */
  257. void
  258. consensus_cache_filter_list(smartlist_t *lst,
  259. const char *key,
  260. const char *value)
  261. {
  262. if (BUG(lst == NULL))
  263. return; // LCOV_EXCL_LINE
  264. if (key == NULL)
  265. return;
  266. SMARTLIST_FOREACH_BEGIN(lst, consensus_cache_entry_t *, ent) {
  267. const char *found_val = consensus_cache_entry_get_value(ent, key);
  268. if (! found_val || strcmp(value, found_val)) {
  269. SMARTLIST_DEL_CURRENT(lst, ent);
  270. }
  271. } SMARTLIST_FOREACH_END(ent);
  272. }
  273. /**
  274. * If <b>ent</b> has a label with the given <b>key</b>, return its
  275. * value. Otherwise return NULL.
  276. *
  277. * The return value is only guaranteed to be valid for as long as you
  278. * hold a reference to <b>ent</b>.
  279. */
  280. const char *
  281. consensus_cache_entry_get_value(const consensus_cache_entry_t *ent,
  282. const char *key)
  283. {
  284. const config_line_t *match = config_line_find(ent->labels, key);
  285. if (match)
  286. return match->value;
  287. else
  288. return NULL;
  289. }
  290. /**
  291. * Return a pointer to the labels in <b>ent</b>.
  292. *
  293. * This pointer is only guaranteed to be valid for as long as you
  294. * hold a reference to <b>ent</b>.
  295. */
  296. const config_line_t *
  297. consensus_cache_entry_get_labels(const consensus_cache_entry_t *ent)
  298. {
  299. return ent->labels;
  300. }
  301. /**
  302. * Increase the reference count of <b>ent</b>.
  303. */
  304. void
  305. consensus_cache_entry_incref(consensus_cache_entry_t *ent)
  306. {
  307. if (BUG(ent->magic != CCE_MAGIC))
  308. return; // LCOV_EXCL_LINE
  309. ++ent->refcnt;
  310. ent->unused_since = TIME_MAX;
  311. }
  312. /**
  313. * Release a reference held to <b>ent</b>.
  314. *
  315. * If it was the last reference, ent will be freed. Therefore, you must not
  316. * use <b>ent</b> after calling this function.
  317. */
  318. void
  319. consensus_cache_entry_decref(consensus_cache_entry_t *ent)
  320. {
  321. if (! ent)
  322. return;
  323. if (BUG(ent->refcnt <= 0))
  324. return; // LCOV_EXCL_LINE
  325. if (BUG(ent->magic != CCE_MAGIC))
  326. return; // LCOV_EXCL_LINE
  327. --ent->refcnt;
  328. if (ent->refcnt == 1 && ent->in_cache) {
  329. /* Only the cache has a reference: we don't need to keep the file
  330. * mapped */
  331. if (ent->map) {
  332. if (ent->release_aggressively) {
  333. consensus_cache_entry_unmap(ent);
  334. } else {
  335. ent->unused_since = approx_time();
  336. }
  337. }
  338. return;
  339. }
  340. if (ent->refcnt > 0)
  341. return;
  342. /* Refcount is zero; we can free it. */
  343. if (ent->map) {
  344. consensus_cache_entry_unmap(ent);
  345. }
  346. tor_free(ent->fname);
  347. config_free_lines(ent->labels);
  348. consensus_cache_entry_handles_clear(ent);
  349. memwipe(ent, 0, sizeof(consensus_cache_entry_t));
  350. tor_free(ent);
  351. }
  352. /**
  353. * Mark <b>ent</b> for deletion from the cache. Deletion will not occur
  354. * until the cache is the only place that holds a reference to <b>ent</b>.
  355. */
  356. void
  357. consensus_cache_entry_mark_for_removal(consensus_cache_entry_t *ent)
  358. {
  359. ent->can_remove = 1;
  360. }
  361. /**
  362. * Mark <b>ent</b> as the kind of entry that we don't need to keep mmap'd for
  363. * any longer than we're actually using it.
  364. */
  365. void
  366. consensus_cache_entry_mark_for_aggressive_release(consensus_cache_entry_t *ent)
  367. {
  368. ent->release_aggressively = 1;
  369. }
  370. /**
  371. * Try to read the body of <b>ent</b> into memory if it isn't already
  372. * loaded. On success, set *<b>body_out</b> to the body, *<b>sz_out</b>
  373. * to its size, and return 0. On failure return -1.
  374. *
  375. * The resulting body pointer will only be valid for as long as you
  376. * hold a reference to <b>ent</b>.
  377. */
  378. int
  379. consensus_cache_entry_get_body(const consensus_cache_entry_t *ent,
  380. const uint8_t **body_out,
  381. size_t *sz_out)
  382. {
  383. if (BUG(ent->magic != CCE_MAGIC))
  384. return -1; // LCOV_EXCL_LINE
  385. if (! ent->map) {
  386. if (! ent->in_cache)
  387. return -1;
  388. consensus_cache_entry_map((consensus_cache_t *)ent->in_cache,
  389. (consensus_cache_entry_t *)ent);
  390. if (! ent->map) {
  391. return -1;
  392. }
  393. }
  394. *body_out = ent->body;
  395. *sz_out = ent->bodylen;
  396. return 0;
  397. }
  398. /**
  399. * Unmap every mmap'd element of <b>cache</b> that has been unused
  400. * since <b>cutoff</b>.
  401. */
  402. void
  403. consensus_cache_unmap_lazy(consensus_cache_t *cache, time_t cutoff)
  404. {
  405. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  406. tor_assert_nonfatal(ent->in_cache == cache);
  407. if (ent->refcnt > 1 || BUG(ent->in_cache == NULL)) {
  408. /* Somebody is using this entry right now */
  409. continue;
  410. }
  411. if (ent->unused_since > cutoff) {
  412. /* Has been unused only for a little while */
  413. continue;
  414. }
  415. if (ent->map == NULL) {
  416. /* Not actually mapped. */
  417. continue;
  418. }
  419. consensus_cache_entry_unmap(ent);
  420. } SMARTLIST_FOREACH_END(ent);
  421. }
  422. /**
  423. * Return the number of currently unused filenames available in this cache.
  424. */
  425. int
  426. consensus_cache_get_n_filenames_available(consensus_cache_t *cache)
  427. {
  428. tor_assert(cache);
  429. int max = cache->max_entries;
  430. int used = smartlist_len(storage_dir_list(cache->dir));
  431. #ifdef MUST_UNMAP_TO_UNLINK
  432. if (used > max)
  433. return 0;
  434. #else
  435. tor_assert_nonfatal(max >= used);
  436. #endif /* defined(MUST_UNMAP_TO_UNLINK) */
  437. return max - used;
  438. }
  439. /**
  440. * Delete every element of <b>cache</b> has been marked with
  441. * consensus_cache_entry_mark_for_removal. If <b>force</b> is false,
  442. * retain those entries which are in use by something other than the cache.
  443. */
  444. void
  445. consensus_cache_delete_pending(consensus_cache_t *cache, int force)
  446. {
  447. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  448. tor_assert_nonfatal(ent->in_cache == cache);
  449. int force_ent = force;
  450. #ifdef MUST_UNMAP_TO_UNLINK
  451. /* We cannot delete anything with an active mmap on win32, so no
  452. * force-deletion. */
  453. if (ent->map) {
  454. force_ent = 0;
  455. }
  456. #endif /* defined(MUST_UNMAP_TO_UNLINK) */
  457. if (! force_ent) {
  458. if (ent->refcnt > 1 || BUG(ent->in_cache == NULL)) {
  459. /* Somebody is using this entry right now */
  460. continue;
  461. }
  462. }
  463. if (ent->can_remove == 0) {
  464. /* Don't want to delete this. */
  465. continue;
  466. }
  467. if (BUG(ent->refcnt <= 0)) {
  468. continue; // LCOV_EXCL_LINE
  469. }
  470. SMARTLIST_DEL_CURRENT(cache->entries, ent);
  471. ent->in_cache = NULL;
  472. char *fname = tor_strdup(ent->fname); /* save a copy */
  473. consensus_cache_entry_decref(ent);
  474. storage_dir_remove_file(cache->dir, fname);
  475. tor_free(fname);
  476. } SMARTLIST_FOREACH_END(ent);
  477. }
  478. /**
  479. * Internal helper: rescan <b>cache</b> and rebuild its list of entries.
  480. */
  481. static void
  482. consensus_cache_rescan(consensus_cache_t *cache)
  483. {
  484. if (cache->entries) {
  485. consensus_cache_clear(cache);
  486. }
  487. cache->entries = smartlist_new();
  488. const smartlist_t *fnames = storage_dir_list(cache->dir);
  489. SMARTLIST_FOREACH_BEGIN(fnames, const char *, fname) {
  490. tor_mmap_t *map = NULL;
  491. config_line_t *labels = NULL;
  492. const uint8_t *body;
  493. size_t bodylen;
  494. map = storage_dir_map_labeled(cache->dir, fname,
  495. &labels, &body, &bodylen);
  496. if (! map) {
  497. /* The ERANGE error might come from tor_mmap_file() -- it means the file
  498. * was empty. EINVAL might come from ..map_labeled() -- it means the
  499. * file was misformatted. In both cases, we should just delete it.
  500. */
  501. if (errno == ERANGE || errno == EINVAL) {
  502. log_warn(LD_FS, "Found %s file %s in consensus cache; removing it.",
  503. errno == ERANGE ? "empty" : "misformatted",
  504. escaped(fname));
  505. storage_dir_remove_file(cache->dir, fname);
  506. } else {
  507. /* Can't load this; continue */
  508. log_warn(LD_FS, "Unable to map file %s from consensus cache: %s",
  509. escaped(fname), strerror(errno));
  510. }
  511. continue;
  512. }
  513. consensus_cache_entry_t *ent =
  514. tor_malloc_zero(sizeof(consensus_cache_entry_t));
  515. ent->magic = CCE_MAGIC;
  516. ent->fname = tor_strdup(fname);
  517. ent->labels = labels;
  518. ent->refcnt = 1;
  519. ent->in_cache = cache;
  520. ent->unused_since = TIME_MAX;
  521. smartlist_add(cache->entries, ent);
  522. tor_munmap_file(map); /* don't actually need to keep this around */
  523. } SMARTLIST_FOREACH_END(fname);
  524. }
  525. /**
  526. * Make sure that <b>ent</b> is mapped into RAM.
  527. */
  528. static void
  529. consensus_cache_entry_map(consensus_cache_t *cache,
  530. consensus_cache_entry_t *ent)
  531. {
  532. if (ent->map)
  533. return;
  534. ent->map = storage_dir_map_labeled(cache->dir, ent->fname,
  535. NULL, &ent->body, &ent->bodylen);
  536. ent->unused_since = TIME_MAX;
  537. }
  538. /**
  539. * Unmap <b>ent</b> from RAM.
  540. *
  541. * Do not call this if something other than the cache is holding a reference
  542. * to <b>ent</b>
  543. */
  544. static void
  545. consensus_cache_entry_unmap(consensus_cache_entry_t *ent)
  546. {
  547. ent->unused_since = TIME_MAX;
  548. if (!ent->map)
  549. return;
  550. tor_munmap_file(ent->map);
  551. ent->map = NULL;
  552. ent->body = NULL;
  553. ent->bodylen = 0;
  554. ent->unused_since = TIME_MAX;
  555. }
  556. HANDLE_IMPL(consensus_cache_entry, consensus_cache_entry_t, )
  557. #ifdef TOR_UNIT_TESTS
  558. /**
  559. * Testing only: Return true iff <b>ent</b> is mapped into memory.
  560. *
  561. * (In normal operation, this information is not exposed.)
  562. */
  563. int
  564. consensus_cache_entry_is_mapped(consensus_cache_entry_t *ent)
  565. {
  566. if (ent->map) {
  567. tor_assert(ent->body);
  568. return 1;
  569. } else {
  570. tor_assert(!ent->body);
  571. return 0;
  572. }
  573. }
  574. #endif /* defined(TOR_UNIT_TESTS) */