object.c 23 KB

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