object.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /* FS-Cache object state machine handler
  2. *
  3. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. *
  11. * See Documentation/filesystems/caching/object.txt for a description of the
  12. * object state machine and the in-kernel representations.
  13. */
  14. #define FSCACHE_DEBUG_LEVEL COOKIE
  15. #include <linux/module.h>
  16. #include <linux/seq_file.h>
  17. #include "internal.h"
  18. const char *fscache_object_states[FSCACHE_OBJECT__NSTATES] = {
  19. [FSCACHE_OBJECT_INIT] = "OBJECT_INIT",
  20. [FSCACHE_OBJECT_LOOKING_UP] = "OBJECT_LOOKING_UP",
  21. [FSCACHE_OBJECT_CREATING] = "OBJECT_CREATING",
  22. [FSCACHE_OBJECT_AVAILABLE] = "OBJECT_AVAILABLE",
  23. [FSCACHE_OBJECT_ACTIVE] = "OBJECT_ACTIVE",
  24. [FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING",
  25. [FSCACHE_OBJECT_DYING] = "OBJECT_DYING",
  26. [FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING",
  27. [FSCACHE_OBJECT_ABORT_INIT] = "OBJECT_ABORT_INIT",
  28. [FSCACHE_OBJECT_RELEASING] = "OBJECT_RELEASING",
  29. [FSCACHE_OBJECT_RECYCLING] = "OBJECT_RECYCLING",
  30. [FSCACHE_OBJECT_WITHDRAWING] = "OBJECT_WITHDRAWING",
  31. [FSCACHE_OBJECT_DEAD] = "OBJECT_DEAD",
  32. };
  33. EXPORT_SYMBOL(fscache_object_states);
  34. static const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
  35. [FSCACHE_OBJECT_INIT] = "INIT",
  36. [FSCACHE_OBJECT_LOOKING_UP] = "LOOK",
  37. [FSCACHE_OBJECT_CREATING] = "CRTN",
  38. [FSCACHE_OBJECT_AVAILABLE] = "AVBL",
  39. [FSCACHE_OBJECT_ACTIVE] = "ACTV",
  40. [FSCACHE_OBJECT_UPDATING] = "UPDT",
  41. [FSCACHE_OBJECT_DYING] = "DYNG",
  42. [FSCACHE_OBJECT_LC_DYING] = "LCDY",
  43. [FSCACHE_OBJECT_ABORT_INIT] = "ABTI",
  44. [FSCACHE_OBJECT_RELEASING] = "RELS",
  45. [FSCACHE_OBJECT_RECYCLING] = "RCYC",
  46. [FSCACHE_OBJECT_WITHDRAWING] = "WTHD",
  47. [FSCACHE_OBJECT_DEAD] = "DEAD",
  48. };
  49. static void fscache_object_slow_work_put_ref(struct slow_work *);
  50. static int fscache_object_slow_work_get_ref(struct slow_work *);
  51. static void fscache_object_slow_work_execute(struct slow_work *);
  52. #ifdef CONFIG_SLOW_WORK_PROC
  53. static void fscache_object_slow_work_desc(struct slow_work *, struct seq_file *);
  54. #endif
  55. static void fscache_initialise_object(struct fscache_object *);
  56. static void fscache_lookup_object(struct fscache_object *);
  57. static void fscache_object_available(struct fscache_object *);
  58. static void fscache_release_object(struct fscache_object *);
  59. static void fscache_withdraw_object(struct fscache_object *);
  60. static void fscache_enqueue_dependents(struct fscache_object *);
  61. static void fscache_dequeue_object(struct fscache_object *);
  62. const struct slow_work_ops fscache_object_slow_work_ops = {
  63. .owner = THIS_MODULE,
  64. .get_ref = fscache_object_slow_work_get_ref,
  65. .put_ref = fscache_object_slow_work_put_ref,
  66. .execute = fscache_object_slow_work_execute,
  67. #ifdef CONFIG_SLOW_WORK_PROC
  68. .desc = fscache_object_slow_work_desc,
  69. #endif
  70. };
  71. EXPORT_SYMBOL(fscache_object_slow_work_ops);
  72. /*
  73. * we need to notify the parent when an op completes that we had outstanding
  74. * upon it
  75. */
  76. static inline void fscache_done_parent_op(struct fscache_object *object)
  77. {
  78. struct fscache_object *parent = object->parent;
  79. _enter("OBJ%x {OBJ%x,%x}",
  80. object->debug_id, parent->debug_id, parent->n_ops);
  81. spin_lock_nested(&parent->lock, 1);
  82. parent->n_ops--;
  83. parent->n_obj_ops--;
  84. if (parent->n_ops == 0)
  85. fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
  86. spin_unlock(&parent->lock);
  87. }
  88. /*
  89. * process events that have been sent to an object's state machine
  90. * - initiates parent lookup
  91. * - does object lookup
  92. * - does object creation
  93. * - does object recycling and retirement
  94. * - does object withdrawal
  95. */
  96. static void fscache_object_state_machine(struct fscache_object *object)
  97. {
  98. enum fscache_object_state new_state;
  99. ASSERT(object != NULL);
  100. _enter("{OBJ%x,%s,%lx}",
  101. object->debug_id, fscache_object_states[object->state],
  102. object->events);
  103. switch (object->state) {
  104. /* wait for the parent object to become ready */
  105. case FSCACHE_OBJECT_INIT:
  106. object->event_mask =
  107. ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  108. fscache_initialise_object(object);
  109. goto done;
  110. /* look up the object metadata on disk */
  111. case FSCACHE_OBJECT_LOOKING_UP:
  112. fscache_lookup_object(object);
  113. goto lookup_transit;
  114. /* create the object metadata on disk */
  115. case FSCACHE_OBJECT_CREATING:
  116. fscache_lookup_object(object);
  117. goto lookup_transit;
  118. /* handle an object becoming available; start pending
  119. * operations and queue dependent operations for processing */
  120. case FSCACHE_OBJECT_AVAILABLE:
  121. fscache_object_available(object);
  122. goto active_transit;
  123. /* normal running state */
  124. case FSCACHE_OBJECT_ACTIVE:
  125. goto active_transit;
  126. /* update the object metadata on disk */
  127. case FSCACHE_OBJECT_UPDATING:
  128. clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
  129. fscache_stat(&fscache_n_updates_run);
  130. object->cache->ops->update_object(object);
  131. goto active_transit;
  132. /* handle an object dying during lookup or creation */
  133. case FSCACHE_OBJECT_LC_DYING:
  134. object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
  135. object->cache->ops->lookup_complete(object);
  136. spin_lock(&object->lock);
  137. object->state = FSCACHE_OBJECT_DYING;
  138. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
  139. &object->cookie->flags))
  140. wake_up_bit(&object->cookie->flags,
  141. FSCACHE_COOKIE_CREATING);
  142. spin_unlock(&object->lock);
  143. fscache_done_parent_op(object);
  144. /* wait for completion of all active operations on this object
  145. * and the death of all child objects of this object */
  146. case FSCACHE_OBJECT_DYING:
  147. dying:
  148. clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
  149. spin_lock(&object->lock);
  150. _debug("dying OBJ%x {%d,%d}",
  151. object->debug_id, object->n_ops, object->n_children);
  152. if (object->n_ops == 0 && object->n_children == 0) {
  153. object->event_mask &=
  154. ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  155. object->event_mask |=
  156. (1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  157. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  158. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  159. (1 << FSCACHE_OBJECT_EV_ERROR);
  160. } else {
  161. object->event_mask &=
  162. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  163. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  164. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  165. (1 << FSCACHE_OBJECT_EV_ERROR));
  166. object->event_mask |=
  167. 1 << FSCACHE_OBJECT_EV_CLEARED;
  168. }
  169. spin_unlock(&object->lock);
  170. fscache_enqueue_dependents(object);
  171. goto terminal_transit;
  172. /* handle an abort during initialisation */
  173. case FSCACHE_OBJECT_ABORT_INIT:
  174. _debug("handle abort init %lx", object->events);
  175. object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
  176. spin_lock(&object->lock);
  177. fscache_dequeue_object(object);
  178. object->state = FSCACHE_OBJECT_DYING;
  179. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
  180. &object->cookie->flags))
  181. wake_up_bit(&object->cookie->flags,
  182. FSCACHE_COOKIE_CREATING);
  183. spin_unlock(&object->lock);
  184. goto dying;
  185. /* handle the netfs releasing an object and possibly marking it
  186. * obsolete too */
  187. case FSCACHE_OBJECT_RELEASING:
  188. case FSCACHE_OBJECT_RECYCLING:
  189. object->event_mask &=
  190. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  191. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  192. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  193. (1 << FSCACHE_OBJECT_EV_ERROR));
  194. fscache_release_object(object);
  195. spin_lock(&object->lock);
  196. object->state = FSCACHE_OBJECT_DEAD;
  197. spin_unlock(&object->lock);
  198. fscache_stat(&fscache_n_object_dead);
  199. goto terminal_transit;
  200. /* handle the parent cache of this object being withdrawn from
  201. * active service */
  202. case FSCACHE_OBJECT_WITHDRAWING:
  203. object->event_mask &=
  204. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  205. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  206. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  207. (1 << FSCACHE_OBJECT_EV_ERROR));
  208. fscache_withdraw_object(object);
  209. spin_lock(&object->lock);
  210. object->state = FSCACHE_OBJECT_DEAD;
  211. spin_unlock(&object->lock);
  212. fscache_stat(&fscache_n_object_dead);
  213. goto terminal_transit;
  214. /* complain about the object being woken up once it is
  215. * deceased */
  216. case FSCACHE_OBJECT_DEAD:
  217. printk(KERN_ERR "FS-Cache:"
  218. " Unexpected event in dead state %lx\n",
  219. object->events & object->event_mask);
  220. BUG();
  221. default:
  222. printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
  223. object->state);
  224. BUG();
  225. }
  226. /* determine the transition from a lookup state */
  227. lookup_transit:
  228. switch (fls(object->events & object->event_mask) - 1) {
  229. case FSCACHE_OBJECT_EV_WITHDRAW:
  230. case FSCACHE_OBJECT_EV_RETIRE:
  231. case FSCACHE_OBJECT_EV_RELEASE:
  232. case FSCACHE_OBJECT_EV_ERROR:
  233. new_state = FSCACHE_OBJECT_LC_DYING;
  234. goto change_state;
  235. case FSCACHE_OBJECT_EV_REQUEUE:
  236. goto done;
  237. case -1:
  238. goto done; /* sleep until event */
  239. default:
  240. goto unsupported_event;
  241. }
  242. /* determine the transition from an active state */
  243. active_transit:
  244. switch (fls(object->events & object->event_mask) - 1) {
  245. case FSCACHE_OBJECT_EV_WITHDRAW:
  246. case FSCACHE_OBJECT_EV_RETIRE:
  247. case FSCACHE_OBJECT_EV_RELEASE:
  248. case FSCACHE_OBJECT_EV_ERROR:
  249. new_state = FSCACHE_OBJECT_DYING;
  250. goto change_state;
  251. case FSCACHE_OBJECT_EV_UPDATE:
  252. new_state = FSCACHE_OBJECT_UPDATING;
  253. goto change_state;
  254. case -1:
  255. new_state = FSCACHE_OBJECT_ACTIVE;
  256. goto change_state; /* sleep until event */
  257. default:
  258. goto unsupported_event;
  259. }
  260. /* determine the transition from a terminal state */
  261. terminal_transit:
  262. switch (fls(object->events & object->event_mask) - 1) {
  263. case FSCACHE_OBJECT_EV_WITHDRAW:
  264. new_state = FSCACHE_OBJECT_WITHDRAWING;
  265. goto change_state;
  266. case FSCACHE_OBJECT_EV_RETIRE:
  267. new_state = FSCACHE_OBJECT_RECYCLING;
  268. goto change_state;
  269. case FSCACHE_OBJECT_EV_RELEASE:
  270. new_state = FSCACHE_OBJECT_RELEASING;
  271. goto change_state;
  272. case FSCACHE_OBJECT_EV_ERROR:
  273. new_state = FSCACHE_OBJECT_WITHDRAWING;
  274. goto change_state;
  275. case FSCACHE_OBJECT_EV_CLEARED:
  276. new_state = FSCACHE_OBJECT_DYING;
  277. goto change_state;
  278. case -1:
  279. goto done; /* sleep until event */
  280. default:
  281. goto unsupported_event;
  282. }
  283. change_state:
  284. spin_lock(&object->lock);
  285. object->state = new_state;
  286. spin_unlock(&object->lock);
  287. done:
  288. _leave(" [->%s]", fscache_object_states[object->state]);
  289. return;
  290. unsupported_event:
  291. printk(KERN_ERR "FS-Cache:"
  292. " Unsupported event %lx [mask %lx] in state %s\n",
  293. object->events, object->event_mask,
  294. fscache_object_states[object->state]);
  295. BUG();
  296. }
  297. /*
  298. * execute an object
  299. */
  300. static void fscache_object_slow_work_execute(struct slow_work *work)
  301. {
  302. struct fscache_object *object =
  303. container_of(work, struct fscache_object, work);
  304. unsigned long start;
  305. _enter("{OBJ%x}", object->debug_id);
  306. clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  307. start = jiffies;
  308. fscache_object_state_machine(object);
  309. fscache_hist(fscache_objs_histogram, start);
  310. if (object->events & object->event_mask)
  311. fscache_enqueue_object(object);
  312. }
  313. /*
  314. * describe an object for slow-work debugging
  315. */
  316. #ifdef CONFIG_SLOW_WORK_PROC
  317. static void fscache_object_slow_work_desc(struct slow_work *work,
  318. struct seq_file *m)
  319. {
  320. struct fscache_object *object =
  321. container_of(work, struct fscache_object, work);
  322. seq_printf(m, "FSC: OBJ%x: %s",
  323. object->debug_id,
  324. fscache_object_states_short[object->state]);
  325. }
  326. #endif
  327. /*
  328. * initialise an object
  329. * - check the specified object's parent to see if we can make use of it
  330. * immediately to do a creation
  331. * - we may need to start the process of creating a parent and we need to wait
  332. * for the parent's lookup and creation to complete if it's not there yet
  333. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  334. * leaf-most cookies of the object and all its children
  335. */
  336. static void fscache_initialise_object(struct fscache_object *object)
  337. {
  338. struct fscache_object *parent;
  339. _enter("");
  340. ASSERT(object->cookie != NULL);
  341. ASSERT(object->cookie->parent != NULL);
  342. ASSERT(list_empty(&object->work.link));
  343. if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
  344. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  345. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  346. (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
  347. _debug("abort init %lx", object->events);
  348. spin_lock(&object->lock);
  349. object->state = FSCACHE_OBJECT_ABORT_INIT;
  350. spin_unlock(&object->lock);
  351. return;
  352. }
  353. spin_lock(&object->cookie->lock);
  354. spin_lock_nested(&object->cookie->parent->lock, 1);
  355. parent = object->parent;
  356. if (!parent) {
  357. _debug("no parent");
  358. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  359. } else {
  360. spin_lock(&object->lock);
  361. spin_lock_nested(&parent->lock, 1);
  362. _debug("parent %s", fscache_object_states[parent->state]);
  363. if (parent->state >= FSCACHE_OBJECT_DYING) {
  364. _debug("bad parent");
  365. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  366. } else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
  367. _debug("wait");
  368. /* we may get woken up in this state by child objects
  369. * binding on to us, so we need to make sure we don't
  370. * add ourself to the list multiple times */
  371. if (list_empty(&object->dep_link)) {
  372. object->cache->ops->grab_object(object);
  373. list_add(&object->dep_link,
  374. &parent->dependents);
  375. /* fscache_acquire_non_index_cookie() uses this
  376. * to wake the chain up */
  377. if (parent->state == FSCACHE_OBJECT_INIT)
  378. fscache_enqueue_object(parent);
  379. }
  380. } else {
  381. _debug("go");
  382. parent->n_ops++;
  383. parent->n_obj_ops++;
  384. object->lookup_jif = jiffies;
  385. object->state = FSCACHE_OBJECT_LOOKING_UP;
  386. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  387. }
  388. spin_unlock(&parent->lock);
  389. spin_unlock(&object->lock);
  390. }
  391. spin_unlock(&object->cookie->parent->lock);
  392. spin_unlock(&object->cookie->lock);
  393. _leave("");
  394. }
  395. /*
  396. * look an object up in the cache from which it was allocated
  397. * - we hold an "access lock" on the parent object, so the parent object cannot
  398. * be withdrawn by either party till we've finished
  399. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  400. * leaf-most cookies of the object and all its children
  401. */
  402. static void fscache_lookup_object(struct fscache_object *object)
  403. {
  404. struct fscache_cookie *cookie = object->cookie;
  405. struct fscache_object *parent;
  406. _enter("");
  407. parent = object->parent;
  408. ASSERT(parent != NULL);
  409. ASSERTCMP(parent->n_ops, >, 0);
  410. ASSERTCMP(parent->n_obj_ops, >, 0);
  411. /* make sure the parent is still available */
  412. ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
  413. if (parent->state >= FSCACHE_OBJECT_DYING ||
  414. test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  415. _debug("unavailable");
  416. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  417. _leave("");
  418. return;
  419. }
  420. _debug("LOOKUP \"%s/%s\" in \"%s\"",
  421. parent->cookie->def->name, cookie->def->name,
  422. object->cache->tag->name);
  423. fscache_stat(&fscache_n_object_lookups);
  424. object->cache->ops->lookup_object(object);
  425. if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
  426. set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
  427. _leave("");
  428. }
  429. /**
  430. * fscache_object_lookup_negative - Note negative cookie lookup
  431. * @object: Object pointing to cookie to mark
  432. *
  433. * Note negative lookup, permitting those waiting to read data from an already
  434. * existing backing object to continue as there's no data for them to read.
  435. */
  436. void fscache_object_lookup_negative(struct fscache_object *object)
  437. {
  438. struct fscache_cookie *cookie = object->cookie;
  439. _enter("{OBJ%x,%s}",
  440. object->debug_id, fscache_object_states[object->state]);
  441. spin_lock(&object->lock);
  442. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  443. fscache_stat(&fscache_n_object_lookups_negative);
  444. /* transit here to allow write requests to begin stacking up
  445. * and read requests to begin returning ENODATA */
  446. object->state = FSCACHE_OBJECT_CREATING;
  447. spin_unlock(&object->lock);
  448. set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
  449. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  450. _debug("wake up lookup %p", &cookie->flags);
  451. smp_mb__before_clear_bit();
  452. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  453. smp_mb__after_clear_bit();
  454. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  455. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  456. } else {
  457. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  458. spin_unlock(&object->lock);
  459. }
  460. _leave("");
  461. }
  462. EXPORT_SYMBOL(fscache_object_lookup_negative);
  463. /**
  464. * fscache_obtained_object - Note successful object lookup or creation
  465. * @object: Object pointing to cookie to mark
  466. *
  467. * Note successful lookup and/or creation, permitting those waiting to write
  468. * data to a backing object to continue.
  469. *
  470. * Note that after calling this, an object's cookie may be relinquished by the
  471. * netfs, and so must be accessed with object lock held.
  472. */
  473. void fscache_obtained_object(struct fscache_object *object)
  474. {
  475. struct fscache_cookie *cookie = object->cookie;
  476. _enter("{OBJ%x,%s}",
  477. object->debug_id, fscache_object_states[object->state]);
  478. /* if we were still looking up, then we must have a positive lookup
  479. * result, in which case there may be data available */
  480. spin_lock(&object->lock);
  481. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  482. fscache_stat(&fscache_n_object_lookups_positive);
  483. clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  484. object->state = FSCACHE_OBJECT_AVAILABLE;
  485. spin_unlock(&object->lock);
  486. smp_mb__before_clear_bit();
  487. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  488. smp_mb__after_clear_bit();
  489. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  490. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  491. } else {
  492. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  493. fscache_stat(&fscache_n_object_created);
  494. object->state = FSCACHE_OBJECT_AVAILABLE;
  495. spin_unlock(&object->lock);
  496. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  497. smp_wmb();
  498. }
  499. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
  500. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
  501. _leave("");
  502. }
  503. EXPORT_SYMBOL(fscache_obtained_object);
  504. /*
  505. * handle an object that has just become available
  506. */
  507. static void fscache_object_available(struct fscache_object *object)
  508. {
  509. _enter("{OBJ%x}", object->debug_id);
  510. spin_lock(&object->lock);
  511. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
  512. wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
  513. fscache_done_parent_op(object);
  514. if (object->n_in_progress == 0) {
  515. if (object->n_ops > 0) {
  516. ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
  517. ASSERTIF(object->n_ops > object->n_obj_ops,
  518. !list_empty(&object->pending_ops));
  519. fscache_start_operations(object);
  520. } else {
  521. ASSERT(list_empty(&object->pending_ops));
  522. }
  523. }
  524. spin_unlock(&object->lock);
  525. object->cache->ops->lookup_complete(object);
  526. fscache_enqueue_dependents(object);
  527. fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
  528. fscache_stat(&fscache_n_object_avail);
  529. _leave("");
  530. }
  531. /*
  532. * drop an object's attachments
  533. */
  534. static void fscache_drop_object(struct fscache_object *object)
  535. {
  536. struct fscache_object *parent = object->parent;
  537. struct fscache_cache *cache = object->cache;
  538. _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
  539. spin_lock(&cache->object_list_lock);
  540. list_del_init(&object->cache_link);
  541. spin_unlock(&cache->object_list_lock);
  542. cache->ops->drop_object(object);
  543. if (parent) {
  544. _debug("release parent OBJ%x {%d}",
  545. parent->debug_id, parent->n_children);
  546. spin_lock(&parent->lock);
  547. parent->n_children--;
  548. if (parent->n_children == 0)
  549. fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
  550. spin_unlock(&parent->lock);
  551. object->parent = NULL;
  552. }
  553. /* this just shifts the object release to the slow work processor */
  554. object->cache->ops->put_object(object);
  555. _leave("");
  556. }
  557. /*
  558. * release or recycle an object that the netfs has discarded
  559. */
  560. static void fscache_release_object(struct fscache_object *object)
  561. {
  562. _enter("");
  563. fscache_drop_object(object);
  564. }
  565. /*
  566. * withdraw an object from active service
  567. */
  568. static void fscache_withdraw_object(struct fscache_object *object)
  569. {
  570. struct fscache_cookie *cookie;
  571. bool detached;
  572. _enter("");
  573. spin_lock(&object->lock);
  574. cookie = object->cookie;
  575. if (cookie) {
  576. /* need to get the cookie lock before the object lock, starting
  577. * from the object pointer */
  578. atomic_inc(&cookie->usage);
  579. spin_unlock(&object->lock);
  580. detached = false;
  581. spin_lock(&cookie->lock);
  582. spin_lock(&object->lock);
  583. if (object->cookie == cookie) {
  584. hlist_del_init(&object->cookie_link);
  585. object->cookie = NULL;
  586. detached = true;
  587. }
  588. spin_unlock(&cookie->lock);
  589. fscache_cookie_put(cookie);
  590. if (detached)
  591. fscache_cookie_put(cookie);
  592. }
  593. spin_unlock(&object->lock);
  594. fscache_drop_object(object);
  595. }
  596. /*
  597. * withdraw an object from active service at the behest of the cache
  598. * - need break the links to a cached object cookie
  599. * - called under two situations:
  600. * (1) recycler decides to reclaim an in-use object
  601. * (2) a cache is unmounted
  602. * - have to take care as the cookie can be being relinquished by the netfs
  603. * simultaneously
  604. * - the object is pinned by the caller holding a refcount on it
  605. */
  606. void fscache_withdrawing_object(struct fscache_cache *cache,
  607. struct fscache_object *object)
  608. {
  609. bool enqueue = false;
  610. _enter(",OBJ%x", object->debug_id);
  611. spin_lock(&object->lock);
  612. if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
  613. object->state = FSCACHE_OBJECT_WITHDRAWING;
  614. enqueue = true;
  615. }
  616. spin_unlock(&object->lock);
  617. if (enqueue)
  618. fscache_enqueue_object(object);
  619. _leave("");
  620. }
  621. /*
  622. * allow the slow work item processor to get a ref on an object
  623. */
  624. static int fscache_object_slow_work_get_ref(struct slow_work *work)
  625. {
  626. struct fscache_object *object =
  627. container_of(work, struct fscache_object, work);
  628. return object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
  629. }
  630. /*
  631. * allow the slow work item processor to discard a ref on a work item
  632. */
  633. static void fscache_object_slow_work_put_ref(struct slow_work *work)
  634. {
  635. struct fscache_object *object =
  636. container_of(work, struct fscache_object, work);
  637. return object->cache->ops->put_object(object);
  638. }
  639. /*
  640. * enqueue an object for metadata-type processing
  641. */
  642. void fscache_enqueue_object(struct fscache_object *object)
  643. {
  644. _enter("{OBJ%x}", object->debug_id);
  645. slow_work_enqueue(&object->work);
  646. }
  647. /*
  648. * enqueue the dependents of an object for metadata-type processing
  649. * - the caller must hold the object's lock
  650. * - this may cause an already locked object to wind up being processed again
  651. */
  652. static void fscache_enqueue_dependents(struct fscache_object *object)
  653. {
  654. struct fscache_object *dep;
  655. _enter("{OBJ%x}", object->debug_id);
  656. if (list_empty(&object->dependents))
  657. return;
  658. spin_lock(&object->lock);
  659. while (!list_empty(&object->dependents)) {
  660. dep = list_entry(object->dependents.next,
  661. struct fscache_object, dep_link);
  662. list_del_init(&dep->dep_link);
  663. /* sort onto appropriate lists */
  664. fscache_enqueue_object(dep);
  665. dep->cache->ops->put_object(dep);
  666. if (!list_empty(&object->dependents))
  667. cond_resched_lock(&object->lock);
  668. }
  669. spin_unlock(&object->lock);
  670. }
  671. /*
  672. * remove an object from whatever queue it's waiting on
  673. * - the caller must hold object->lock
  674. */
  675. void fscache_dequeue_object(struct fscache_object *object)
  676. {
  677. _enter("{OBJ%x}", object->debug_id);
  678. if (!list_empty(&object->dep_link)) {
  679. spin_lock(&object->parent->lock);
  680. list_del_init(&object->dep_link);
  681. spin_unlock(&object->parent->lock);
  682. }
  683. _leave("");
  684. }
  685. /**
  686. * fscache_check_aux - Ask the netfs whether an object on disk is still valid
  687. * @object: The object to ask about
  688. * @data: The auxiliary data for the object
  689. * @datalen: The size of the auxiliary data
  690. *
  691. * This function consults the netfs about the coherency state of an object
  692. */
  693. enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  694. const void *data, uint16_t datalen)
  695. {
  696. enum fscache_checkaux result;
  697. if (!object->cookie->def->check_aux) {
  698. fscache_stat(&fscache_n_checkaux_none);
  699. return FSCACHE_CHECKAUX_OKAY;
  700. }
  701. result = object->cookie->def->check_aux(object->cookie->netfs_data,
  702. data, datalen);
  703. switch (result) {
  704. /* entry okay as is */
  705. case FSCACHE_CHECKAUX_OKAY:
  706. fscache_stat(&fscache_n_checkaux_okay);
  707. break;
  708. /* entry requires update */
  709. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  710. fscache_stat(&fscache_n_checkaux_update);
  711. break;
  712. /* entry requires deletion */
  713. case FSCACHE_CHECKAUX_OBSOLETE:
  714. fscache_stat(&fscache_n_checkaux_obsolete);
  715. break;
  716. default:
  717. BUG();
  718. }
  719. return result;
  720. }
  721. EXPORT_SYMBOL(fscache_check_aux);