cookie.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* netfs cookie management
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * See Documentation/filesystems/caching/netfs-api.rst for more information on
  8. * the netfs API.
  9. */
  10. #define FSCACHE_DEBUG_LEVEL COOKIE
  11. #include <linux/module.h>
  12. #include <linux/slab.h>
  13. #include "internal.h"
  14. struct kmem_cache *fscache_cookie_jar;
  15. static void fscache_cookie_lru_timed_out(struct timer_list *timer);
  16. static void fscache_cookie_lru_worker(struct work_struct *work);
  17. static void fscache_cookie_worker(struct work_struct *work);
  18. static void fscache_unhash_cookie(struct fscache_cookie *cookie);
  19. static void fscache_perform_invalidation(struct fscache_cookie *cookie);
  20. #define fscache_cookie_hash_shift 15
  21. static struct hlist_bl_head fscache_cookie_hash[1 << fscache_cookie_hash_shift];
  22. static LIST_HEAD(fscache_cookies);
  23. static DEFINE_RWLOCK(fscache_cookies_lock);
  24. static LIST_HEAD(fscache_cookie_lru);
  25. static DEFINE_SPINLOCK(fscache_cookie_lru_lock);
  26. DEFINE_TIMER(fscache_cookie_lru_timer, fscache_cookie_lru_timed_out);
  27. static DECLARE_WORK(fscache_cookie_lru_work, fscache_cookie_lru_worker);
  28. static const char fscache_cookie_states[FSCACHE_COOKIE_STATE__NR] = "-LCAIFUWRD";
  29. static unsigned int fscache_lru_cookie_timeout = 10 * HZ;
  30. void fscache_print_cookie(struct fscache_cookie *cookie, char prefix)
  31. {
  32. const u8 *k;
  33. pr_err("%c-cookie c=%08x [fl=%lx na=%u nA=%u s=%c]\n",
  34. prefix,
  35. cookie->debug_id,
  36. cookie->flags,
  37. atomic_read(&cookie->n_active),
  38. atomic_read(&cookie->n_accesses),
  39. fscache_cookie_states[cookie->state]);
  40. pr_err("%c-cookie V=%08x [%s]\n",
  41. prefix,
  42. cookie->volume->debug_id,
  43. cookie->volume->key);
  44. k = (cookie->key_len <= sizeof(cookie->inline_key)) ?
  45. cookie->inline_key : cookie->key;
  46. pr_err("%c-key=[%u] '%*phN'\n", prefix, cookie->key_len, cookie->key_len, k);
  47. }
  48. static void fscache_free_cookie(struct fscache_cookie *cookie)
  49. {
  50. if (WARN_ON_ONCE(!list_empty(&cookie->commit_link))) {
  51. spin_lock(&fscache_cookie_lru_lock);
  52. list_del_init(&cookie->commit_link);
  53. spin_unlock(&fscache_cookie_lru_lock);
  54. fscache_stat_d(&fscache_n_cookies_lru);
  55. fscache_stat(&fscache_n_cookies_lru_removed);
  56. }
  57. if (WARN_ON_ONCE(test_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags))) {
  58. fscache_print_cookie(cookie, 'F');
  59. return;
  60. }
  61. write_lock(&fscache_cookies_lock);
  62. list_del(&cookie->proc_link);
  63. write_unlock(&fscache_cookies_lock);
  64. if (cookie->aux_len > sizeof(cookie->inline_aux))
  65. kfree(cookie->aux);
  66. if (cookie->key_len > sizeof(cookie->inline_key))
  67. kfree(cookie->key);
  68. fscache_stat_d(&fscache_n_cookies);
  69. kmem_cache_free(fscache_cookie_jar, cookie);
  70. }
  71. static void __fscache_queue_cookie(struct fscache_cookie *cookie)
  72. {
  73. if (!queue_work(fscache_wq, &cookie->work))
  74. fscache_put_cookie(cookie, fscache_cookie_put_over_queued);
  75. }
  76. static void fscache_queue_cookie(struct fscache_cookie *cookie,
  77. enum fscache_cookie_trace where)
  78. {
  79. fscache_get_cookie(cookie, where);
  80. __fscache_queue_cookie(cookie);
  81. }
  82. /*
  83. * Initialise the access gate on a cookie by setting a flag to prevent the
  84. * state machine from being queued when the access counter transitions to 0.
  85. * We're only interested in this when we withdraw caching services from the
  86. * cookie.
  87. */
  88. static void fscache_init_access_gate(struct fscache_cookie *cookie)
  89. {
  90. int n_accesses;
  91. n_accesses = atomic_read(&cookie->n_accesses);
  92. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  93. n_accesses, fscache_access_cache_pin);
  94. set_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
  95. }
  96. /**
  97. * fscache_end_cookie_access - Unpin a cache at the end of an access.
  98. * @cookie: A data file cookie
  99. * @why: An indication of the circumstances of the access for tracing
  100. *
  101. * Unpin a cache cookie after we've accessed it and bring a deferred
  102. * relinquishment or withdrawal state into effect.
  103. *
  104. * The @why indicator is provided for tracing purposes.
  105. */
  106. void fscache_end_cookie_access(struct fscache_cookie *cookie,
  107. enum fscache_access_trace why)
  108. {
  109. int n_accesses;
  110. smp_mb__before_atomic();
  111. n_accesses = atomic_dec_return(&cookie->n_accesses);
  112. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  113. n_accesses, why);
  114. if (n_accesses == 0 &&
  115. !test_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags))
  116. fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
  117. }
  118. EXPORT_SYMBOL(fscache_end_cookie_access);
  119. /*
  120. * Pin the cache behind a cookie so that we can access it.
  121. */
  122. static void __fscache_begin_cookie_access(struct fscache_cookie *cookie,
  123. enum fscache_access_trace why)
  124. {
  125. int n_accesses;
  126. n_accesses = atomic_inc_return(&cookie->n_accesses);
  127. smp_mb__after_atomic(); /* (Future) read state after is-caching.
  128. * Reread n_accesses after is-caching
  129. */
  130. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  131. n_accesses, why);
  132. }
  133. /**
  134. * fscache_begin_cookie_access - Pin a cache so data can be accessed
  135. * @cookie: A data file cookie
  136. * @why: An indication of the circumstances of the access for tracing
  137. *
  138. * Attempt to pin the cache to prevent it from going away whilst we're
  139. * accessing data and returns true if successful. This works as follows:
  140. *
  141. * (1) If the cookie is not being cached (ie. FSCACHE_COOKIE_IS_CACHING is not
  142. * set), we return false to indicate access was not permitted.
  143. *
  144. * (2) If the cookie is being cached, we increment its n_accesses count and
  145. * then recheck the IS_CACHING flag, ending the access if it got cleared.
  146. *
  147. * (3) When we end the access, we decrement the cookie's n_accesses and wake
  148. * up the any waiters if it reaches 0.
  149. *
  150. * (4) Whilst the cookie is actively being cached, its n_accesses is kept
  151. * artificially incremented to prevent wakeups from happening.
  152. *
  153. * (5) When the cache is taken offline or if the cookie is culled, the flag is
  154. * cleared to prevent new accesses, the cookie's n_accesses is decremented
  155. * and we wait for it to become 0.
  156. *
  157. * The @why indicator are merely provided for tracing purposes.
  158. */
  159. bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
  160. enum fscache_access_trace why)
  161. {
  162. if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
  163. return false;
  164. __fscache_begin_cookie_access(cookie, why);
  165. if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags) ||
  166. !fscache_cache_is_live(cookie->volume->cache)) {
  167. fscache_end_cookie_access(cookie, fscache_access_unlive);
  168. return false;
  169. }
  170. return true;
  171. }
  172. static inline void wake_up_cookie_state(struct fscache_cookie *cookie)
  173. {
  174. /* Use a barrier to ensure that waiters see the state variable
  175. * change, as spin_unlock doesn't guarantee a barrier.
  176. *
  177. * See comments over wake_up_bit() and waitqueue_active().
  178. */
  179. smp_mb();
  180. wake_up_var(&cookie->state);
  181. }
  182. /*
  183. * Change the state a cookie is at and wake up anyone waiting for that. Impose
  184. * an ordering between the stuff stored in the cookie and the state member.
  185. * Paired with fscache_cookie_state().
  186. */
  187. static void __fscache_set_cookie_state(struct fscache_cookie *cookie,
  188. enum fscache_cookie_state state)
  189. {
  190. smp_store_release(&cookie->state, state);
  191. }
  192. static void fscache_set_cookie_state(struct fscache_cookie *cookie,
  193. enum fscache_cookie_state state)
  194. {
  195. spin_lock(&cookie->lock);
  196. __fscache_set_cookie_state(cookie, state);
  197. spin_unlock(&cookie->lock);
  198. wake_up_cookie_state(cookie);
  199. }
  200. /**
  201. * fscache_cookie_lookup_negative - Note negative lookup
  202. * @cookie: The cookie that was being looked up
  203. *
  204. * Note that some part of the metadata path in the cache doesn't exist and so
  205. * we can release any waiting readers in the certain knowledge that there's
  206. * nothing for them to actually read.
  207. *
  208. * This function uses no locking and must only be called from the state machine.
  209. */
  210. void fscache_cookie_lookup_negative(struct fscache_cookie *cookie)
  211. {
  212. set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  213. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_CREATING);
  214. }
  215. EXPORT_SYMBOL(fscache_cookie_lookup_negative);
  216. /**
  217. * fscache_resume_after_invalidation - Allow I/O to resume after invalidation
  218. * @cookie: The cookie that was invalidated
  219. *
  220. * Tell fscache that invalidation is sufficiently complete that I/O can be
  221. * allowed again.
  222. */
  223. void fscache_resume_after_invalidation(struct fscache_cookie *cookie)
  224. {
  225. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
  226. }
  227. EXPORT_SYMBOL(fscache_resume_after_invalidation);
  228. /**
  229. * fscache_caching_failed - Report that a failure stopped caching on a cookie
  230. * @cookie: The cookie that was affected
  231. *
  232. * Tell fscache that caching on a cookie needs to be stopped due to some sort
  233. * of failure.
  234. *
  235. * This function uses no locking and must only be called from the state machine.
  236. */
  237. void fscache_caching_failed(struct fscache_cookie *cookie)
  238. {
  239. clear_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
  240. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_FAILED);
  241. trace_fscache_cookie(cookie->debug_id, refcount_read(&cookie->ref),
  242. fscache_cookie_failed);
  243. }
  244. EXPORT_SYMBOL(fscache_caching_failed);
  245. /*
  246. * Set the index key in a cookie. The cookie struct has space for a 16-byte
  247. * key plus length and hash, but if that's not big enough, it's instead a
  248. * pointer to a buffer containing 3 bytes of hash, 1 byte of length and then
  249. * the key data.
  250. */
  251. static int fscache_set_key(struct fscache_cookie *cookie,
  252. const void *index_key, size_t index_key_len)
  253. {
  254. void *buf;
  255. size_t buf_size;
  256. buf_size = round_up(index_key_len, sizeof(__le32));
  257. if (index_key_len > sizeof(cookie->inline_key)) {
  258. buf = kzalloc(buf_size, GFP_KERNEL);
  259. if (!buf)
  260. return -ENOMEM;
  261. cookie->key = buf;
  262. } else {
  263. buf = cookie->inline_key;
  264. }
  265. memcpy(buf, index_key, index_key_len);
  266. cookie->key_hash = fscache_hash(cookie->volume->key_hash,
  267. buf, buf_size);
  268. return 0;
  269. }
  270. static bool fscache_cookie_same(const struct fscache_cookie *a,
  271. const struct fscache_cookie *b)
  272. {
  273. const void *ka, *kb;
  274. if (a->key_hash != b->key_hash ||
  275. a->volume != b->volume ||
  276. a->key_len != b->key_len)
  277. return false;
  278. if (a->key_len <= sizeof(a->inline_key)) {
  279. ka = &a->inline_key;
  280. kb = &b->inline_key;
  281. } else {
  282. ka = a->key;
  283. kb = b->key;
  284. }
  285. return memcmp(ka, kb, a->key_len) == 0;
  286. }
  287. static atomic_t fscache_cookie_debug_id = ATOMIC_INIT(1);
  288. /*
  289. * Allocate a cookie.
  290. */
  291. static struct fscache_cookie *fscache_alloc_cookie(
  292. struct fscache_volume *volume,
  293. u8 advice,
  294. const void *index_key, size_t index_key_len,
  295. const void *aux_data, size_t aux_data_len,
  296. loff_t object_size)
  297. {
  298. struct fscache_cookie *cookie;
  299. /* allocate and initialise a cookie */
  300. cookie = kmem_cache_zalloc(fscache_cookie_jar, GFP_KERNEL);
  301. if (!cookie)
  302. return NULL;
  303. fscache_stat(&fscache_n_cookies);
  304. cookie->volume = volume;
  305. cookie->advice = advice;
  306. cookie->key_len = index_key_len;
  307. cookie->aux_len = aux_data_len;
  308. cookie->object_size = object_size;
  309. if (object_size == 0)
  310. __set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  311. if (fscache_set_key(cookie, index_key, index_key_len) < 0)
  312. goto nomem;
  313. if (cookie->aux_len <= sizeof(cookie->inline_aux)) {
  314. memcpy(cookie->inline_aux, aux_data, cookie->aux_len);
  315. } else {
  316. cookie->aux = kmemdup(aux_data, cookie->aux_len, GFP_KERNEL);
  317. if (!cookie->aux)
  318. goto nomem;
  319. }
  320. refcount_set(&cookie->ref, 1);
  321. cookie->debug_id = atomic_inc_return(&fscache_cookie_debug_id);
  322. spin_lock_init(&cookie->lock);
  323. INIT_LIST_HEAD(&cookie->commit_link);
  324. INIT_WORK(&cookie->work, fscache_cookie_worker);
  325. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
  326. write_lock(&fscache_cookies_lock);
  327. list_add_tail(&cookie->proc_link, &fscache_cookies);
  328. write_unlock(&fscache_cookies_lock);
  329. fscache_see_cookie(cookie, fscache_cookie_new_acquire);
  330. return cookie;
  331. nomem:
  332. fscache_free_cookie(cookie);
  333. return NULL;
  334. }
  335. static inline bool fscache_cookie_is_dropped(struct fscache_cookie *cookie)
  336. {
  337. return READ_ONCE(cookie->state) == FSCACHE_COOKIE_STATE_DROPPED;
  338. }
  339. static void fscache_wait_on_collision(struct fscache_cookie *candidate,
  340. struct fscache_cookie *wait_for)
  341. {
  342. enum fscache_cookie_state *statep = &wait_for->state;
  343. wait_var_event_timeout(statep, fscache_cookie_is_dropped(wait_for),
  344. 20 * HZ);
  345. if (!fscache_cookie_is_dropped(wait_for)) {
  346. pr_notice("Potential collision c=%08x old: c=%08x",
  347. candidate->debug_id, wait_for->debug_id);
  348. wait_var_event(statep, fscache_cookie_is_dropped(wait_for));
  349. }
  350. }
  351. /*
  352. * Attempt to insert the new cookie into the hash. If there's a collision, we
  353. * wait for the old cookie to complete if it's being relinquished and an error
  354. * otherwise.
  355. */
  356. static bool fscache_hash_cookie(struct fscache_cookie *candidate)
  357. {
  358. struct fscache_cookie *cursor, *wait_for = NULL;
  359. struct hlist_bl_head *h;
  360. struct hlist_bl_node *p;
  361. unsigned int bucket;
  362. bucket = candidate->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  363. h = &fscache_cookie_hash[bucket];
  364. hlist_bl_lock(h);
  365. hlist_bl_for_each_entry(cursor, p, h, hash_link) {
  366. if (fscache_cookie_same(candidate, cursor)) {
  367. if (!test_bit(FSCACHE_COOKIE_RELINQUISHED, &cursor->flags))
  368. goto collision;
  369. wait_for = fscache_get_cookie(cursor,
  370. fscache_cookie_get_hash_collision);
  371. break;
  372. }
  373. }
  374. fscache_get_volume(candidate->volume, fscache_volume_get_cookie);
  375. atomic_inc(&candidate->volume->n_cookies);
  376. hlist_bl_add_head(&candidate->hash_link, h);
  377. set_bit(FSCACHE_COOKIE_IS_HASHED, &candidate->flags);
  378. hlist_bl_unlock(h);
  379. if (wait_for) {
  380. fscache_wait_on_collision(candidate, wait_for);
  381. fscache_put_cookie(wait_for, fscache_cookie_put_hash_collision);
  382. }
  383. return true;
  384. collision:
  385. trace_fscache_cookie(cursor->debug_id, refcount_read(&cursor->ref),
  386. fscache_cookie_collision);
  387. pr_err("Duplicate cookie detected\n");
  388. fscache_print_cookie(cursor, 'O');
  389. fscache_print_cookie(candidate, 'N');
  390. hlist_bl_unlock(h);
  391. return false;
  392. }
  393. /*
  394. * Request a cookie to represent a data storage object within a volume.
  395. *
  396. * We never let on to the netfs about errors. We may set a negative cookie
  397. * pointer, but that's okay
  398. */
  399. struct fscache_cookie *__fscache_acquire_cookie(
  400. struct fscache_volume *volume,
  401. u8 advice,
  402. const void *index_key, size_t index_key_len,
  403. const void *aux_data, size_t aux_data_len,
  404. loff_t object_size)
  405. {
  406. struct fscache_cookie *cookie;
  407. _enter("V=%x", volume->debug_id);
  408. if (!index_key || !index_key_len || index_key_len > 255 || aux_data_len > 255)
  409. return NULL;
  410. if (!aux_data || !aux_data_len) {
  411. aux_data = NULL;
  412. aux_data_len = 0;
  413. }
  414. fscache_stat(&fscache_n_acquires);
  415. cookie = fscache_alloc_cookie(volume, advice,
  416. index_key, index_key_len,
  417. aux_data, aux_data_len,
  418. object_size);
  419. if (!cookie) {
  420. fscache_stat(&fscache_n_acquires_oom);
  421. return NULL;
  422. }
  423. if (!fscache_hash_cookie(cookie)) {
  424. fscache_see_cookie(cookie, fscache_cookie_discard);
  425. fscache_free_cookie(cookie);
  426. return NULL;
  427. }
  428. trace_fscache_acquire(cookie);
  429. fscache_stat(&fscache_n_acquires_ok);
  430. _leave(" = c=%08x", cookie->debug_id);
  431. return cookie;
  432. }
  433. EXPORT_SYMBOL(__fscache_acquire_cookie);
  434. /*
  435. * Prepare a cache object to be written to.
  436. */
  437. static void fscache_prepare_to_write(struct fscache_cookie *cookie)
  438. {
  439. cookie->volume->cache->ops->prepare_to_write(cookie);
  440. }
  441. /*
  442. * Look up a cookie in the cache.
  443. */
  444. static void fscache_perform_lookup(struct fscache_cookie *cookie)
  445. {
  446. enum fscache_access_trace trace = fscache_access_lookup_cookie_end_failed;
  447. bool need_withdraw = false;
  448. _enter("");
  449. if (!cookie->volume->cache_priv) {
  450. fscache_create_volume(cookie->volume, true);
  451. if (!cookie->volume->cache_priv) {
  452. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
  453. goto out;
  454. }
  455. }
  456. if (!cookie->volume->cache->ops->lookup_cookie(cookie)) {
  457. if (cookie->state != FSCACHE_COOKIE_STATE_FAILED)
  458. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
  459. need_withdraw = true;
  460. _leave(" [fail]");
  461. goto out;
  462. }
  463. fscache_see_cookie(cookie, fscache_cookie_see_active);
  464. spin_lock(&cookie->lock);
  465. if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  466. __fscache_set_cookie_state(cookie,
  467. FSCACHE_COOKIE_STATE_INVALIDATING);
  468. else
  469. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_ACTIVE);
  470. spin_unlock(&cookie->lock);
  471. wake_up_cookie_state(cookie);
  472. trace = fscache_access_lookup_cookie_end;
  473. out:
  474. fscache_end_cookie_access(cookie, trace);
  475. if (need_withdraw)
  476. fscache_withdraw_cookie(cookie);
  477. fscache_end_volume_access(cookie->volume, cookie, trace);
  478. }
  479. /*
  480. * Begin the process of looking up a cookie. We offload the actual process to
  481. * a worker thread.
  482. */
  483. static bool fscache_begin_lookup(struct fscache_cookie *cookie, bool will_modify)
  484. {
  485. if (will_modify) {
  486. set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
  487. set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
  488. }
  489. if (!fscache_begin_volume_access(cookie->volume, cookie,
  490. fscache_access_lookup_cookie))
  491. return false;
  492. __fscache_begin_cookie_access(cookie, fscache_access_lookup_cookie);
  493. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_LOOKING_UP);
  494. set_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags);
  495. set_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags);
  496. return true;
  497. }
  498. /*
  499. * Start using the cookie for I/O. This prevents the backing object from being
  500. * reaped by VM pressure.
  501. */
  502. void __fscache_use_cookie(struct fscache_cookie *cookie, bool will_modify)
  503. {
  504. enum fscache_cookie_state state;
  505. bool queue = false;
  506. int n_active;
  507. _enter("c=%08x", cookie->debug_id);
  508. if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
  509. "Trying to use relinquished cookie\n"))
  510. return;
  511. spin_lock(&cookie->lock);
  512. n_active = atomic_inc_return(&cookie->n_active);
  513. trace_fscache_active(cookie->debug_id, refcount_read(&cookie->ref),
  514. n_active, atomic_read(&cookie->n_accesses),
  515. will_modify ?
  516. fscache_active_use_modify : fscache_active_use);
  517. again:
  518. state = fscache_cookie_state(cookie);
  519. switch (state) {
  520. case FSCACHE_COOKIE_STATE_QUIESCENT:
  521. queue = fscache_begin_lookup(cookie, will_modify);
  522. break;
  523. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  524. case FSCACHE_COOKIE_STATE_CREATING:
  525. if (will_modify)
  526. set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags);
  527. break;
  528. case FSCACHE_COOKIE_STATE_ACTIVE:
  529. case FSCACHE_COOKIE_STATE_INVALIDATING:
  530. if (will_modify &&
  531. !test_and_set_bit(FSCACHE_COOKIE_LOCAL_WRITE, &cookie->flags)) {
  532. set_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
  533. queue = true;
  534. }
  535. /*
  536. * We could race with cookie_lru which may set LRU_DISCARD bit
  537. * but has yet to run the cookie state machine. If this happens
  538. * and another thread tries to use the cookie, clear LRU_DISCARD
  539. * so we don't end up withdrawing the cookie while in use.
  540. */
  541. if (test_and_clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags))
  542. fscache_see_cookie(cookie, fscache_cookie_see_lru_discard_clear);
  543. break;
  544. case FSCACHE_COOKIE_STATE_FAILED:
  545. case FSCACHE_COOKIE_STATE_WITHDRAWING:
  546. break;
  547. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  548. spin_unlock(&cookie->lock);
  549. wait_var_event(&cookie->state,
  550. fscache_cookie_state(cookie) !=
  551. FSCACHE_COOKIE_STATE_LRU_DISCARDING);
  552. spin_lock(&cookie->lock);
  553. goto again;
  554. case FSCACHE_COOKIE_STATE_DROPPED:
  555. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  556. WARN(1, "Can't use cookie in state %u\n", state);
  557. break;
  558. }
  559. spin_unlock(&cookie->lock);
  560. if (queue)
  561. fscache_queue_cookie(cookie, fscache_cookie_get_use_work);
  562. _leave("");
  563. }
  564. EXPORT_SYMBOL(__fscache_use_cookie);
  565. static void fscache_unuse_cookie_locked(struct fscache_cookie *cookie)
  566. {
  567. clear_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags);
  568. if (!test_bit(FSCACHE_COOKIE_IS_CACHING, &cookie->flags))
  569. return;
  570. cookie->unused_at = jiffies;
  571. spin_lock(&fscache_cookie_lru_lock);
  572. if (list_empty(&cookie->commit_link)) {
  573. fscache_get_cookie(cookie, fscache_cookie_get_lru);
  574. fscache_stat(&fscache_n_cookies_lru);
  575. }
  576. list_move_tail(&cookie->commit_link, &fscache_cookie_lru);
  577. spin_unlock(&fscache_cookie_lru_lock);
  578. timer_reduce(&fscache_cookie_lru_timer,
  579. jiffies + fscache_lru_cookie_timeout);
  580. }
  581. /*
  582. * Stop using the cookie for I/O.
  583. */
  584. void __fscache_unuse_cookie(struct fscache_cookie *cookie,
  585. const void *aux_data, const loff_t *object_size)
  586. {
  587. unsigned int debug_id = cookie->debug_id;
  588. unsigned int r = refcount_read(&cookie->ref);
  589. unsigned int a = atomic_read(&cookie->n_accesses);
  590. unsigned int c;
  591. if (aux_data || object_size)
  592. __fscache_update_cookie(cookie, aux_data, object_size);
  593. /* Subtract 1 from counter unless that drops it to 0 (ie. it was 1) */
  594. c = atomic_fetch_add_unless(&cookie->n_active, -1, 1);
  595. if (c != 1) {
  596. trace_fscache_active(debug_id, r, c - 1, a, fscache_active_unuse);
  597. return;
  598. }
  599. spin_lock(&cookie->lock);
  600. r = refcount_read(&cookie->ref);
  601. a = atomic_read(&cookie->n_accesses);
  602. c = atomic_dec_return(&cookie->n_active);
  603. trace_fscache_active(debug_id, r, c, a, fscache_active_unuse);
  604. if (c == 0)
  605. fscache_unuse_cookie_locked(cookie);
  606. spin_unlock(&cookie->lock);
  607. }
  608. EXPORT_SYMBOL(__fscache_unuse_cookie);
  609. /*
  610. * Perform work upon the cookie, such as committing its cache state,
  611. * relinquishing it or withdrawing the backing cache. We're protected from the
  612. * cache going away under us as object withdrawal must come through this
  613. * non-reentrant work item.
  614. */
  615. static void fscache_cookie_state_machine(struct fscache_cookie *cookie)
  616. {
  617. enum fscache_cookie_state state;
  618. bool wake = false;
  619. _enter("c=%x", cookie->debug_id);
  620. again:
  621. spin_lock(&cookie->lock);
  622. again_locked:
  623. state = cookie->state;
  624. switch (state) {
  625. case FSCACHE_COOKIE_STATE_QUIESCENT:
  626. /* The QUIESCENT state is jumped to the LOOKING_UP state by
  627. * fscache_use_cookie().
  628. */
  629. if (atomic_read(&cookie->n_accesses) == 0 &&
  630. test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
  631. __fscache_set_cookie_state(cookie,
  632. FSCACHE_COOKIE_STATE_RELINQUISHING);
  633. wake = true;
  634. goto again_locked;
  635. }
  636. break;
  637. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  638. spin_unlock(&cookie->lock);
  639. fscache_init_access_gate(cookie);
  640. fscache_perform_lookup(cookie);
  641. goto again;
  642. case FSCACHE_COOKIE_STATE_INVALIDATING:
  643. spin_unlock(&cookie->lock);
  644. fscache_perform_invalidation(cookie);
  645. goto again;
  646. case FSCACHE_COOKIE_STATE_ACTIVE:
  647. if (test_and_clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags)) {
  648. spin_unlock(&cookie->lock);
  649. fscache_prepare_to_write(cookie);
  650. spin_lock(&cookie->lock);
  651. }
  652. if (test_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags)) {
  653. __fscache_set_cookie_state(cookie,
  654. FSCACHE_COOKIE_STATE_LRU_DISCARDING);
  655. wake = true;
  656. goto again_locked;
  657. }
  658. fallthrough;
  659. case FSCACHE_COOKIE_STATE_FAILED:
  660. if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  661. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  662. if (atomic_read(&cookie->n_accesses) != 0)
  663. break;
  664. if (test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
  665. __fscache_set_cookie_state(cookie,
  666. FSCACHE_COOKIE_STATE_RELINQUISHING);
  667. wake = true;
  668. goto again_locked;
  669. }
  670. if (test_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags)) {
  671. __fscache_set_cookie_state(cookie,
  672. FSCACHE_COOKIE_STATE_WITHDRAWING);
  673. wake = true;
  674. goto again_locked;
  675. }
  676. break;
  677. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  678. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  679. case FSCACHE_COOKIE_STATE_WITHDRAWING:
  680. if (cookie->cache_priv) {
  681. spin_unlock(&cookie->lock);
  682. cookie->volume->cache->ops->withdraw_cookie(cookie);
  683. spin_lock(&cookie->lock);
  684. }
  685. if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  686. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  687. switch (state) {
  688. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  689. fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
  690. fscache_unhash_cookie(cookie);
  691. __fscache_set_cookie_state(cookie,
  692. FSCACHE_COOKIE_STATE_DROPPED);
  693. wake = true;
  694. goto out;
  695. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  696. fscache_see_cookie(cookie, fscache_cookie_see_lru_discard);
  697. break;
  698. case FSCACHE_COOKIE_STATE_WITHDRAWING:
  699. fscache_see_cookie(cookie, fscache_cookie_see_withdraw);
  700. break;
  701. default:
  702. BUG();
  703. }
  704. clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
  705. clear_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
  706. clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
  707. clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
  708. set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  709. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
  710. wake = true;
  711. goto again_locked;
  712. case FSCACHE_COOKIE_STATE_DROPPED:
  713. break;
  714. default:
  715. WARN_ONCE(1, "Cookie %x in unexpected state %u\n",
  716. cookie->debug_id, state);
  717. break;
  718. }
  719. out:
  720. spin_unlock(&cookie->lock);
  721. if (wake)
  722. wake_up_cookie_state(cookie);
  723. _leave("");
  724. }
  725. static void fscache_cookie_worker(struct work_struct *work)
  726. {
  727. struct fscache_cookie *cookie = container_of(work, struct fscache_cookie, work);
  728. fscache_see_cookie(cookie, fscache_cookie_see_work);
  729. fscache_cookie_state_machine(cookie);
  730. fscache_put_cookie(cookie, fscache_cookie_put_work);
  731. }
  732. /*
  733. * Wait for the object to become inactive. The cookie's work item will be
  734. * scheduled when someone transitions n_accesses to 0 - but if someone's
  735. * already done that, schedule it anyway.
  736. */
  737. static void __fscache_withdraw_cookie(struct fscache_cookie *cookie)
  738. {
  739. int n_accesses;
  740. bool unpinned;
  741. unpinned = test_and_clear_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
  742. /* Need to read the access count after unpinning */
  743. n_accesses = atomic_read(&cookie->n_accesses);
  744. if (unpinned)
  745. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  746. n_accesses, fscache_access_cache_unpin);
  747. if (n_accesses == 0)
  748. fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
  749. }
  750. static void fscache_cookie_lru_do_one(struct fscache_cookie *cookie)
  751. {
  752. fscache_see_cookie(cookie, fscache_cookie_see_lru_do_one);
  753. spin_lock(&cookie->lock);
  754. if (cookie->state != FSCACHE_COOKIE_STATE_ACTIVE ||
  755. time_before(jiffies, cookie->unused_at + fscache_lru_cookie_timeout) ||
  756. atomic_read(&cookie->n_active) > 0) {
  757. spin_unlock(&cookie->lock);
  758. fscache_stat(&fscache_n_cookies_lru_removed);
  759. } else {
  760. set_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
  761. spin_unlock(&cookie->lock);
  762. fscache_stat(&fscache_n_cookies_lru_expired);
  763. _debug("lru c=%x", cookie->debug_id);
  764. __fscache_withdraw_cookie(cookie);
  765. }
  766. fscache_put_cookie(cookie, fscache_cookie_put_lru);
  767. }
  768. static void fscache_cookie_lru_worker(struct work_struct *work)
  769. {
  770. struct fscache_cookie *cookie;
  771. unsigned long unused_at;
  772. spin_lock(&fscache_cookie_lru_lock);
  773. while (!list_empty(&fscache_cookie_lru)) {
  774. cookie = list_first_entry(&fscache_cookie_lru,
  775. struct fscache_cookie, commit_link);
  776. unused_at = cookie->unused_at + fscache_lru_cookie_timeout;
  777. if (time_before(jiffies, unused_at)) {
  778. timer_reduce(&fscache_cookie_lru_timer, unused_at);
  779. break;
  780. }
  781. list_del_init(&cookie->commit_link);
  782. fscache_stat_d(&fscache_n_cookies_lru);
  783. spin_unlock(&fscache_cookie_lru_lock);
  784. fscache_cookie_lru_do_one(cookie);
  785. spin_lock(&fscache_cookie_lru_lock);
  786. }
  787. spin_unlock(&fscache_cookie_lru_lock);
  788. }
  789. static void fscache_cookie_lru_timed_out(struct timer_list *timer)
  790. {
  791. queue_work(fscache_wq, &fscache_cookie_lru_work);
  792. }
  793. static void fscache_cookie_drop_from_lru(struct fscache_cookie *cookie)
  794. {
  795. bool need_put = false;
  796. if (!list_empty(&cookie->commit_link)) {
  797. spin_lock(&fscache_cookie_lru_lock);
  798. if (!list_empty(&cookie->commit_link)) {
  799. list_del_init(&cookie->commit_link);
  800. fscache_stat_d(&fscache_n_cookies_lru);
  801. fscache_stat(&fscache_n_cookies_lru_dropped);
  802. need_put = true;
  803. }
  804. spin_unlock(&fscache_cookie_lru_lock);
  805. if (need_put)
  806. fscache_put_cookie(cookie, fscache_cookie_put_lru);
  807. }
  808. }
  809. /*
  810. * Remove a cookie from the hash table.
  811. */
  812. static void fscache_unhash_cookie(struct fscache_cookie *cookie)
  813. {
  814. struct hlist_bl_head *h;
  815. unsigned int bucket;
  816. bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  817. h = &fscache_cookie_hash[bucket];
  818. hlist_bl_lock(h);
  819. hlist_bl_del(&cookie->hash_link);
  820. clear_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags);
  821. hlist_bl_unlock(h);
  822. fscache_stat(&fscache_n_relinquishes_dropped);
  823. }
  824. static void fscache_drop_withdraw_cookie(struct fscache_cookie *cookie)
  825. {
  826. fscache_cookie_drop_from_lru(cookie);
  827. __fscache_withdraw_cookie(cookie);
  828. }
  829. /**
  830. * fscache_withdraw_cookie - Mark a cookie for withdrawal
  831. * @cookie: The cookie to be withdrawn.
  832. *
  833. * Allow the cache backend to withdraw the backing for a cookie for its own
  834. * reasons, even if that cookie is in active use.
  835. */
  836. void fscache_withdraw_cookie(struct fscache_cookie *cookie)
  837. {
  838. set_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
  839. fscache_drop_withdraw_cookie(cookie);
  840. }
  841. EXPORT_SYMBOL(fscache_withdraw_cookie);
  842. /*
  843. * Allow the netfs to release a cookie back to the cache.
  844. * - the object will be marked as recyclable on disk if retire is true
  845. */
  846. void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
  847. {
  848. fscache_stat(&fscache_n_relinquishes);
  849. if (retire)
  850. fscache_stat(&fscache_n_relinquishes_retire);
  851. _enter("c=%08x{%d},%d",
  852. cookie->debug_id, atomic_read(&cookie->n_active), retire);
  853. if (WARN(test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
  854. "Cookie c=%x already relinquished\n", cookie->debug_id))
  855. return;
  856. if (retire)
  857. set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
  858. trace_fscache_relinquish(cookie, retire);
  859. ASSERTCMP(atomic_read(&cookie->n_active), ==, 0);
  860. ASSERTCMP(atomic_read(&cookie->volume->n_cookies), >, 0);
  861. atomic_dec(&cookie->volume->n_cookies);
  862. if (test_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags)) {
  863. set_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags);
  864. fscache_drop_withdraw_cookie(cookie);
  865. } else {
  866. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_DROPPED);
  867. fscache_unhash_cookie(cookie);
  868. }
  869. fscache_put_cookie(cookie, fscache_cookie_put_relinquish);
  870. }
  871. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  872. /*
  873. * Drop a reference to a cookie.
  874. */
  875. void fscache_put_cookie(struct fscache_cookie *cookie,
  876. enum fscache_cookie_trace where)
  877. {
  878. struct fscache_volume *volume = cookie->volume;
  879. unsigned int cookie_debug_id = cookie->debug_id;
  880. bool zero;
  881. int ref;
  882. zero = __refcount_dec_and_test(&cookie->ref, &ref);
  883. trace_fscache_cookie(cookie_debug_id, ref - 1, where);
  884. if (zero) {
  885. fscache_free_cookie(cookie);
  886. fscache_put_volume(volume, fscache_volume_put_cookie);
  887. }
  888. }
  889. EXPORT_SYMBOL(fscache_put_cookie);
  890. /*
  891. * Get a reference to a cookie.
  892. */
  893. struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
  894. enum fscache_cookie_trace where)
  895. {
  896. int ref;
  897. __refcount_inc(&cookie->ref, &ref);
  898. trace_fscache_cookie(cookie->debug_id, ref + 1, where);
  899. return cookie;
  900. }
  901. EXPORT_SYMBOL(fscache_get_cookie);
  902. /*
  903. * Ask the cache to effect invalidation of a cookie.
  904. */
  905. static void fscache_perform_invalidation(struct fscache_cookie *cookie)
  906. {
  907. if (!cookie->volume->cache->ops->invalidate_cookie(cookie))
  908. fscache_caching_failed(cookie);
  909. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  910. }
  911. /*
  912. * Invalidate an object.
  913. */
  914. void __fscache_invalidate(struct fscache_cookie *cookie,
  915. const void *aux_data, loff_t new_size,
  916. unsigned int flags)
  917. {
  918. bool is_caching;
  919. _enter("c=%x", cookie->debug_id);
  920. fscache_stat(&fscache_n_invalidates);
  921. if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
  922. "Trying to invalidate relinquished cookie\n"))
  923. return;
  924. if ((flags & FSCACHE_INVAL_DIO_WRITE) &&
  925. test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
  926. return;
  927. spin_lock(&cookie->lock);
  928. set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  929. fscache_update_aux(cookie, aux_data, &new_size);
  930. cookie->inval_counter++;
  931. trace_fscache_invalidate(cookie, new_size);
  932. switch (cookie->state) {
  933. case FSCACHE_COOKIE_STATE_INVALIDATING: /* is_still_valid will catch it */
  934. default:
  935. spin_unlock(&cookie->lock);
  936. _leave(" [no %u]", cookie->state);
  937. return;
  938. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  939. if (!test_and_set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  940. __fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie);
  941. fallthrough;
  942. case FSCACHE_COOKIE_STATE_CREATING:
  943. spin_unlock(&cookie->lock);
  944. _leave(" [look %x]", cookie->inval_counter);
  945. return;
  946. case FSCACHE_COOKIE_STATE_ACTIVE:
  947. is_caching = fscache_begin_cookie_access(
  948. cookie, fscache_access_invalidate_cookie);
  949. if (is_caching)
  950. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_INVALIDATING);
  951. spin_unlock(&cookie->lock);
  952. wake_up_cookie_state(cookie);
  953. if (is_caching)
  954. fscache_queue_cookie(cookie, fscache_cookie_get_inval_work);
  955. _leave(" [inv]");
  956. return;
  957. }
  958. }
  959. EXPORT_SYMBOL(__fscache_invalidate);
  960. #ifdef CONFIG_PROC_FS
  961. /*
  962. * Generate a list of extant cookies in /proc/fs/fscache/cookies
  963. */
  964. static int fscache_cookies_seq_show(struct seq_file *m, void *v)
  965. {
  966. struct fscache_cookie *cookie;
  967. unsigned int keylen = 0, auxlen = 0;
  968. u8 *p;
  969. if (v == &fscache_cookies) {
  970. seq_puts(m,
  971. "COOKIE VOLUME REF ACT ACC S FL DEF \n"
  972. "======== ======== === === === = == ================\n"
  973. );
  974. return 0;
  975. }
  976. cookie = list_entry(v, struct fscache_cookie, proc_link);
  977. seq_printf(m,
  978. "%08x %08x %3d %3d %3d %c %02lx",
  979. cookie->debug_id,
  980. cookie->volume->debug_id,
  981. refcount_read(&cookie->ref),
  982. atomic_read(&cookie->n_active),
  983. atomic_read(&cookie->n_accesses),
  984. fscache_cookie_states[cookie->state],
  985. cookie->flags);
  986. keylen = cookie->key_len;
  987. auxlen = cookie->aux_len;
  988. if (keylen > 0 || auxlen > 0) {
  989. seq_puts(m, " ");
  990. p = keylen <= sizeof(cookie->inline_key) ?
  991. cookie->inline_key : cookie->key;
  992. for (; keylen > 0; keylen--)
  993. seq_printf(m, "%02x", *p++);
  994. if (auxlen > 0) {
  995. seq_puts(m, ", ");
  996. p = auxlen <= sizeof(cookie->inline_aux) ?
  997. cookie->inline_aux : cookie->aux;
  998. for (; auxlen > 0; auxlen--)
  999. seq_printf(m, "%02x", *p++);
  1000. }
  1001. }
  1002. seq_puts(m, "\n");
  1003. return 0;
  1004. }
  1005. static void *fscache_cookies_seq_start(struct seq_file *m, loff_t *_pos)
  1006. __acquires(fscache_cookies_lock)
  1007. {
  1008. read_lock(&fscache_cookies_lock);
  1009. return seq_list_start_head(&fscache_cookies, *_pos);
  1010. }
  1011. static void *fscache_cookies_seq_next(struct seq_file *m, void *v, loff_t *_pos)
  1012. {
  1013. return seq_list_next(v, &fscache_cookies, _pos);
  1014. }
  1015. static void fscache_cookies_seq_stop(struct seq_file *m, void *v)
  1016. __releases(rcu)
  1017. {
  1018. read_unlock(&fscache_cookies_lock);
  1019. }
  1020. const struct seq_operations fscache_cookies_seq_ops = {
  1021. .start = fscache_cookies_seq_start,
  1022. .next = fscache_cookies_seq_next,
  1023. .stop = fscache_cookies_seq_stop,
  1024. .show = fscache_cookies_seq_show,
  1025. };
  1026. #endif