conscache.c 18 KB

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