conscache.c 14 KB

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