object.c 28 KB

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