lru_cache.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /*
  2. lru_cache.c
  3. This file is part of DRBD by Philipp Reisner and Lars Ellenberg.
  4. Copyright (C) 2003-2008, LINBIT Information Technologies GmbH.
  5. Copyright (C) 2003-2008, Philipp Reisner <philipp.reisner@linbit.com>.
  6. Copyright (C) 2003-2008, Lars Ellenberg <lars.ellenberg@linbit.com>.
  7. drbd is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2, or (at your option)
  10. any later version.
  11. drbd is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with drbd; see the file COPYING. If not, write to
  17. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/bitops.h>
  21. #include <linux/slab.h>
  22. #include <linux/string.h> /* for memset */
  23. #include <linux/seq_file.h> /* for seq_printf */
  24. #include <linux/lru_cache.h>
  25. MODULE_AUTHOR("Philipp Reisner <phil@linbit.com>, "
  26. "Lars Ellenberg <lars@linbit.com>");
  27. MODULE_DESCRIPTION("lru_cache - Track sets of hot objects");
  28. MODULE_LICENSE("GPL");
  29. /* this is developers aid only.
  30. * it catches concurrent access (lack of locking on the users part) */
  31. #define PARANOIA_ENTRY() do { \
  32. BUG_ON(!lc); \
  33. BUG_ON(!lc->nr_elements); \
  34. BUG_ON(test_and_set_bit(__LC_PARANOIA, &lc->flags)); \
  35. } while (0)
  36. #define RETURN(x...) do { \
  37. clear_bit_unlock(__LC_PARANOIA, &lc->flags); \
  38. return x ; } while (0)
  39. /* BUG() if e is not one of the elements tracked by lc */
  40. #define PARANOIA_LC_ELEMENT(lc, e) do { \
  41. struct lru_cache *lc_ = (lc); \
  42. struct lc_element *e_ = (e); \
  43. unsigned i = e_->lc_index; \
  44. BUG_ON(i >= lc_->nr_elements); \
  45. BUG_ON(lc_->lc_element[i] != e_); } while (0)
  46. /* We need to atomically
  47. * - try to grab the lock (set LC_LOCKED)
  48. * - only if there is no pending transaction
  49. * (neither LC_DIRTY nor LC_STARVING is set)
  50. * Because of PARANOIA_ENTRY() above abusing lc->flags as well,
  51. * it is not sufficient to just say
  52. * return 0 == cmpxchg(&lc->flags, 0, LC_LOCKED);
  53. */
  54. int lc_try_lock(struct lru_cache *lc)
  55. {
  56. unsigned long val;
  57. do {
  58. val = cmpxchg(&lc->flags, 0, LC_LOCKED);
  59. } while (unlikely (val == LC_PARANOIA));
  60. /* Spin until no-one is inside a PARANOIA_ENTRY()/RETURN() section. */
  61. return 0 == val;
  62. #if 0
  63. /* Alternative approach, spin in case someone enters or leaves a
  64. * PARANOIA_ENTRY()/RETURN() section. */
  65. unsigned long old, new, val;
  66. do {
  67. old = lc->flags & LC_PARANOIA;
  68. new = old | LC_LOCKED;
  69. val = cmpxchg(&lc->flags, old, new);
  70. } while (unlikely (val == (old ^ LC_PARANOIA)));
  71. return old == val;
  72. #endif
  73. }
  74. /**
  75. * lc_create - prepares to track objects in an active set
  76. * @name: descriptive name only used in lc_seq_printf_stats and lc_seq_dump_details
  77. * @max_pending_changes: maximum changes to accumulate until a transaction is required
  78. * @e_count: number of elements allowed to be active simultaneously
  79. * @e_size: size of the tracked objects
  80. * @e_off: offset to the &struct lc_element member in a tracked object
  81. *
  82. * Returns a pointer to a newly initialized struct lru_cache on success,
  83. * or NULL on (allocation) failure.
  84. */
  85. struct lru_cache *lc_create(const char *name, struct kmem_cache *cache,
  86. unsigned max_pending_changes,
  87. unsigned e_count, size_t e_size, size_t e_off)
  88. {
  89. struct hlist_head *slot = NULL;
  90. struct lc_element **element = NULL;
  91. struct lru_cache *lc;
  92. struct lc_element *e;
  93. unsigned cache_obj_size = kmem_cache_size(cache);
  94. unsigned i;
  95. WARN_ON(cache_obj_size < e_size);
  96. if (cache_obj_size < e_size)
  97. return NULL;
  98. /* e_count too big; would probably fail the allocation below anyways.
  99. * for typical use cases, e_count should be few thousand at most. */
  100. if (e_count > LC_MAX_ACTIVE)
  101. return NULL;
  102. slot = kcalloc(e_count, sizeof(struct hlist_head), GFP_KERNEL);
  103. if (!slot)
  104. goto out_fail;
  105. element = kzalloc(e_count * sizeof(struct lc_element *), GFP_KERNEL);
  106. if (!element)
  107. goto out_fail;
  108. lc = kzalloc(sizeof(*lc), GFP_KERNEL);
  109. if (!lc)
  110. goto out_fail;
  111. INIT_LIST_HEAD(&lc->in_use);
  112. INIT_LIST_HEAD(&lc->lru);
  113. INIT_LIST_HEAD(&lc->free);
  114. INIT_LIST_HEAD(&lc->to_be_changed);
  115. lc->name = name;
  116. lc->element_size = e_size;
  117. lc->element_off = e_off;
  118. lc->nr_elements = e_count;
  119. lc->max_pending_changes = max_pending_changes;
  120. lc->lc_cache = cache;
  121. lc->lc_element = element;
  122. lc->lc_slot = slot;
  123. /* preallocate all objects */
  124. for (i = 0; i < e_count; i++) {
  125. void *p = kmem_cache_alloc(cache, GFP_KERNEL);
  126. if (!p)
  127. break;
  128. memset(p, 0, lc->element_size);
  129. e = p + e_off;
  130. e->lc_index = i;
  131. e->lc_number = LC_FREE;
  132. e->lc_new_number = LC_FREE;
  133. list_add(&e->list, &lc->free);
  134. element[i] = e;
  135. }
  136. if (i == e_count)
  137. return lc;
  138. /* else: could not allocate all elements, give up */
  139. for (i--; i; i--) {
  140. void *p = element[i];
  141. kmem_cache_free(cache, p - e_off);
  142. }
  143. kfree(lc);
  144. out_fail:
  145. kfree(element);
  146. kfree(slot);
  147. return NULL;
  148. }
  149. static void lc_free_by_index(struct lru_cache *lc, unsigned i)
  150. {
  151. void *p = lc->lc_element[i];
  152. WARN_ON(!p);
  153. if (p) {
  154. p -= lc->element_off;
  155. kmem_cache_free(lc->lc_cache, p);
  156. }
  157. }
  158. /**
  159. * lc_destroy - frees memory allocated by lc_create()
  160. * @lc: the lru cache to destroy
  161. */
  162. void lc_destroy(struct lru_cache *lc)
  163. {
  164. unsigned i;
  165. if (!lc)
  166. return;
  167. for (i = 0; i < lc->nr_elements; i++)
  168. lc_free_by_index(lc, i);
  169. kfree(lc->lc_element);
  170. kfree(lc->lc_slot);
  171. kfree(lc);
  172. }
  173. /**
  174. * lc_reset - does a full reset for @lc and the hash table slots.
  175. * @lc: the lru cache to operate on
  176. *
  177. * It is roughly the equivalent of re-allocating a fresh lru_cache object,
  178. * basically a short cut to lc_destroy(lc); lc = lc_create(...);
  179. */
  180. void lc_reset(struct lru_cache *lc)
  181. {
  182. unsigned i;
  183. INIT_LIST_HEAD(&lc->in_use);
  184. INIT_LIST_HEAD(&lc->lru);
  185. INIT_LIST_HEAD(&lc->free);
  186. INIT_LIST_HEAD(&lc->to_be_changed);
  187. lc->used = 0;
  188. lc->hits = 0;
  189. lc->misses = 0;
  190. lc->starving = 0;
  191. lc->locked = 0;
  192. lc->changed = 0;
  193. lc->pending_changes = 0;
  194. lc->flags = 0;
  195. memset(lc->lc_slot, 0, sizeof(struct hlist_head) * lc->nr_elements);
  196. for (i = 0; i < lc->nr_elements; i++) {
  197. struct lc_element *e = lc->lc_element[i];
  198. void *p = e;
  199. p -= lc->element_off;
  200. memset(p, 0, lc->element_size);
  201. /* re-init it */
  202. e->lc_index = i;
  203. e->lc_number = LC_FREE;
  204. e->lc_new_number = LC_FREE;
  205. list_add(&e->list, &lc->free);
  206. }
  207. }
  208. /**
  209. * lc_seq_printf_stats - print stats about @lc into @seq
  210. * @seq: the seq_file to print into
  211. * @lc: the lru cache to print statistics of
  212. */
  213. void lc_seq_printf_stats(struct seq_file *seq, struct lru_cache *lc)
  214. {
  215. /* NOTE:
  216. * total calls to lc_get are
  217. * (starving + hits + misses)
  218. * misses include "locked" count (update from an other thread in
  219. * progress) and "changed", when this in fact lead to an successful
  220. * update of the cache.
  221. */
  222. seq_printf(seq, "\t%s: used:%u/%u hits:%lu misses:%lu starving:%lu locked:%lu changed:%lu\n",
  223. lc->name, lc->used, lc->nr_elements,
  224. lc->hits, lc->misses, lc->starving, lc->locked, lc->changed);
  225. }
  226. static struct hlist_head *lc_hash_slot(struct lru_cache *lc, unsigned int enr)
  227. {
  228. return lc->lc_slot + (enr % lc->nr_elements);
  229. }
  230. static struct lc_element *__lc_find(struct lru_cache *lc, unsigned int enr,
  231. bool include_changing)
  232. {
  233. struct lc_element *e;
  234. BUG_ON(!lc);
  235. BUG_ON(!lc->nr_elements);
  236. hlist_for_each_entry(e, lc_hash_slot(lc, enr), colision) {
  237. /* "about to be changed" elements, pending transaction commit,
  238. * are hashed by their "new number". "Normal" elements have
  239. * lc_number == lc_new_number. */
  240. if (e->lc_new_number != enr)
  241. continue;
  242. if (e->lc_new_number == e->lc_number || include_changing)
  243. return e;
  244. break;
  245. }
  246. return NULL;
  247. }
  248. /**
  249. * lc_find - find element by label, if present in the hash table
  250. * @lc: The lru_cache object
  251. * @enr: element number
  252. *
  253. * Returns the pointer to an element, if the element with the requested
  254. * "label" or element number is present in the hash table,
  255. * or NULL if not found. Does not change the refcnt.
  256. * Ignores elements that are "about to be used", i.e. not yet in the active
  257. * set, but still pending transaction commit.
  258. */
  259. struct lc_element *lc_find(struct lru_cache *lc, unsigned int enr)
  260. {
  261. return __lc_find(lc, enr, 0);
  262. }
  263. /**
  264. * lc_is_used - find element by label
  265. * @lc: The lru_cache object
  266. * @enr: element number
  267. *
  268. * Returns true, if the element with the requested "label" or element number is
  269. * present in the hash table, and is used (refcnt > 0).
  270. * Also finds elements that are not _currently_ used but only "about to be
  271. * used", i.e. on the "to_be_changed" list, pending transaction commit.
  272. */
  273. bool lc_is_used(struct lru_cache *lc, unsigned int enr)
  274. {
  275. struct lc_element *e = __lc_find(lc, enr, 1);
  276. return e && e->refcnt;
  277. }
  278. /**
  279. * lc_del - removes an element from the cache
  280. * @lc: The lru_cache object
  281. * @e: The element to remove
  282. *
  283. * @e must be unused (refcnt == 0). Moves @e from "lru" to "free" list,
  284. * sets @e->enr to %LC_FREE.
  285. */
  286. void lc_del(struct lru_cache *lc, struct lc_element *e)
  287. {
  288. PARANOIA_ENTRY();
  289. PARANOIA_LC_ELEMENT(lc, e);
  290. BUG_ON(e->refcnt);
  291. e->lc_number = e->lc_new_number = LC_FREE;
  292. hlist_del_init(&e->colision);
  293. list_move(&e->list, &lc->free);
  294. RETURN();
  295. }
  296. static struct lc_element *lc_prepare_for_change(struct lru_cache *lc, unsigned new_number)
  297. {
  298. struct list_head *n;
  299. struct lc_element *e;
  300. if (!list_empty(&lc->free))
  301. n = lc->free.next;
  302. else if (!list_empty(&lc->lru))
  303. n = lc->lru.prev;
  304. else
  305. return NULL;
  306. e = list_entry(n, struct lc_element, list);
  307. PARANOIA_LC_ELEMENT(lc, e);
  308. e->lc_new_number = new_number;
  309. if (!hlist_unhashed(&e->colision))
  310. __hlist_del(&e->colision);
  311. hlist_add_head(&e->colision, lc_hash_slot(lc, new_number));
  312. list_move(&e->list, &lc->to_be_changed);
  313. return e;
  314. }
  315. static int lc_unused_element_available(struct lru_cache *lc)
  316. {
  317. if (!list_empty(&lc->free))
  318. return 1; /* something on the free list */
  319. if (!list_empty(&lc->lru))
  320. return 1; /* something to evict */
  321. return 0;
  322. }
  323. /* used as internal flags to __lc_get */
  324. enum {
  325. LC_GET_MAY_CHANGE = 1,
  326. LC_GET_MAY_USE_UNCOMMITTED = 2,
  327. };
  328. static struct lc_element *__lc_get(struct lru_cache *lc, unsigned int enr, unsigned int flags)
  329. {
  330. struct lc_element *e;
  331. PARANOIA_ENTRY();
  332. if (lc->flags & LC_STARVING) {
  333. ++lc->starving;
  334. RETURN(NULL);
  335. }
  336. e = __lc_find(lc, enr, 1);
  337. /* if lc_new_number != lc_number,
  338. * this enr is currently being pulled in already,
  339. * and will be available once the pending transaction
  340. * has been committed. */
  341. if (e) {
  342. if (e->lc_new_number != e->lc_number) {
  343. /* It has been found above, but on the "to_be_changed"
  344. * list, not yet committed. Don't pull it in twice,
  345. * wait for the transaction, then try again...
  346. */
  347. if (!(flags & LC_GET_MAY_USE_UNCOMMITTED))
  348. RETURN(NULL);
  349. /* ... unless the caller is aware of the implications,
  350. * probably preparing a cumulative transaction. */
  351. ++e->refcnt;
  352. ++lc->hits;
  353. RETURN(e);
  354. }
  355. /* else: lc_new_number == lc_number; a real hit. */
  356. ++lc->hits;
  357. if (e->refcnt++ == 0)
  358. lc->used++;
  359. list_move(&e->list, &lc->in_use); /* Not evictable... */
  360. RETURN(e);
  361. }
  362. /* e == NULL */
  363. ++lc->misses;
  364. if (!(flags & LC_GET_MAY_CHANGE))
  365. RETURN(NULL);
  366. /* To avoid races with lc_try_lock(), first, mark us dirty
  367. * (using test_and_set_bit, as it implies memory barriers), ... */
  368. test_and_set_bit(__LC_DIRTY, &lc->flags);
  369. /* ... only then check if it is locked anyways. If lc_unlock clears
  370. * the dirty bit again, that's not a problem, we will come here again.
  371. */
  372. if (test_bit(__LC_LOCKED, &lc->flags)) {
  373. ++lc->locked;
  374. RETURN(NULL);
  375. }
  376. /* In case there is nothing available and we can not kick out
  377. * the LRU element, we have to wait ...
  378. */
  379. if (!lc_unused_element_available(lc)) {
  380. __set_bit(__LC_STARVING, &lc->flags);
  381. RETURN(NULL);
  382. }
  383. /* It was not present in the active set. We are going to recycle an
  384. * unused (or even "free") element, but we won't accumulate more than
  385. * max_pending_changes changes. */
  386. if (lc->pending_changes >= lc->max_pending_changes)
  387. RETURN(NULL);
  388. e = lc_prepare_for_change(lc, enr);
  389. BUG_ON(!e);
  390. clear_bit(__LC_STARVING, &lc->flags);
  391. BUG_ON(++e->refcnt != 1);
  392. lc->used++;
  393. lc->pending_changes++;
  394. RETURN(e);
  395. }
  396. /**
  397. * lc_get - get element by label, maybe change the active set
  398. * @lc: the lru cache to operate on
  399. * @enr: the label to look up
  400. *
  401. * Finds an element in the cache, increases its usage count,
  402. * "touches" and returns it.
  403. *
  404. * In case the requested number is not present, it needs to be added to the
  405. * cache. Therefore it is possible that an other element becomes evicted from
  406. * the cache. In either case, the user is notified so he is able to e.g. keep
  407. * a persistent log of the cache changes, and therefore the objects in use.
  408. *
  409. * Return values:
  410. * NULL
  411. * The cache was marked %LC_STARVING,
  412. * or the requested label was not in the active set
  413. * and a changing transaction is still pending (@lc was marked %LC_DIRTY).
  414. * Or no unused or free element could be recycled (@lc will be marked as
  415. * %LC_STARVING, blocking further lc_get() operations).
  416. *
  417. * pointer to the element with the REQUESTED element number.
  418. * In this case, it can be used right away
  419. *
  420. * pointer to an UNUSED element with some different element number,
  421. * where that different number may also be %LC_FREE.
  422. *
  423. * In this case, the cache is marked %LC_DIRTY,
  424. * so lc_try_lock() will no longer succeed.
  425. * The returned element pointer is moved to the "to_be_changed" list,
  426. * and registered with the new element number on the hash collision chains,
  427. * so it is possible to pick it up from lc_is_used().
  428. * Up to "max_pending_changes" (see lc_create()) can be accumulated.
  429. * The user now should do whatever housekeeping is necessary,
  430. * typically serialize on lc_try_lock_for_transaction(), then call
  431. * lc_committed(lc) and lc_unlock(), to finish the change.
  432. *
  433. * NOTE: The user needs to check the lc_number on EACH use, so he recognizes
  434. * any cache set change.
  435. */
  436. struct lc_element *lc_get(struct lru_cache *lc, unsigned int enr)
  437. {
  438. return __lc_get(lc, enr, LC_GET_MAY_CHANGE);
  439. }
  440. /**
  441. * lc_get_cumulative - like lc_get; also finds to-be-changed elements
  442. * @lc: the lru cache to operate on
  443. * @enr: the label to look up
  444. *
  445. * Unlike lc_get this also returns the element for @enr, if it is belonging to
  446. * a pending transaction, so the return values are like for lc_get(),
  447. * plus:
  448. *
  449. * pointer to an element already on the "to_be_changed" list.
  450. * In this case, the cache was already marked %LC_DIRTY.
  451. *
  452. * Caller needs to make sure that the pending transaction is completed,
  453. * before proceeding to actually use this element.
  454. */
  455. struct lc_element *lc_get_cumulative(struct lru_cache *lc, unsigned int enr)
  456. {
  457. return __lc_get(lc, enr, LC_GET_MAY_CHANGE|LC_GET_MAY_USE_UNCOMMITTED);
  458. }
  459. /**
  460. * lc_try_get - get element by label, if present; do not change the active set
  461. * @lc: the lru cache to operate on
  462. * @enr: the label to look up
  463. *
  464. * Finds an element in the cache, increases its usage count,
  465. * "touches" and returns it.
  466. *
  467. * Return values:
  468. * NULL
  469. * The cache was marked %LC_STARVING,
  470. * or the requested label was not in the active set
  471. *
  472. * pointer to the element with the REQUESTED element number.
  473. * In this case, it can be used right away
  474. */
  475. struct lc_element *lc_try_get(struct lru_cache *lc, unsigned int enr)
  476. {
  477. return __lc_get(lc, enr, 0);
  478. }
  479. /**
  480. * lc_committed - tell @lc that pending changes have been recorded
  481. * @lc: the lru cache to operate on
  482. *
  483. * User is expected to serialize on explicit lc_try_lock_for_transaction()
  484. * before the transaction is started, and later needs to lc_unlock() explicitly
  485. * as well.
  486. */
  487. void lc_committed(struct lru_cache *lc)
  488. {
  489. struct lc_element *e, *tmp;
  490. PARANOIA_ENTRY();
  491. list_for_each_entry_safe(e, tmp, &lc->to_be_changed, list) {
  492. /* count number of changes, not number of transactions */
  493. ++lc->changed;
  494. e->lc_number = e->lc_new_number;
  495. list_move(&e->list, &lc->in_use);
  496. }
  497. lc->pending_changes = 0;
  498. RETURN();
  499. }
  500. /**
  501. * lc_put - give up refcnt of @e
  502. * @lc: the lru cache to operate on
  503. * @e: the element to put
  504. *
  505. * If refcnt reaches zero, the element is moved to the lru list,
  506. * and a %LC_STARVING (if set) is cleared.
  507. * Returns the new (post-decrement) refcnt.
  508. */
  509. unsigned int lc_put(struct lru_cache *lc, struct lc_element *e)
  510. {
  511. PARANOIA_ENTRY();
  512. PARANOIA_LC_ELEMENT(lc, e);
  513. BUG_ON(e->refcnt == 0);
  514. BUG_ON(e->lc_number != e->lc_new_number);
  515. if (--e->refcnt == 0) {
  516. /* move it to the front of LRU. */
  517. list_move(&e->list, &lc->lru);
  518. lc->used--;
  519. clear_bit_unlock(__LC_STARVING, &lc->flags);
  520. }
  521. RETURN(e->refcnt);
  522. }
  523. /**
  524. * lc_element_by_index
  525. * @lc: the lru cache to operate on
  526. * @i: the index of the element to return
  527. */
  528. struct lc_element *lc_element_by_index(struct lru_cache *lc, unsigned i)
  529. {
  530. BUG_ON(i >= lc->nr_elements);
  531. BUG_ON(lc->lc_element[i] == NULL);
  532. BUG_ON(lc->lc_element[i]->lc_index != i);
  533. return lc->lc_element[i];
  534. }
  535. /**
  536. * lc_index_of
  537. * @lc: the lru cache to operate on
  538. * @e: the element to query for its index position in lc->element
  539. */
  540. unsigned int lc_index_of(struct lru_cache *lc, struct lc_element *e)
  541. {
  542. PARANOIA_LC_ELEMENT(lc, e);
  543. return e->lc_index;
  544. }
  545. /**
  546. * lc_set - associate index with label
  547. * @lc: the lru cache to operate on
  548. * @enr: the label to set
  549. * @index: the element index to associate label with.
  550. *
  551. * Used to initialize the active set to some previously recorded state.
  552. */
  553. void lc_set(struct lru_cache *lc, unsigned int enr, int index)
  554. {
  555. struct lc_element *e;
  556. struct list_head *lh;
  557. if (index < 0 || index >= lc->nr_elements)
  558. return;
  559. e = lc_element_by_index(lc, index);
  560. BUG_ON(e->lc_number != e->lc_new_number);
  561. BUG_ON(e->refcnt != 0);
  562. e->lc_number = e->lc_new_number = enr;
  563. hlist_del_init(&e->colision);
  564. if (enr == LC_FREE)
  565. lh = &lc->free;
  566. else {
  567. hlist_add_head(&e->colision, lc_hash_slot(lc, enr));
  568. lh = &lc->lru;
  569. }
  570. list_move(&e->list, lh);
  571. }
  572. /**
  573. * lc_dump - Dump a complete LRU cache to seq in textual form.
  574. * @lc: the lru cache to operate on
  575. * @seq: the &struct seq_file pointer to seq_printf into
  576. * @utext: user supplied additional "heading" or other info
  577. * @detail: function pointer the user may provide to dump further details
  578. * of the object the lc_element is embedded in. May be NULL.
  579. * Note: a leading space ' ' and trailing newline '\n' is implied.
  580. */
  581. void lc_seq_dump_details(struct seq_file *seq, struct lru_cache *lc, char *utext,
  582. void (*detail) (struct seq_file *, struct lc_element *))
  583. {
  584. unsigned int nr_elements = lc->nr_elements;
  585. struct lc_element *e;
  586. int i;
  587. seq_printf(seq, "\tnn: lc_number (new nr) refcnt %s\n ", utext);
  588. for (i = 0; i < nr_elements; i++) {
  589. e = lc_element_by_index(lc, i);
  590. if (e->lc_number != e->lc_new_number)
  591. seq_printf(seq, "\t%5d: %6d %8d %6d ",
  592. i, e->lc_number, e->lc_new_number, e->refcnt);
  593. else
  594. seq_printf(seq, "\t%5d: %6d %-8s %6d ",
  595. i, e->lc_number, "-\"-", e->refcnt);
  596. if (detail)
  597. detail(seq, e);
  598. seq_putc(seq, '\n');
  599. }
  600. }
  601. EXPORT_SYMBOL(lc_create);
  602. EXPORT_SYMBOL(lc_reset);
  603. EXPORT_SYMBOL(lc_destroy);
  604. EXPORT_SYMBOL(lc_set);
  605. EXPORT_SYMBOL(lc_del);
  606. EXPORT_SYMBOL(lc_try_get);
  607. EXPORT_SYMBOL(lc_find);
  608. EXPORT_SYMBOL(lc_get);
  609. EXPORT_SYMBOL(lc_put);
  610. EXPORT_SYMBOL(lc_committed);
  611. EXPORT_SYMBOL(lc_element_by_index);
  612. EXPORT_SYMBOL(lc_index_of);
  613. EXPORT_SYMBOL(lc_seq_printf_stats);
  614. EXPORT_SYMBOL(lc_seq_dump_details);
  615. EXPORT_SYMBOL(lc_try_lock);
  616. EXPORT_SYMBOL(lc_is_used);
  617. EXPORT_SYMBOL(lc_get_cumulative);