conscache.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. * Do not add any entry that has been marked for removal.
  160. *
  161. * Does not adjust reference counts.
  162. */
  163. void
  164. consensus_cache_find_all(smartlist_t *out,
  165. consensus_cache_t *cache,
  166. const char *key,
  167. const char *value)
  168. {
  169. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  170. if (ent->can_remove == 1) {
  171. /* We want to delete this; pretend it isn't there. */
  172. continue;
  173. }
  174. if (! key) {
  175. smartlist_add(out, ent);
  176. continue;
  177. }
  178. const char *found_val = consensus_cache_entry_get_value(ent, key);
  179. if (found_val && !strcmp(value, found_val)) {
  180. smartlist_add(out, ent);
  181. }
  182. } SMARTLIST_FOREACH_END(ent);
  183. }
  184. /**
  185. * Given a list of consensus_cache_entry_t, remove all those entries
  186. * that do not have <b>key</b>=<b>value</b> in their labels.
  187. *
  188. * Does not adjust reference counts.
  189. */
  190. void
  191. consensus_cache_filter_list(smartlist_t *lst,
  192. const char *key,
  193. const char *value)
  194. {
  195. if (BUG(lst == NULL))
  196. return; // LCOV_EXCL_LINE
  197. if (key == NULL)
  198. return;
  199. SMARTLIST_FOREACH_BEGIN(lst, consensus_cache_entry_t *, ent) {
  200. const char *found_val = consensus_cache_entry_get_value(ent, key);
  201. if (! found_val || strcmp(value, found_val)) {
  202. SMARTLIST_DEL_CURRENT(lst, ent);
  203. }
  204. } SMARTLIST_FOREACH_END(ent);
  205. }
  206. /**
  207. * If <b>ent</b> has a label with the given <b>key</b>, return its
  208. * value. Otherwise return NULL.
  209. *
  210. * The return value is only guaranteed to be valid for as long as you
  211. * hold a reference to <b>ent</b>.
  212. */
  213. const char *
  214. consensus_cache_entry_get_value(const consensus_cache_entry_t *ent,
  215. const char *key)
  216. {
  217. const config_line_t *match = config_line_find(ent->labels, key);
  218. if (match)
  219. return match->value;
  220. else
  221. return NULL;
  222. }
  223. /**
  224. * Return a pointer to the labels in <b>ent</b>.
  225. *
  226. * This pointer is only guaranteed to be valid for as long as you
  227. * hold a reference to <b>ent</b>.
  228. */
  229. const config_line_t *
  230. consensus_cache_entry_get_labels(const consensus_cache_entry_t *ent)
  231. {
  232. return ent->labels;
  233. }
  234. /**
  235. * Increase the reference count of <b>ent</b>.
  236. */
  237. void
  238. consensus_cache_entry_incref(consensus_cache_entry_t *ent)
  239. {
  240. if (BUG(ent->magic != CCE_MAGIC))
  241. return; // LCOV_EXCL_LINE
  242. ++ent->refcnt;
  243. ent->unused_since = TIME_MAX;
  244. }
  245. /**
  246. * Release a reference held to <b>ent</b>.
  247. *
  248. * If it was the last reference, ent will be freed. Therefore, you must not
  249. * use <b>ent</b> after calling this function.
  250. */
  251. void
  252. consensus_cache_entry_decref(consensus_cache_entry_t *ent)
  253. {
  254. if (! ent)
  255. return;
  256. if (BUG(ent->refcnt <= 0))
  257. return; // LCOV_EXCL_LINE
  258. if (BUG(ent->magic != CCE_MAGIC))
  259. return; // LCOV_EXCL_LINE
  260. --ent->refcnt;
  261. if (ent->refcnt == 1 && ent->in_cache) {
  262. /* Only the cache has a reference: we don't need to keep the file
  263. * mapped */
  264. if (ent->map) {
  265. if (ent->release_aggressively) {
  266. consensus_cache_entry_unmap(ent);
  267. } else {
  268. ent->unused_since = approx_time();
  269. }
  270. }
  271. return;
  272. }
  273. if (ent->refcnt > 0)
  274. return;
  275. /* Refcount is zero; we can free it. */
  276. if (ent->map) {
  277. consensus_cache_entry_unmap(ent);
  278. }
  279. tor_free(ent->fname);
  280. config_free_lines(ent->labels);
  281. memwipe(ent, 0, sizeof(consensus_cache_entry_t));
  282. tor_free(ent);
  283. }
  284. /**
  285. * Mark <b>ent</b> for deletion from the cache. Deletion will not occur
  286. * until the cache is the only place that holds a reference to <b>ent</b>.
  287. */
  288. void
  289. consensus_cache_entry_mark_for_removal(consensus_cache_entry_t *ent)
  290. {
  291. ent->can_remove = 1;
  292. }
  293. /**
  294. * Mark <b>ent</b> as the kind of entry that we don't need to keep mmap'd for
  295. * any longer than we're actually using it.
  296. */
  297. void
  298. consensus_cache_entry_mark_for_aggressive_release(consensus_cache_entry_t *ent)
  299. {
  300. ent->release_aggressively = 1;
  301. }
  302. /**
  303. * Try to read the body of <b>ent</b> into memory if it isn't already
  304. * loaded. On success, set *<b>body_out</b> to the body, *<b>sz_out</b>
  305. * to its size, and return 0. On failure return -1.
  306. *
  307. * The resulting body pointer will only be valid for as long as you
  308. * hold a reference to <b>ent</b>.
  309. */
  310. int
  311. consensus_cache_entry_get_body(const consensus_cache_entry_t *ent,
  312. const uint8_t **body_out,
  313. size_t *sz_out)
  314. {
  315. if (BUG(ent->magic != CCE_MAGIC))
  316. return -1; // LCOV_EXCL_LINE
  317. if (! ent->map) {
  318. if (! ent->in_cache)
  319. return -1;
  320. consensus_cache_entry_map((consensus_cache_t *)ent->in_cache,
  321. (consensus_cache_entry_t *)ent);
  322. if (! ent->map) {
  323. return -1;
  324. }
  325. }
  326. *body_out = ent->body;
  327. *sz_out = ent->bodylen;
  328. return 0;
  329. }
  330. /**
  331. * Unmap every mmap'd element of <b>cache</b> that has been unused
  332. * since <b>cutoff</b>.
  333. */
  334. void
  335. consensus_cache_unmap_lazy(consensus_cache_t *cache, time_t cutoff)
  336. {
  337. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  338. tor_assert_nonfatal(ent->in_cache == cache);
  339. if (ent->refcnt > 1 || BUG(ent->in_cache == NULL)) {
  340. /* Somebody is using this entry right now */
  341. continue;
  342. }
  343. if (ent->unused_since > cutoff) {
  344. /* Has been unused only for a little while */
  345. continue;
  346. }
  347. if (ent->map == NULL) {
  348. /* Not actually mapped. */
  349. continue;
  350. }
  351. consensus_cache_entry_unmap(ent);
  352. } SMARTLIST_FOREACH_END(ent);
  353. }
  354. /**
  355. * Delete every element of <b>cache</b> has been marked with
  356. * consensus_cache_entry_mark_for_removal, and which is not in use except by
  357. * the cache.
  358. */
  359. void
  360. consensus_cache_delete_pending(consensus_cache_t *cache)
  361. {
  362. SMARTLIST_FOREACH_BEGIN(cache->entries, consensus_cache_entry_t *, ent) {
  363. tor_assert_nonfatal(ent->in_cache == cache);
  364. if (ent->refcnt > 1 || BUG(ent->in_cache == NULL)) {
  365. /* Somebody is using this entry right now */
  366. continue;
  367. }
  368. if (ent->can_remove == 0) {
  369. /* Don't want to delete this. */
  370. continue;
  371. }
  372. if (BUG(ent->refcnt <= 0)) {
  373. continue; // LCOV_EXCL_LINE
  374. }
  375. SMARTLIST_DEL_CURRENT(cache->entries, ent);
  376. ent->in_cache = NULL;
  377. char *fname = tor_strdup(ent->fname); /* save a copy */
  378. consensus_cache_entry_decref(ent);
  379. storage_dir_remove_file(cache->dir, fname);
  380. tor_free(fname);
  381. } SMARTLIST_FOREACH_END(ent);
  382. }
  383. /**
  384. * Internal helper: rescan <b>cache</b> and rebuild its list of entries.
  385. */
  386. static void
  387. consensus_cache_rescan(consensus_cache_t *cache)
  388. {
  389. if (cache->entries) {
  390. consensus_cache_clear(cache);
  391. }
  392. cache->entries = smartlist_new();
  393. const smartlist_t *fnames = storage_dir_list(cache->dir);
  394. SMARTLIST_FOREACH_BEGIN(fnames, const char *, fname) {
  395. tor_mmap_t *map = NULL;
  396. config_line_t *labels = NULL;
  397. const uint8_t *body;
  398. size_t bodylen;
  399. map = storage_dir_map_labeled(cache->dir, fname,
  400. &labels, &body, &bodylen);
  401. if (! map) {
  402. /* Can't load this; continue */
  403. log_warn(LD_FS, "Unable to map file %s from consensus cache: %s",
  404. escaped(fname), strerror(errno));
  405. continue;
  406. }
  407. consensus_cache_entry_t *ent =
  408. tor_malloc_zero(sizeof(consensus_cache_entry_t));
  409. ent->magic = CCE_MAGIC;
  410. ent->fname = tor_strdup(fname);
  411. ent->labels = labels;
  412. ent->refcnt = 1;
  413. ent->in_cache = cache;
  414. ent->unused_since = TIME_MAX;
  415. smartlist_add(cache->entries, ent);
  416. tor_munmap_file(map); /* don't actually need to keep this around */
  417. } SMARTLIST_FOREACH_END(fname);
  418. }
  419. /**
  420. * Make sure that <b>ent</b> is mapped into RAM.
  421. */
  422. static void
  423. consensus_cache_entry_map(consensus_cache_t *cache,
  424. consensus_cache_entry_t *ent)
  425. {
  426. if (ent->map)
  427. return;
  428. ent->map = storage_dir_map_labeled(cache->dir, ent->fname,
  429. NULL, &ent->body, &ent->bodylen);
  430. ent->unused_since = TIME_MAX;
  431. }
  432. /**
  433. * Unmap <b>ent</b> from RAM.
  434. *
  435. * Do not call this if something other than the cache is holding a reference
  436. * to <b>ent</b>
  437. */
  438. static void
  439. consensus_cache_entry_unmap(consensus_cache_entry_t *ent)
  440. {
  441. ent->unused_since = TIME_MAX;
  442. if (!ent->map)
  443. return;
  444. tor_munmap_file(ent->map);
  445. ent->map = NULL;
  446. ent->body = NULL;
  447. ent->bodylen = 0;
  448. ent->unused_since = TIME_MAX;
  449. }
  450. #ifdef TOR_UNIT_TESTS
  451. /**
  452. * Testing only: Return true iff <b>ent</b> is mapped into memory.
  453. *
  454. * (In normal operation, this information is not exposed.)
  455. */
  456. int
  457. consensus_cache_entry_is_mapped(consensus_cache_entry_t *ent)
  458. {
  459. if (ent->map) {
  460. tor_assert(ent->body);
  461. return 1;
  462. } else {
  463. tor_assert(!ent->body);
  464. return 0;
  465. }
  466. }
  467. #endif