object.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  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/slab.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_INVALIDATING] = "OBJECT_INVALIDATING",
  25. [FSCACHE_OBJECT_UPDATING] = "OBJECT_UPDATING",
  26. [FSCACHE_OBJECT_DYING] = "OBJECT_DYING",
  27. [FSCACHE_OBJECT_LC_DYING] = "OBJECT_LC_DYING",
  28. [FSCACHE_OBJECT_ABORT_INIT] = "OBJECT_ABORT_INIT",
  29. [FSCACHE_OBJECT_RELEASING] = "OBJECT_RELEASING",
  30. [FSCACHE_OBJECT_RECYCLING] = "OBJECT_RECYCLING",
  31. [FSCACHE_OBJECT_WITHDRAWING] = "OBJECT_WITHDRAWING",
  32. [FSCACHE_OBJECT_DEAD] = "OBJECT_DEAD",
  33. };
  34. EXPORT_SYMBOL(fscache_object_states);
  35. const char fscache_object_states_short[FSCACHE_OBJECT__NSTATES][5] = {
  36. [FSCACHE_OBJECT_INIT] = "INIT",
  37. [FSCACHE_OBJECT_LOOKING_UP] = "LOOK",
  38. [FSCACHE_OBJECT_CREATING] = "CRTN",
  39. [FSCACHE_OBJECT_AVAILABLE] = "AVBL",
  40. [FSCACHE_OBJECT_ACTIVE] = "ACTV",
  41. [FSCACHE_OBJECT_INVALIDATING] = "INVL",
  42. [FSCACHE_OBJECT_UPDATING] = "UPDT",
  43. [FSCACHE_OBJECT_DYING] = "DYNG",
  44. [FSCACHE_OBJECT_LC_DYING] = "LCDY",
  45. [FSCACHE_OBJECT_ABORT_INIT] = "ABTI",
  46. [FSCACHE_OBJECT_RELEASING] = "RELS",
  47. [FSCACHE_OBJECT_RECYCLING] = "RCYC",
  48. [FSCACHE_OBJECT_WITHDRAWING] = "WTHD",
  49. [FSCACHE_OBJECT_DEAD] = "DEAD",
  50. };
  51. static int fscache_get_object(struct fscache_object *);
  52. static void fscache_put_object(struct fscache_object *);
  53. static void fscache_initialise_object(struct fscache_object *);
  54. static void fscache_lookup_object(struct fscache_object *);
  55. static void fscache_object_available(struct fscache_object *);
  56. static void fscache_invalidate_object(struct fscache_object *);
  57. static void fscache_release_object(struct fscache_object *);
  58. static void fscache_withdraw_object(struct fscache_object *);
  59. static void fscache_enqueue_dependents(struct fscache_object *);
  60. static void fscache_dequeue_object(struct fscache_object *);
  61. /*
  62. * we need to notify the parent when an op completes that we had outstanding
  63. * upon it
  64. */
  65. static inline void fscache_done_parent_op(struct fscache_object *object)
  66. {
  67. struct fscache_object *parent = object->parent;
  68. _enter("OBJ%x {OBJ%x,%x}",
  69. object->debug_id, parent->debug_id, parent->n_ops);
  70. spin_lock_nested(&parent->lock, 1);
  71. parent->n_ops--;
  72. parent->n_obj_ops--;
  73. if (parent->n_ops == 0)
  74. fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
  75. spin_unlock(&parent->lock);
  76. }
  77. /*
  78. * Notify netfs of invalidation completion.
  79. */
  80. static inline void fscache_invalidation_complete(struct fscache_cookie *cookie)
  81. {
  82. if (test_and_clear_bit(FSCACHE_COOKIE_INVALIDATING, &cookie->flags))
  83. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_INVALIDATING);
  84. }
  85. /*
  86. * process events that have been sent to an object's state machine
  87. * - initiates parent lookup
  88. * - does object lookup
  89. * - does object creation
  90. * - does object recycling and retirement
  91. * - does object withdrawal
  92. */
  93. static void fscache_object_state_machine(struct fscache_object *object)
  94. {
  95. enum fscache_object_state new_state;
  96. struct fscache_cookie *cookie;
  97. ASSERT(object != NULL);
  98. _enter("{OBJ%x,%s,%lx}",
  99. object->debug_id, fscache_object_states[object->state],
  100. object->events);
  101. switch (object->state) {
  102. /* wait for the parent object to become ready */
  103. case FSCACHE_OBJECT_INIT:
  104. object->event_mask =
  105. ULONG_MAX & ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  106. fscache_initialise_object(object);
  107. goto done;
  108. /* look up the object metadata on disk */
  109. case FSCACHE_OBJECT_LOOKING_UP:
  110. fscache_lookup_object(object);
  111. goto lookup_transit;
  112. /* create the object metadata on disk */
  113. case FSCACHE_OBJECT_CREATING:
  114. fscache_lookup_object(object);
  115. goto lookup_transit;
  116. /* handle an object becoming available; start pending
  117. * operations and queue dependent operations for processing */
  118. case FSCACHE_OBJECT_AVAILABLE:
  119. fscache_object_available(object);
  120. goto active_transit;
  121. /* normal running state */
  122. case FSCACHE_OBJECT_ACTIVE:
  123. goto active_transit;
  124. /* Invalidate an object on disk */
  125. case FSCACHE_OBJECT_INVALIDATING:
  126. clear_bit(FSCACHE_OBJECT_EV_INVALIDATE, &object->events);
  127. fscache_stat(&fscache_n_invalidates_run);
  128. fscache_stat(&fscache_n_cop_invalidate_object);
  129. fscache_invalidate_object(object);
  130. fscache_stat_d(&fscache_n_cop_invalidate_object);
  131. fscache_raise_event(object, FSCACHE_OBJECT_EV_UPDATE);
  132. goto active_transit;
  133. /* update the object metadata on disk */
  134. case FSCACHE_OBJECT_UPDATING:
  135. clear_bit(FSCACHE_OBJECT_EV_UPDATE, &object->events);
  136. fscache_stat(&fscache_n_updates_run);
  137. fscache_stat(&fscache_n_cop_update_object);
  138. object->cache->ops->update_object(object);
  139. fscache_stat_d(&fscache_n_cop_update_object);
  140. goto active_transit;
  141. /* handle an object dying during lookup or creation */
  142. case FSCACHE_OBJECT_LC_DYING:
  143. object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
  144. fscache_stat(&fscache_n_cop_lookup_complete);
  145. object->cache->ops->lookup_complete(object);
  146. fscache_stat_d(&fscache_n_cop_lookup_complete);
  147. spin_lock(&object->lock);
  148. object->state = FSCACHE_OBJECT_DYING;
  149. cookie = object->cookie;
  150. if (cookie) {
  151. if (test_and_clear_bit(FSCACHE_COOKIE_LOOKING_UP,
  152. &cookie->flags))
  153. wake_up_bit(&cookie->flags,
  154. FSCACHE_COOKIE_LOOKING_UP);
  155. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
  156. &cookie->flags))
  157. wake_up_bit(&cookie->flags,
  158. FSCACHE_COOKIE_CREATING);
  159. }
  160. spin_unlock(&object->lock);
  161. fscache_done_parent_op(object);
  162. /* wait for completion of all active operations on this object
  163. * and the death of all child objects of this object */
  164. case FSCACHE_OBJECT_DYING:
  165. dying:
  166. clear_bit(FSCACHE_OBJECT_EV_CLEARED, &object->events);
  167. spin_lock(&object->lock);
  168. _debug("dying OBJ%x {%d,%d}",
  169. object->debug_id, object->n_ops, object->n_children);
  170. if (object->n_ops == 0 && object->n_children == 0) {
  171. object->event_mask &=
  172. ~(1 << FSCACHE_OBJECT_EV_CLEARED);
  173. object->event_mask |=
  174. (1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  175. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  176. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  177. (1 << FSCACHE_OBJECT_EV_ERROR);
  178. } else {
  179. object->event_mask &=
  180. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  181. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  182. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  183. (1 << FSCACHE_OBJECT_EV_ERROR));
  184. object->event_mask |=
  185. 1 << FSCACHE_OBJECT_EV_CLEARED;
  186. }
  187. spin_unlock(&object->lock);
  188. fscache_enqueue_dependents(object);
  189. fscache_start_operations(object);
  190. goto terminal_transit;
  191. /* handle an abort during initialisation */
  192. case FSCACHE_OBJECT_ABORT_INIT:
  193. _debug("handle abort init %lx", object->events);
  194. object->event_mask &= ~(1 << FSCACHE_OBJECT_EV_UPDATE);
  195. spin_lock(&object->lock);
  196. fscache_dequeue_object(object);
  197. object->state = FSCACHE_OBJECT_DYING;
  198. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING,
  199. &object->cookie->flags))
  200. wake_up_bit(&object->cookie->flags,
  201. FSCACHE_COOKIE_CREATING);
  202. spin_unlock(&object->lock);
  203. goto dying;
  204. /* handle the netfs releasing an object and possibly marking it
  205. * obsolete too */
  206. case FSCACHE_OBJECT_RELEASING:
  207. case FSCACHE_OBJECT_RECYCLING:
  208. object->event_mask &=
  209. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  210. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  211. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  212. (1 << FSCACHE_OBJECT_EV_ERROR));
  213. fscache_release_object(object);
  214. spin_lock(&object->lock);
  215. object->state = FSCACHE_OBJECT_DEAD;
  216. spin_unlock(&object->lock);
  217. fscache_stat(&fscache_n_object_dead);
  218. goto terminal_transit;
  219. /* handle the parent cache of this object being withdrawn from
  220. * active service */
  221. case FSCACHE_OBJECT_WITHDRAWING:
  222. object->event_mask &=
  223. ~((1 << FSCACHE_OBJECT_EV_WITHDRAW) |
  224. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  225. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  226. (1 << FSCACHE_OBJECT_EV_ERROR));
  227. fscache_withdraw_object(object);
  228. spin_lock(&object->lock);
  229. object->state = FSCACHE_OBJECT_DEAD;
  230. spin_unlock(&object->lock);
  231. fscache_stat(&fscache_n_object_dead);
  232. goto terminal_transit;
  233. /* complain about the object being woken up once it is
  234. * deceased */
  235. case FSCACHE_OBJECT_DEAD:
  236. printk(KERN_ERR "FS-Cache:"
  237. " Unexpected event in dead state %lx\n",
  238. object->events & object->event_mask);
  239. BUG();
  240. default:
  241. printk(KERN_ERR "FS-Cache: Unknown object state %u\n",
  242. object->state);
  243. BUG();
  244. }
  245. /* determine the transition from a lookup state */
  246. lookup_transit:
  247. switch (fls(object->events & object->event_mask) - 1) {
  248. case FSCACHE_OBJECT_EV_WITHDRAW:
  249. case FSCACHE_OBJECT_EV_RETIRE:
  250. case FSCACHE_OBJECT_EV_RELEASE:
  251. case FSCACHE_OBJECT_EV_ERROR:
  252. new_state = FSCACHE_OBJECT_LC_DYING;
  253. goto change_state;
  254. case FSCACHE_OBJECT_EV_REQUEUE:
  255. goto done;
  256. case -1:
  257. goto done; /* sleep until event */
  258. default:
  259. goto unsupported_event;
  260. }
  261. /* determine the transition from an active state */
  262. active_transit:
  263. switch (fls(object->events & object->event_mask) - 1) {
  264. case FSCACHE_OBJECT_EV_WITHDRAW:
  265. case FSCACHE_OBJECT_EV_RETIRE:
  266. case FSCACHE_OBJECT_EV_RELEASE:
  267. case FSCACHE_OBJECT_EV_ERROR:
  268. new_state = FSCACHE_OBJECT_DYING;
  269. goto change_state;
  270. case FSCACHE_OBJECT_EV_INVALIDATE:
  271. new_state = FSCACHE_OBJECT_INVALIDATING;
  272. goto change_state;
  273. case FSCACHE_OBJECT_EV_UPDATE:
  274. new_state = FSCACHE_OBJECT_UPDATING;
  275. goto change_state;
  276. case -1:
  277. new_state = FSCACHE_OBJECT_ACTIVE;
  278. goto change_state; /* sleep until event */
  279. default:
  280. goto unsupported_event;
  281. }
  282. /* determine the transition from a terminal state */
  283. terminal_transit:
  284. switch (fls(object->events & object->event_mask) - 1) {
  285. case FSCACHE_OBJECT_EV_WITHDRAW:
  286. new_state = FSCACHE_OBJECT_WITHDRAWING;
  287. goto change_state;
  288. case FSCACHE_OBJECT_EV_RETIRE:
  289. new_state = FSCACHE_OBJECT_RECYCLING;
  290. goto change_state;
  291. case FSCACHE_OBJECT_EV_RELEASE:
  292. new_state = FSCACHE_OBJECT_RELEASING;
  293. goto change_state;
  294. case FSCACHE_OBJECT_EV_ERROR:
  295. new_state = FSCACHE_OBJECT_WITHDRAWING;
  296. goto change_state;
  297. case FSCACHE_OBJECT_EV_CLEARED:
  298. new_state = FSCACHE_OBJECT_DYING;
  299. goto change_state;
  300. case -1:
  301. goto done; /* sleep until event */
  302. default:
  303. goto unsupported_event;
  304. }
  305. change_state:
  306. spin_lock(&object->lock);
  307. object->state = new_state;
  308. spin_unlock(&object->lock);
  309. done:
  310. _leave(" [->%s]", fscache_object_states[object->state]);
  311. return;
  312. unsupported_event:
  313. printk(KERN_ERR "FS-Cache:"
  314. " Unsupported event %lx [mask %lx] in state %s\n",
  315. object->events, object->event_mask,
  316. fscache_object_states[object->state]);
  317. BUG();
  318. }
  319. /*
  320. * execute an object
  321. */
  322. void fscache_object_work_func(struct work_struct *work)
  323. {
  324. struct fscache_object *object =
  325. container_of(work, struct fscache_object, work);
  326. unsigned long start;
  327. _enter("{OBJ%x}", object->debug_id);
  328. start = jiffies;
  329. fscache_object_state_machine(object);
  330. fscache_hist(fscache_objs_histogram, start);
  331. if (object->events & object->event_mask)
  332. fscache_enqueue_object(object);
  333. clear_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  334. fscache_put_object(object);
  335. }
  336. EXPORT_SYMBOL(fscache_object_work_func);
  337. /*
  338. * initialise an object
  339. * - check the specified object's parent to see if we can make use of it
  340. * immediately to do a creation
  341. * - we may need to start the process of creating a parent and we need to wait
  342. * for the parent's lookup and creation to complete if it's not there yet
  343. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  344. * leaf-most cookies of the object and all its children
  345. */
  346. static void fscache_initialise_object(struct fscache_object *object)
  347. {
  348. struct fscache_object *parent;
  349. _enter("");
  350. ASSERT(object->cookie != NULL);
  351. ASSERT(object->cookie->parent != NULL);
  352. if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
  353. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  354. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  355. (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
  356. _debug("abort init %lx", object->events);
  357. spin_lock(&object->lock);
  358. object->state = FSCACHE_OBJECT_ABORT_INIT;
  359. spin_unlock(&object->lock);
  360. return;
  361. }
  362. spin_lock(&object->cookie->lock);
  363. spin_lock_nested(&object->cookie->parent->lock, 1);
  364. parent = object->parent;
  365. if (!parent) {
  366. _debug("no parent");
  367. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  368. } else {
  369. spin_lock(&object->lock);
  370. spin_lock_nested(&parent->lock, 1);
  371. _debug("parent %s", fscache_object_states[parent->state]);
  372. if (parent->state >= FSCACHE_OBJECT_DYING) {
  373. _debug("bad parent");
  374. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  375. } else if (parent->state < FSCACHE_OBJECT_AVAILABLE) {
  376. _debug("wait");
  377. /* we may get woken up in this state by child objects
  378. * binding on to us, so we need to make sure we don't
  379. * add ourself to the list multiple times */
  380. if (list_empty(&object->dep_link)) {
  381. fscache_stat(&fscache_n_cop_grab_object);
  382. object->cache->ops->grab_object(object);
  383. fscache_stat_d(&fscache_n_cop_grab_object);
  384. list_add(&object->dep_link,
  385. &parent->dependents);
  386. /* fscache_acquire_non_index_cookie() uses this
  387. * to wake the chain up */
  388. if (parent->state == FSCACHE_OBJECT_INIT)
  389. fscache_enqueue_object(parent);
  390. }
  391. } else {
  392. _debug("go");
  393. parent->n_ops++;
  394. parent->n_obj_ops++;
  395. object->lookup_jif = jiffies;
  396. object->state = FSCACHE_OBJECT_LOOKING_UP;
  397. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  398. }
  399. spin_unlock(&parent->lock);
  400. spin_unlock(&object->lock);
  401. }
  402. spin_unlock(&object->cookie->parent->lock);
  403. spin_unlock(&object->cookie->lock);
  404. _leave("");
  405. }
  406. /*
  407. * look an object up in the cache from which it was allocated
  408. * - we hold an "access lock" on the parent object, so the parent object cannot
  409. * be withdrawn by either party till we've finished
  410. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  411. * leaf-most cookies of the object and all its children
  412. */
  413. static void fscache_lookup_object(struct fscache_object *object)
  414. {
  415. struct fscache_cookie *cookie = object->cookie;
  416. struct fscache_object *parent;
  417. int ret;
  418. _enter("");
  419. parent = object->parent;
  420. ASSERT(parent != NULL);
  421. ASSERTCMP(parent->n_ops, >, 0);
  422. ASSERTCMP(parent->n_obj_ops, >, 0);
  423. /* make sure the parent is still available */
  424. ASSERTCMP(parent->state, >=, FSCACHE_OBJECT_AVAILABLE);
  425. if (parent->state >= FSCACHE_OBJECT_DYING ||
  426. test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  427. _debug("unavailable");
  428. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  429. _leave("");
  430. return;
  431. }
  432. _debug("LOOKUP \"%s/%s\" in \"%s\"",
  433. parent->cookie->def->name, cookie->def->name,
  434. object->cache->tag->name);
  435. fscache_stat(&fscache_n_object_lookups);
  436. fscache_stat(&fscache_n_cop_lookup_object);
  437. ret = object->cache->ops->lookup_object(object);
  438. fscache_stat_d(&fscache_n_cop_lookup_object);
  439. if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
  440. set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
  441. if (ret == -ETIMEDOUT) {
  442. /* probably stuck behind another object, so move this one to
  443. * the back of the queue */
  444. fscache_stat(&fscache_n_object_lookups_timed_out);
  445. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  446. }
  447. _leave("");
  448. }
  449. /**
  450. * fscache_object_lookup_negative - Note negative cookie lookup
  451. * @object: Object pointing to cookie to mark
  452. *
  453. * Note negative lookup, permitting those waiting to read data from an already
  454. * existing backing object to continue as there's no data for them to read.
  455. */
  456. void fscache_object_lookup_negative(struct fscache_object *object)
  457. {
  458. struct fscache_cookie *cookie = object->cookie;
  459. _enter("{OBJ%x,%s}",
  460. object->debug_id, fscache_object_states[object->state]);
  461. spin_lock(&object->lock);
  462. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  463. fscache_stat(&fscache_n_object_lookups_negative);
  464. /* transit here to allow write requests to begin stacking up
  465. * and read requests to begin returning ENODATA */
  466. object->state = FSCACHE_OBJECT_CREATING;
  467. spin_unlock(&object->lock);
  468. set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
  469. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  470. _debug("wake up lookup %p", &cookie->flags);
  471. smp_mb__before_clear_bit();
  472. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  473. smp_mb__after_clear_bit();
  474. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  475. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  476. } else {
  477. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  478. spin_unlock(&object->lock);
  479. }
  480. _leave("");
  481. }
  482. EXPORT_SYMBOL(fscache_object_lookup_negative);
  483. /**
  484. * fscache_obtained_object - Note successful object lookup or creation
  485. * @object: Object pointing to cookie to mark
  486. *
  487. * Note successful lookup and/or creation, permitting those waiting to write
  488. * data to a backing object to continue.
  489. *
  490. * Note that after calling this, an object's cookie may be relinquished by the
  491. * netfs, and so must be accessed with object lock held.
  492. */
  493. void fscache_obtained_object(struct fscache_object *object)
  494. {
  495. struct fscache_cookie *cookie = object->cookie;
  496. _enter("{OBJ%x,%s}",
  497. object->debug_id, fscache_object_states[object->state]);
  498. /* if we were still looking up, then we must have a positive lookup
  499. * result, in which case there may be data available */
  500. spin_lock(&object->lock);
  501. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  502. fscache_stat(&fscache_n_object_lookups_positive);
  503. clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  504. object->state = FSCACHE_OBJECT_AVAILABLE;
  505. spin_unlock(&object->lock);
  506. smp_mb__before_clear_bit();
  507. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  508. smp_mb__after_clear_bit();
  509. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  510. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  511. } else {
  512. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  513. fscache_stat(&fscache_n_object_created);
  514. object->state = FSCACHE_OBJECT_AVAILABLE;
  515. spin_unlock(&object->lock);
  516. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  517. smp_wmb();
  518. }
  519. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
  520. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
  521. _leave("");
  522. }
  523. EXPORT_SYMBOL(fscache_obtained_object);
  524. /*
  525. * handle an object that has just become available
  526. */
  527. static void fscache_object_available(struct fscache_object *object)
  528. {
  529. _enter("{OBJ%x}", object->debug_id);
  530. spin_lock(&object->lock);
  531. if (object->cookie &&
  532. test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
  533. wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
  534. fscache_done_parent_op(object);
  535. if (object->n_in_progress == 0) {
  536. if (object->n_ops > 0) {
  537. ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
  538. fscache_start_operations(object);
  539. } else {
  540. ASSERT(list_empty(&object->pending_ops));
  541. }
  542. }
  543. spin_unlock(&object->lock);
  544. fscache_stat(&fscache_n_cop_lookup_complete);
  545. object->cache->ops->lookup_complete(object);
  546. fscache_stat_d(&fscache_n_cop_lookup_complete);
  547. fscache_enqueue_dependents(object);
  548. fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
  549. fscache_stat(&fscache_n_object_avail);
  550. _leave("");
  551. }
  552. /*
  553. * drop an object's attachments
  554. */
  555. static void fscache_drop_object(struct fscache_object *object)
  556. {
  557. struct fscache_object *parent = object->parent;
  558. struct fscache_cache *cache = object->cache;
  559. _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
  560. ASSERTCMP(object->cookie, ==, NULL);
  561. ASSERT(hlist_unhashed(&object->cookie_link));
  562. spin_lock(&cache->object_list_lock);
  563. list_del_init(&object->cache_link);
  564. spin_unlock(&cache->object_list_lock);
  565. fscache_stat(&fscache_n_cop_drop_object);
  566. cache->ops->drop_object(object);
  567. fscache_stat_d(&fscache_n_cop_drop_object);
  568. if (parent) {
  569. _debug("release parent OBJ%x {%d}",
  570. parent->debug_id, parent->n_children);
  571. spin_lock(&parent->lock);
  572. parent->n_children--;
  573. if (parent->n_children == 0)
  574. fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
  575. spin_unlock(&parent->lock);
  576. object->parent = NULL;
  577. }
  578. /* this just shifts the object release to the work processor */
  579. fscache_put_object(object);
  580. _leave("");
  581. }
  582. /*
  583. * release or recycle an object that the netfs has discarded
  584. */
  585. static void fscache_release_object(struct fscache_object *object)
  586. {
  587. _enter("");
  588. fscache_drop_object(object);
  589. }
  590. /*
  591. * withdraw an object from active service
  592. */
  593. static void fscache_withdraw_object(struct fscache_object *object)
  594. {
  595. struct fscache_cookie *cookie;
  596. bool detached;
  597. _enter("");
  598. spin_lock(&object->lock);
  599. cookie = object->cookie;
  600. if (cookie) {
  601. /* need to get the cookie lock before the object lock, starting
  602. * from the object pointer */
  603. atomic_inc(&cookie->usage);
  604. spin_unlock(&object->lock);
  605. detached = false;
  606. spin_lock(&cookie->lock);
  607. spin_lock(&object->lock);
  608. if (object->cookie == cookie) {
  609. hlist_del_init(&object->cookie_link);
  610. object->cookie = NULL;
  611. fscache_invalidation_complete(cookie);
  612. detached = true;
  613. }
  614. spin_unlock(&cookie->lock);
  615. fscache_cookie_put(cookie);
  616. if (detached)
  617. fscache_cookie_put(cookie);
  618. }
  619. spin_unlock(&object->lock);
  620. fscache_drop_object(object);
  621. }
  622. /*
  623. * withdraw an object from active service at the behest of the cache
  624. * - need break the links to a cached object cookie
  625. * - called under two situations:
  626. * (1) recycler decides to reclaim an in-use object
  627. * (2) a cache is unmounted
  628. * - have to take care as the cookie can be being relinquished by the netfs
  629. * simultaneously
  630. * - the object is pinned by the caller holding a refcount on it
  631. */
  632. void fscache_withdrawing_object(struct fscache_cache *cache,
  633. struct fscache_object *object)
  634. {
  635. bool enqueue = false;
  636. _enter(",OBJ%x", object->debug_id);
  637. spin_lock(&object->lock);
  638. if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
  639. object->state = FSCACHE_OBJECT_WITHDRAWING;
  640. enqueue = true;
  641. }
  642. spin_unlock(&object->lock);
  643. if (enqueue)
  644. fscache_enqueue_object(object);
  645. _leave("");
  646. }
  647. /*
  648. * get a ref on an object
  649. */
  650. static int fscache_get_object(struct fscache_object *object)
  651. {
  652. int ret;
  653. fscache_stat(&fscache_n_cop_grab_object);
  654. ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
  655. fscache_stat_d(&fscache_n_cop_grab_object);
  656. return ret;
  657. }
  658. /*
  659. * discard a ref on a work item
  660. */
  661. static void fscache_put_object(struct fscache_object *object)
  662. {
  663. fscache_stat(&fscache_n_cop_put_object);
  664. object->cache->ops->put_object(object);
  665. fscache_stat_d(&fscache_n_cop_put_object);
  666. }
  667. /*
  668. * enqueue an object for metadata-type processing
  669. */
  670. void fscache_enqueue_object(struct fscache_object *object)
  671. {
  672. _enter("{OBJ%x}", object->debug_id);
  673. if (fscache_get_object(object) >= 0) {
  674. wait_queue_head_t *cong_wq =
  675. &get_cpu_var(fscache_object_cong_wait);
  676. if (queue_work(fscache_object_wq, &object->work)) {
  677. if (fscache_object_congested())
  678. wake_up(cong_wq);
  679. } else
  680. fscache_put_object(object);
  681. put_cpu_var(fscache_object_cong_wait);
  682. }
  683. }
  684. /**
  685. * fscache_object_sleep_till_congested - Sleep until object wq is congested
  686. * @timoutp: Scheduler sleep timeout
  687. *
  688. * Allow an object handler to sleep until the object workqueue is congested.
  689. *
  690. * The caller must set up a wake up event before calling this and must have set
  691. * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
  692. * condition before calling this function as no test is made here.
  693. *
  694. * %true is returned if the object wq is congested, %false otherwise.
  695. */
  696. bool fscache_object_sleep_till_congested(signed long *timeoutp)
  697. {
  698. wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
  699. DEFINE_WAIT(wait);
  700. if (fscache_object_congested())
  701. return true;
  702. add_wait_queue_exclusive(cong_wq, &wait);
  703. if (!fscache_object_congested())
  704. *timeoutp = schedule_timeout(*timeoutp);
  705. finish_wait(cong_wq, &wait);
  706. return fscache_object_congested();
  707. }
  708. EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
  709. /*
  710. * enqueue the dependents of an object for metadata-type processing
  711. * - the caller must hold the object's lock
  712. * - this may cause an already locked object to wind up being processed again
  713. */
  714. static void fscache_enqueue_dependents(struct fscache_object *object)
  715. {
  716. struct fscache_object *dep;
  717. _enter("{OBJ%x}", object->debug_id);
  718. if (list_empty(&object->dependents))
  719. return;
  720. spin_lock(&object->lock);
  721. while (!list_empty(&object->dependents)) {
  722. dep = list_entry(object->dependents.next,
  723. struct fscache_object, dep_link);
  724. list_del_init(&dep->dep_link);
  725. /* sort onto appropriate lists */
  726. fscache_enqueue_object(dep);
  727. fscache_put_object(dep);
  728. if (!list_empty(&object->dependents))
  729. cond_resched_lock(&object->lock);
  730. }
  731. spin_unlock(&object->lock);
  732. }
  733. /*
  734. * remove an object from whatever queue it's waiting on
  735. * - the caller must hold object->lock
  736. */
  737. void fscache_dequeue_object(struct fscache_object *object)
  738. {
  739. _enter("{OBJ%x}", object->debug_id);
  740. if (!list_empty(&object->dep_link)) {
  741. spin_lock(&object->parent->lock);
  742. list_del_init(&object->dep_link);
  743. spin_unlock(&object->parent->lock);
  744. }
  745. _leave("");
  746. }
  747. /**
  748. * fscache_check_aux - Ask the netfs whether an object on disk is still valid
  749. * @object: The object to ask about
  750. * @data: The auxiliary data for the object
  751. * @datalen: The size of the auxiliary data
  752. *
  753. * This function consults the netfs about the coherency state of an object
  754. */
  755. enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  756. const void *data, uint16_t datalen)
  757. {
  758. enum fscache_checkaux result;
  759. if (!object->cookie->def->check_aux) {
  760. fscache_stat(&fscache_n_checkaux_none);
  761. return FSCACHE_CHECKAUX_OKAY;
  762. }
  763. result = object->cookie->def->check_aux(object->cookie->netfs_data,
  764. data, datalen);
  765. switch (result) {
  766. /* entry okay as is */
  767. case FSCACHE_CHECKAUX_OKAY:
  768. fscache_stat(&fscache_n_checkaux_okay);
  769. break;
  770. /* entry requires update */
  771. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  772. fscache_stat(&fscache_n_checkaux_update);
  773. break;
  774. /* entry requires deletion */
  775. case FSCACHE_CHECKAUX_OBSOLETE:
  776. fscache_stat(&fscache_n_checkaux_obsolete);
  777. break;
  778. default:
  779. BUG();
  780. }
  781. return result;
  782. }
  783. EXPORT_SYMBOL(fscache_check_aux);
  784. /*
  785. * Asynchronously invalidate an object.
  786. */
  787. static void fscache_invalidate_object(struct fscache_object *object)
  788. {
  789. struct fscache_operation *op;
  790. struct fscache_cookie *cookie = object->cookie;
  791. _enter("{OBJ%x}", object->debug_id);
  792. /* Reject any new read/write ops and abort any that are pending. */
  793. fscache_invalidate_writes(cookie);
  794. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  795. fscache_cancel_all_ops(object);
  796. /* Now we have to wait for in-progress reads and writes */
  797. op = kzalloc(sizeof(*op), GFP_KERNEL);
  798. if (!op) {
  799. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  800. _leave(" [ENOMEM]");
  801. return;
  802. }
  803. fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
  804. op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
  805. spin_lock(&cookie->lock);
  806. if (fscache_submit_exclusive_op(object, op) < 0)
  807. BUG();
  808. spin_unlock(&cookie->lock);
  809. fscache_put_operation(op);
  810. /* Once we've completed the invalidation, we know there will be no data
  811. * stored in the cache and thus we can reinstate the data-check-skip
  812. * optimisation.
  813. */
  814. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  815. /* We can allow read and write requests to come in once again. They'll
  816. * queue up behind our exclusive invalidation operation.
  817. */
  818. fscache_invalidation_complete(cookie);
  819. _leave("");
  820. }