object.c 25 KB

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