fscache_cookie.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  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 (dhowells@redhat.com)
  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. if (atomic_read(&cookie->n_accesses) != 0)
  654. /* still being accessed: postpone it */
  655. break;
  656. __fscache_set_cookie_state(cookie,
  657. FSCACHE_COOKIE_STATE_LRU_DISCARDING);
  658. wake = true;
  659. goto again_locked;
  660. }
  661. fallthrough;
  662. case FSCACHE_COOKIE_STATE_FAILED:
  663. if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  664. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  665. if (atomic_read(&cookie->n_accesses) != 0)
  666. break;
  667. if (test_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags)) {
  668. __fscache_set_cookie_state(cookie,
  669. FSCACHE_COOKIE_STATE_RELINQUISHING);
  670. wake = true;
  671. goto again_locked;
  672. }
  673. if (test_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags)) {
  674. __fscache_set_cookie_state(cookie,
  675. FSCACHE_COOKIE_STATE_WITHDRAWING);
  676. wake = true;
  677. goto again_locked;
  678. }
  679. break;
  680. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  681. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  682. case FSCACHE_COOKIE_STATE_WITHDRAWING:
  683. if (cookie->cache_priv) {
  684. spin_unlock(&cookie->lock);
  685. cookie->volume->cache->ops->withdraw_cookie(cookie);
  686. spin_lock(&cookie->lock);
  687. }
  688. if (test_and_clear_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  689. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  690. switch (state) {
  691. case FSCACHE_COOKIE_STATE_RELINQUISHING:
  692. fscache_see_cookie(cookie, fscache_cookie_see_relinquish);
  693. fscache_unhash_cookie(cookie);
  694. __fscache_set_cookie_state(cookie,
  695. FSCACHE_COOKIE_STATE_DROPPED);
  696. wake = true;
  697. goto out;
  698. case FSCACHE_COOKIE_STATE_LRU_DISCARDING:
  699. fscache_see_cookie(cookie, fscache_cookie_see_lru_discard);
  700. break;
  701. case FSCACHE_COOKIE_STATE_WITHDRAWING:
  702. fscache_see_cookie(cookie, fscache_cookie_see_withdraw);
  703. break;
  704. default:
  705. BUG();
  706. }
  707. clear_bit(FSCACHE_COOKIE_NEEDS_UPDATE, &cookie->flags);
  708. clear_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
  709. clear_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
  710. clear_bit(FSCACHE_COOKIE_DO_PREP_TO_WRITE, &cookie->flags);
  711. set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  712. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_QUIESCENT);
  713. wake = true;
  714. goto again_locked;
  715. case FSCACHE_COOKIE_STATE_DROPPED:
  716. break;
  717. default:
  718. WARN_ONCE(1, "Cookie %x in unexpected state %u\n",
  719. cookie->debug_id, state);
  720. break;
  721. }
  722. out:
  723. spin_unlock(&cookie->lock);
  724. if (wake)
  725. wake_up_cookie_state(cookie);
  726. _leave("");
  727. }
  728. static void fscache_cookie_worker(struct work_struct *work)
  729. {
  730. struct fscache_cookie *cookie = container_of(work, struct fscache_cookie, work);
  731. fscache_see_cookie(cookie, fscache_cookie_see_work);
  732. fscache_cookie_state_machine(cookie);
  733. fscache_put_cookie(cookie, fscache_cookie_put_work);
  734. }
  735. /*
  736. * Wait for the object to become inactive. The cookie's work item will be
  737. * scheduled when someone transitions n_accesses to 0 - but if someone's
  738. * already done that, schedule it anyway.
  739. */
  740. static void __fscache_withdraw_cookie(struct fscache_cookie *cookie)
  741. {
  742. int n_accesses;
  743. bool unpinned;
  744. unpinned = test_and_clear_bit(FSCACHE_COOKIE_NO_ACCESS_WAKE, &cookie->flags);
  745. /* Need to read the access count after unpinning */
  746. n_accesses = atomic_read(&cookie->n_accesses);
  747. if (unpinned)
  748. trace_fscache_access(cookie->debug_id, refcount_read(&cookie->ref),
  749. n_accesses, fscache_access_cache_unpin);
  750. if (n_accesses == 0)
  751. fscache_queue_cookie(cookie, fscache_cookie_get_end_access);
  752. }
  753. static void fscache_cookie_lru_do_one(struct fscache_cookie *cookie)
  754. {
  755. fscache_see_cookie(cookie, fscache_cookie_see_lru_do_one);
  756. spin_lock(&cookie->lock);
  757. if (cookie->state != FSCACHE_COOKIE_STATE_ACTIVE ||
  758. time_before(jiffies, cookie->unused_at + fscache_lru_cookie_timeout) ||
  759. atomic_read(&cookie->n_active) > 0) {
  760. spin_unlock(&cookie->lock);
  761. fscache_stat(&fscache_n_cookies_lru_removed);
  762. } else {
  763. set_bit(FSCACHE_COOKIE_DO_LRU_DISCARD, &cookie->flags);
  764. spin_unlock(&cookie->lock);
  765. fscache_stat(&fscache_n_cookies_lru_expired);
  766. _debug("lru c=%x", cookie->debug_id);
  767. __fscache_withdraw_cookie(cookie);
  768. }
  769. fscache_put_cookie(cookie, fscache_cookie_put_lru);
  770. }
  771. static void fscache_cookie_lru_worker(struct work_struct *work)
  772. {
  773. struct fscache_cookie *cookie;
  774. unsigned long unused_at;
  775. spin_lock(&fscache_cookie_lru_lock);
  776. while (!list_empty(&fscache_cookie_lru)) {
  777. cookie = list_first_entry(&fscache_cookie_lru,
  778. struct fscache_cookie, commit_link);
  779. unused_at = cookie->unused_at + fscache_lru_cookie_timeout;
  780. if (time_before(jiffies, unused_at)) {
  781. timer_reduce(&fscache_cookie_lru_timer, unused_at);
  782. break;
  783. }
  784. list_del_init(&cookie->commit_link);
  785. fscache_stat_d(&fscache_n_cookies_lru);
  786. spin_unlock(&fscache_cookie_lru_lock);
  787. fscache_cookie_lru_do_one(cookie);
  788. spin_lock(&fscache_cookie_lru_lock);
  789. }
  790. spin_unlock(&fscache_cookie_lru_lock);
  791. }
  792. static void fscache_cookie_lru_timed_out(struct timer_list *timer)
  793. {
  794. queue_work(fscache_wq, &fscache_cookie_lru_work);
  795. }
  796. static void fscache_cookie_drop_from_lru(struct fscache_cookie *cookie)
  797. {
  798. bool need_put = false;
  799. if (!list_empty(&cookie->commit_link)) {
  800. spin_lock(&fscache_cookie_lru_lock);
  801. if (!list_empty(&cookie->commit_link)) {
  802. list_del_init(&cookie->commit_link);
  803. fscache_stat_d(&fscache_n_cookies_lru);
  804. fscache_stat(&fscache_n_cookies_lru_dropped);
  805. need_put = true;
  806. }
  807. spin_unlock(&fscache_cookie_lru_lock);
  808. if (need_put)
  809. fscache_put_cookie(cookie, fscache_cookie_put_lru);
  810. }
  811. }
  812. /*
  813. * Remove a cookie from the hash table.
  814. */
  815. static void fscache_unhash_cookie(struct fscache_cookie *cookie)
  816. {
  817. struct hlist_bl_head *h;
  818. unsigned int bucket;
  819. bucket = cookie->key_hash & (ARRAY_SIZE(fscache_cookie_hash) - 1);
  820. h = &fscache_cookie_hash[bucket];
  821. hlist_bl_lock(h);
  822. hlist_bl_del(&cookie->hash_link);
  823. clear_bit(FSCACHE_COOKIE_IS_HASHED, &cookie->flags);
  824. hlist_bl_unlock(h);
  825. fscache_stat(&fscache_n_relinquishes_dropped);
  826. }
  827. static void fscache_drop_withdraw_cookie(struct fscache_cookie *cookie)
  828. {
  829. fscache_cookie_drop_from_lru(cookie);
  830. __fscache_withdraw_cookie(cookie);
  831. }
  832. /**
  833. * fscache_withdraw_cookie - Mark a cookie for withdrawal
  834. * @cookie: The cookie to be withdrawn.
  835. *
  836. * Allow the cache backend to withdraw the backing for a cookie for its own
  837. * reasons, even if that cookie is in active use.
  838. */
  839. void fscache_withdraw_cookie(struct fscache_cookie *cookie)
  840. {
  841. set_bit(FSCACHE_COOKIE_DO_WITHDRAW, &cookie->flags);
  842. fscache_drop_withdraw_cookie(cookie);
  843. }
  844. EXPORT_SYMBOL(fscache_withdraw_cookie);
  845. /*
  846. * Allow the netfs to release a cookie back to the cache.
  847. * - the object will be marked as recyclable on disk if retire is true
  848. */
  849. void __fscache_relinquish_cookie(struct fscache_cookie *cookie, bool retire)
  850. {
  851. fscache_stat(&fscache_n_relinquishes);
  852. if (retire)
  853. fscache_stat(&fscache_n_relinquishes_retire);
  854. _enter("c=%08x{%d},%d",
  855. cookie->debug_id, atomic_read(&cookie->n_active), retire);
  856. if (WARN(test_and_set_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
  857. "Cookie c=%x already relinquished\n", cookie->debug_id))
  858. return;
  859. if (retire)
  860. set_bit(FSCACHE_COOKIE_RETIRED, &cookie->flags);
  861. trace_fscache_relinquish(cookie, retire);
  862. ASSERTCMP(atomic_read(&cookie->n_active), ==, 0);
  863. ASSERTCMP(atomic_read(&cookie->volume->n_cookies), >, 0);
  864. atomic_dec(&cookie->volume->n_cookies);
  865. if (test_bit(FSCACHE_COOKIE_HAS_BEEN_CACHED, &cookie->flags)) {
  866. set_bit(FSCACHE_COOKIE_DO_RELINQUISH, &cookie->flags);
  867. fscache_drop_withdraw_cookie(cookie);
  868. } else {
  869. fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_DROPPED);
  870. fscache_unhash_cookie(cookie);
  871. }
  872. fscache_put_cookie(cookie, fscache_cookie_put_relinquish);
  873. }
  874. EXPORT_SYMBOL(__fscache_relinquish_cookie);
  875. /*
  876. * Drop a reference to a cookie.
  877. */
  878. void fscache_put_cookie(struct fscache_cookie *cookie,
  879. enum fscache_cookie_trace where)
  880. {
  881. struct fscache_volume *volume = cookie->volume;
  882. unsigned int cookie_debug_id = cookie->debug_id;
  883. bool zero;
  884. int ref;
  885. zero = __refcount_dec_and_test(&cookie->ref, &ref);
  886. trace_fscache_cookie(cookie_debug_id, ref - 1, where);
  887. if (zero) {
  888. fscache_free_cookie(cookie);
  889. fscache_put_volume(volume, fscache_volume_put_cookie);
  890. }
  891. }
  892. EXPORT_SYMBOL(fscache_put_cookie);
  893. /*
  894. * Get a reference to a cookie.
  895. */
  896. struct fscache_cookie *fscache_get_cookie(struct fscache_cookie *cookie,
  897. enum fscache_cookie_trace where)
  898. {
  899. int ref;
  900. __refcount_inc(&cookie->ref, &ref);
  901. trace_fscache_cookie(cookie->debug_id, ref + 1, where);
  902. return cookie;
  903. }
  904. EXPORT_SYMBOL(fscache_get_cookie);
  905. /*
  906. * Ask the cache to effect invalidation of a cookie.
  907. */
  908. static void fscache_perform_invalidation(struct fscache_cookie *cookie)
  909. {
  910. if (!cookie->volume->cache->ops->invalidate_cookie(cookie))
  911. fscache_caching_failed(cookie);
  912. fscache_end_cookie_access(cookie, fscache_access_invalidate_cookie_end);
  913. }
  914. /*
  915. * Invalidate an object.
  916. */
  917. void __fscache_invalidate(struct fscache_cookie *cookie,
  918. const void *aux_data, loff_t new_size,
  919. unsigned int flags)
  920. {
  921. bool is_caching;
  922. _enter("c=%x", cookie->debug_id);
  923. fscache_stat(&fscache_n_invalidates);
  924. if (WARN(test_bit(FSCACHE_COOKIE_RELINQUISHED, &cookie->flags),
  925. "Trying to invalidate relinquished cookie\n"))
  926. return;
  927. if ((flags & FSCACHE_INVAL_DIO_WRITE) &&
  928. test_and_set_bit(FSCACHE_COOKIE_DISABLED, &cookie->flags))
  929. return;
  930. spin_lock(&cookie->lock);
  931. set_bit(FSCACHE_COOKIE_NO_DATA_TO_READ, &cookie->flags);
  932. fscache_update_aux(cookie, aux_data, &new_size);
  933. cookie->inval_counter++;
  934. trace_fscache_invalidate(cookie, new_size);
  935. switch (cookie->state) {
  936. case FSCACHE_COOKIE_STATE_INVALIDATING: /* is_still_valid will catch it */
  937. default:
  938. spin_unlock(&cookie->lock);
  939. _leave(" [no %u]", cookie->state);
  940. return;
  941. case FSCACHE_COOKIE_STATE_LOOKING_UP:
  942. if (!test_and_set_bit(FSCACHE_COOKIE_DO_INVALIDATE, &cookie->flags))
  943. __fscache_begin_cookie_access(cookie, fscache_access_invalidate_cookie);
  944. fallthrough;
  945. case FSCACHE_COOKIE_STATE_CREATING:
  946. spin_unlock(&cookie->lock);
  947. _leave(" [look %x]", cookie->inval_counter);
  948. return;
  949. case FSCACHE_COOKIE_STATE_ACTIVE:
  950. is_caching = fscache_begin_cookie_access(
  951. cookie, fscache_access_invalidate_cookie);
  952. if (is_caching)
  953. __fscache_set_cookie_state(cookie, FSCACHE_COOKIE_STATE_INVALIDATING);
  954. spin_unlock(&cookie->lock);
  955. wake_up_cookie_state(cookie);
  956. if (is_caching)
  957. fscache_queue_cookie(cookie, fscache_cookie_get_inval_work);
  958. _leave(" [inv]");
  959. return;
  960. }
  961. }
  962. EXPORT_SYMBOL(__fscache_invalidate);
  963. #ifdef CONFIG_PROC_FS
  964. /*
  965. * Generate a list of extant cookies in /proc/fs/fscache/cookies
  966. */
  967. static int fscache_cookies_seq_show(struct seq_file *m, void *v)
  968. {
  969. struct fscache_cookie *cookie;
  970. unsigned int keylen = 0, auxlen = 0;
  971. u8 *p;
  972. if (v == &fscache_cookies) {
  973. seq_puts(m,
  974. "COOKIE VOLUME REF ACT ACC S FL DEF \n"
  975. "======== ======== === === === = == ================\n"
  976. );
  977. return 0;
  978. }
  979. cookie = list_entry(v, struct fscache_cookie, proc_link);
  980. seq_printf(m,
  981. "%08x %08x %3d %3d %3d %c %02lx",
  982. cookie->debug_id,
  983. cookie->volume->debug_id,
  984. refcount_read(&cookie->ref),
  985. atomic_read(&cookie->n_active),
  986. atomic_read(&cookie->n_accesses),
  987. fscache_cookie_states[cookie->state],
  988. cookie->flags);
  989. keylen = cookie->key_len;
  990. auxlen = cookie->aux_len;
  991. if (keylen > 0 || auxlen > 0) {
  992. seq_puts(m, " ");
  993. p = keylen <= sizeof(cookie->inline_key) ?
  994. cookie->inline_key : cookie->key;
  995. for (; keylen > 0; keylen--)
  996. seq_printf(m, "%02x", *p++);
  997. if (auxlen > 0) {
  998. seq_puts(m, ", ");
  999. p = auxlen <= sizeof(cookie->inline_aux) ?
  1000. cookie->inline_aux : cookie->aux;
  1001. for (; auxlen > 0; auxlen--)
  1002. seq_printf(m, "%02x", *p++);
  1003. }
  1004. }
  1005. seq_puts(m, "\n");
  1006. return 0;
  1007. }
  1008. static void *fscache_cookies_seq_start(struct seq_file *m, loff_t *_pos)
  1009. __acquires(fscache_cookies_lock)
  1010. {
  1011. read_lock(&fscache_cookies_lock);
  1012. return seq_list_start_head(&fscache_cookies, *_pos);
  1013. }
  1014. static void *fscache_cookies_seq_next(struct seq_file *m, void *v, loff_t *_pos)
  1015. {
  1016. return seq_list_next(v, &fscache_cookies, _pos);
  1017. }
  1018. static void fscache_cookies_seq_stop(struct seq_file *m, void *v)
  1019. __releases(rcu)
  1020. {
  1021. read_unlock(&fscache_cookies_lock);
  1022. }
  1023. const struct seq_operations fscache_cookies_seq_ops = {
  1024. .start = fscache_cookies_seq_start,
  1025. .next = fscache_cookies_seq_next,
  1026. .stop = fscache_cookies_seq_stop,
  1027. .show = fscache_cookies_seq_show,
  1028. };
  1029. #endif