conscache.c 13 KB

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