conscache.c 16 KB

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