object.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  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. static 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. /**
  345. * fscache_object_init - Initialise a cache object description
  346. * @object: Object description
  347. * @cookie: Cookie object will be attached to
  348. * @cache: Cache in which backing object will be found
  349. *
  350. * Initialise a cache object description to its basic values.
  351. *
  352. * See Documentation/filesystems/caching/backend-api.txt for a complete
  353. * description.
  354. */
  355. void fscache_object_init(struct fscache_object *object,
  356. struct fscache_cookie *cookie,
  357. struct fscache_cache *cache)
  358. {
  359. atomic_inc(&cache->object_count);
  360. object->state = FSCACHE_OBJECT_INIT;
  361. spin_lock_init(&object->lock);
  362. INIT_LIST_HEAD(&object->cache_link);
  363. INIT_HLIST_NODE(&object->cookie_link);
  364. INIT_WORK(&object->work, fscache_object_work_func);
  365. INIT_LIST_HEAD(&object->dependents);
  366. INIT_LIST_HEAD(&object->dep_link);
  367. INIT_LIST_HEAD(&object->pending_ops);
  368. object->n_children = 0;
  369. object->n_ops = object->n_in_progress = object->n_exclusive = 0;
  370. object->events = object->event_mask = 0;
  371. object->flags = 0;
  372. object->store_limit = 0;
  373. object->store_limit_l = 0;
  374. object->cache = cache;
  375. object->cookie = cookie;
  376. object->parent = NULL;
  377. }
  378. EXPORT_SYMBOL(fscache_object_init);
  379. /*
  380. * initialise an object
  381. * - check the specified object's parent to see if we can make use of it
  382. * immediately to do a creation
  383. * - we may need to start the process of creating a parent and we need to wait
  384. * for the parent's lookup and creation to complete if it's not there yet
  385. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  386. * leaf-most cookies of the object and all its children
  387. */
  388. static void fscache_initialise_object(struct fscache_object *object)
  389. {
  390. struct fscache_object *parent;
  391. _enter("");
  392. ASSERT(object->cookie != NULL);
  393. ASSERT(object->cookie->parent != NULL);
  394. if (object->events & ((1 << FSCACHE_OBJECT_EV_ERROR) |
  395. (1 << FSCACHE_OBJECT_EV_RELEASE) |
  396. (1 << FSCACHE_OBJECT_EV_RETIRE) |
  397. (1 << FSCACHE_OBJECT_EV_WITHDRAW))) {
  398. _debug("abort init %lx", object->events);
  399. spin_lock(&object->lock);
  400. object->state = FSCACHE_OBJECT_ABORT_INIT;
  401. spin_unlock(&object->lock);
  402. return;
  403. }
  404. spin_lock(&object->cookie->lock);
  405. spin_lock_nested(&object->cookie->parent->lock, 1);
  406. parent = object->parent;
  407. if (!parent) {
  408. _debug("no parent");
  409. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  410. } else {
  411. spin_lock(&object->lock);
  412. spin_lock_nested(&parent->lock, 1);
  413. _debug("parent %s", fscache_object_states[parent->state]);
  414. if (fscache_object_is_dying(parent)) {
  415. _debug("bad parent");
  416. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  417. } else if (!fscache_object_is_available(parent)) {
  418. _debug("wait");
  419. /* we may get woken up in this state by child objects
  420. * binding on to us, so we need to make sure we don't
  421. * add ourself to the list multiple times */
  422. if (list_empty(&object->dep_link)) {
  423. fscache_stat(&fscache_n_cop_grab_object);
  424. object->cache->ops->grab_object(object);
  425. fscache_stat_d(&fscache_n_cop_grab_object);
  426. list_add(&object->dep_link,
  427. &parent->dependents);
  428. /* fscache_acquire_non_index_cookie() uses this
  429. * to wake the chain up */
  430. if (parent->state == FSCACHE_OBJECT_INIT)
  431. fscache_enqueue_object(parent);
  432. }
  433. } else {
  434. _debug("go");
  435. parent->n_ops++;
  436. parent->n_obj_ops++;
  437. object->lookup_jif = jiffies;
  438. object->state = FSCACHE_OBJECT_LOOKING_UP;
  439. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  440. }
  441. spin_unlock(&parent->lock);
  442. spin_unlock(&object->lock);
  443. }
  444. spin_unlock(&object->cookie->parent->lock);
  445. spin_unlock(&object->cookie->lock);
  446. _leave("");
  447. }
  448. /*
  449. * look an object up in the cache from which it was allocated
  450. * - we hold an "access lock" on the parent object, so the parent object cannot
  451. * be withdrawn by either party till we've finished
  452. * - an object's cookie is pinned until we clear FSCACHE_COOKIE_CREATING on the
  453. * leaf-most cookies of the object and all its children
  454. */
  455. static void fscache_lookup_object(struct fscache_object *object)
  456. {
  457. struct fscache_cookie *cookie = object->cookie;
  458. struct fscache_object *parent;
  459. int ret;
  460. _enter("");
  461. parent = object->parent;
  462. ASSERT(parent != NULL);
  463. ASSERTCMP(parent->n_ops, >, 0);
  464. ASSERTCMP(parent->n_obj_ops, >, 0);
  465. /* make sure the parent is still available */
  466. ASSERT(fscache_object_is_available(parent));
  467. if (fscache_object_is_dying(parent) ||
  468. test_bit(FSCACHE_IOERROR, &object->cache->flags)) {
  469. _debug("unavailable");
  470. set_bit(FSCACHE_OBJECT_EV_WITHDRAW, &object->events);
  471. _leave("");
  472. return;
  473. }
  474. _debug("LOOKUP \"%s/%s\" in \"%s\"",
  475. parent->cookie->def->name, cookie->def->name,
  476. object->cache->tag->name);
  477. fscache_stat(&fscache_n_object_lookups);
  478. fscache_stat(&fscache_n_cop_lookup_object);
  479. ret = object->cache->ops->lookup_object(object);
  480. fscache_stat_d(&fscache_n_cop_lookup_object);
  481. if (test_bit(FSCACHE_OBJECT_EV_ERROR, &object->events))
  482. set_bit(FSCACHE_COOKIE_UNAVAILABLE, &cookie->flags);
  483. if (ret == -ETIMEDOUT) {
  484. /* probably stuck behind another object, so move this one to
  485. * the back of the queue */
  486. fscache_stat(&fscache_n_object_lookups_timed_out);
  487. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  488. }
  489. _leave("");
  490. }
  491. /**
  492. * fscache_object_lookup_negative - Note negative cookie lookup
  493. * @object: Object pointing to cookie to mark
  494. *
  495. * Note negative lookup, permitting those waiting to read data from an already
  496. * existing backing object to continue as there's no data for them to read.
  497. */
  498. void fscache_object_lookup_negative(struct fscache_object *object)
  499. {
  500. struct fscache_cookie *cookie = object->cookie;
  501. _enter("{OBJ%x,%s}",
  502. object->debug_id, fscache_object_states[object->state]);
  503. spin_lock(&object->lock);
  504. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  505. fscache_stat(&fscache_n_object_lookups_negative);
  506. /* transit here to allow write requests to begin stacking up
  507. * and read requests to begin returning ENODATA */
  508. object->state = FSCACHE_OBJECT_CREATING;
  509. spin_unlock(&object->lock);
  510. set_bit(FSCACHE_COOKIE_PENDING_FILL, &cookie->flags);
  511. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  512. _debug("wake up lookup %p", &cookie->flags);
  513. smp_mb__before_clear_bit();
  514. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  515. smp_mb__after_clear_bit();
  516. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  517. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  518. } else {
  519. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  520. spin_unlock(&object->lock);
  521. }
  522. _leave("");
  523. }
  524. EXPORT_SYMBOL(fscache_object_lookup_negative);
  525. /**
  526. * fscache_obtained_object - Note successful object lookup or creation
  527. * @object: Object pointing to cookie to mark
  528. *
  529. * Note successful lookup and/or creation, permitting those waiting to write
  530. * data to a backing object to continue.
  531. *
  532. * Note that after calling this, an object's cookie may be relinquished by the
  533. * netfs, and so must be accessed with object lock held.
  534. */
  535. void fscache_obtained_object(struct fscache_object *object)
  536. {
  537. struct fscache_cookie *cookie = object->cookie;
  538. _enter("{OBJ%x,%s}",
  539. object->debug_id, fscache_object_states[object->state]);
  540. /* if we were still looking up, then we must have a positive lookup
  541. * result, in which case there may be data available */
  542. spin_lock(&object->lock);
  543. if (object->state == FSCACHE_OBJECT_LOOKING_UP) {
  544. fscache_stat(&fscache_n_object_lookups_positive);
  545. clear_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  546. object->state = FSCACHE_OBJECT_AVAILABLE;
  547. spin_unlock(&object->lock);
  548. smp_mb__before_clear_bit();
  549. clear_bit(FSCACHE_COOKIE_LOOKING_UP, &cookie->flags);
  550. smp_mb__after_clear_bit();
  551. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_LOOKING_UP);
  552. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  553. } else {
  554. ASSERTCMP(object->state, ==, FSCACHE_OBJECT_CREATING);
  555. fscache_stat(&fscache_n_object_created);
  556. object->state = FSCACHE_OBJECT_AVAILABLE;
  557. spin_unlock(&object->lock);
  558. set_bit(FSCACHE_OBJECT_EV_REQUEUE, &object->events);
  559. smp_wmb();
  560. }
  561. if (test_and_clear_bit(FSCACHE_COOKIE_CREATING, &cookie->flags))
  562. wake_up_bit(&cookie->flags, FSCACHE_COOKIE_CREATING);
  563. _leave("");
  564. }
  565. EXPORT_SYMBOL(fscache_obtained_object);
  566. /*
  567. * handle an object that has just become available
  568. */
  569. static void fscache_object_available(struct fscache_object *object)
  570. {
  571. _enter("{OBJ%x}", object->debug_id);
  572. spin_lock(&object->lock);
  573. if (object->cookie &&
  574. test_and_clear_bit(FSCACHE_COOKIE_CREATING, &object->cookie->flags))
  575. wake_up_bit(&object->cookie->flags, FSCACHE_COOKIE_CREATING);
  576. fscache_done_parent_op(object);
  577. if (object->n_in_progress == 0) {
  578. if (object->n_ops > 0) {
  579. ASSERTCMP(object->n_ops, >=, object->n_obj_ops);
  580. fscache_start_operations(object);
  581. } else {
  582. ASSERT(list_empty(&object->pending_ops));
  583. }
  584. }
  585. spin_unlock(&object->lock);
  586. fscache_stat(&fscache_n_cop_lookup_complete);
  587. object->cache->ops->lookup_complete(object);
  588. fscache_stat_d(&fscache_n_cop_lookup_complete);
  589. fscache_enqueue_dependents(object);
  590. fscache_hist(fscache_obj_instantiate_histogram, object->lookup_jif);
  591. fscache_stat(&fscache_n_object_avail);
  592. _leave("");
  593. }
  594. /*
  595. * drop an object's attachments
  596. */
  597. static void fscache_drop_object(struct fscache_object *object)
  598. {
  599. struct fscache_object *parent = object->parent;
  600. struct fscache_cache *cache = object->cache;
  601. _enter("{OBJ%x,%d}", object->debug_id, object->n_children);
  602. ASSERTCMP(object->cookie, ==, NULL);
  603. ASSERT(hlist_unhashed(&object->cookie_link));
  604. spin_lock(&cache->object_list_lock);
  605. list_del_init(&object->cache_link);
  606. spin_unlock(&cache->object_list_lock);
  607. fscache_stat(&fscache_n_cop_drop_object);
  608. cache->ops->drop_object(object);
  609. fscache_stat_d(&fscache_n_cop_drop_object);
  610. if (parent) {
  611. _debug("release parent OBJ%x {%d}",
  612. parent->debug_id, parent->n_children);
  613. spin_lock(&parent->lock);
  614. parent->n_children--;
  615. if (parent->n_children == 0)
  616. fscache_raise_event(parent, FSCACHE_OBJECT_EV_CLEARED);
  617. spin_unlock(&parent->lock);
  618. object->parent = NULL;
  619. }
  620. /* this just shifts the object release to the work processor */
  621. fscache_put_object(object);
  622. _leave("");
  623. }
  624. /*
  625. * release or recycle an object that the netfs has discarded
  626. */
  627. static void fscache_release_object(struct fscache_object *object)
  628. {
  629. _enter("");
  630. fscache_drop_object(object);
  631. }
  632. /*
  633. * withdraw an object from active service
  634. */
  635. static void fscache_withdraw_object(struct fscache_object *object)
  636. {
  637. struct fscache_cookie *cookie;
  638. bool detached;
  639. _enter("");
  640. spin_lock(&object->lock);
  641. cookie = object->cookie;
  642. if (cookie) {
  643. /* need to get the cookie lock before the object lock, starting
  644. * from the object pointer */
  645. atomic_inc(&cookie->usage);
  646. spin_unlock(&object->lock);
  647. detached = false;
  648. spin_lock(&cookie->lock);
  649. spin_lock(&object->lock);
  650. if (object->cookie == cookie) {
  651. hlist_del_init(&object->cookie_link);
  652. object->cookie = NULL;
  653. fscache_invalidation_complete(cookie);
  654. detached = true;
  655. }
  656. spin_unlock(&cookie->lock);
  657. fscache_cookie_put(cookie);
  658. if (detached)
  659. fscache_cookie_put(cookie);
  660. }
  661. spin_unlock(&object->lock);
  662. fscache_drop_object(object);
  663. }
  664. /*
  665. * withdraw an object from active service at the behest of the cache
  666. * - need break the links to a cached object cookie
  667. * - called under two situations:
  668. * (1) recycler decides to reclaim an in-use object
  669. * (2) a cache is unmounted
  670. * - have to take care as the cookie can be being relinquished by the netfs
  671. * simultaneously
  672. * - the object is pinned by the caller holding a refcount on it
  673. */
  674. void fscache_withdrawing_object(struct fscache_cache *cache,
  675. struct fscache_object *object)
  676. {
  677. bool enqueue = false;
  678. _enter(",OBJ%x", object->debug_id);
  679. spin_lock(&object->lock);
  680. if (object->state < FSCACHE_OBJECT_WITHDRAWING) {
  681. object->state = FSCACHE_OBJECT_WITHDRAWING;
  682. enqueue = true;
  683. }
  684. spin_unlock(&object->lock);
  685. if (enqueue)
  686. fscache_enqueue_object(object);
  687. _leave("");
  688. }
  689. /*
  690. * get a ref on an object
  691. */
  692. static int fscache_get_object(struct fscache_object *object)
  693. {
  694. int ret;
  695. fscache_stat(&fscache_n_cop_grab_object);
  696. ret = object->cache->ops->grab_object(object) ? 0 : -EAGAIN;
  697. fscache_stat_d(&fscache_n_cop_grab_object);
  698. return ret;
  699. }
  700. /*
  701. * discard a ref on a work item
  702. */
  703. static void fscache_put_object(struct fscache_object *object)
  704. {
  705. fscache_stat(&fscache_n_cop_put_object);
  706. object->cache->ops->put_object(object);
  707. fscache_stat_d(&fscache_n_cop_put_object);
  708. }
  709. /*
  710. * enqueue an object for metadata-type processing
  711. */
  712. void fscache_enqueue_object(struct fscache_object *object)
  713. {
  714. _enter("{OBJ%x}", object->debug_id);
  715. if (fscache_get_object(object) >= 0) {
  716. wait_queue_head_t *cong_wq =
  717. &get_cpu_var(fscache_object_cong_wait);
  718. if (queue_work(fscache_object_wq, &object->work)) {
  719. if (fscache_object_congested())
  720. wake_up(cong_wq);
  721. } else
  722. fscache_put_object(object);
  723. put_cpu_var(fscache_object_cong_wait);
  724. }
  725. }
  726. /**
  727. * fscache_object_sleep_till_congested - Sleep until object wq is congested
  728. * @timoutp: Scheduler sleep timeout
  729. *
  730. * Allow an object handler to sleep until the object workqueue is congested.
  731. *
  732. * The caller must set up a wake up event before calling this and must have set
  733. * the appropriate sleep mode (such as TASK_UNINTERRUPTIBLE) and tested its own
  734. * condition before calling this function as no test is made here.
  735. *
  736. * %true is returned if the object wq is congested, %false otherwise.
  737. */
  738. bool fscache_object_sleep_till_congested(signed long *timeoutp)
  739. {
  740. wait_queue_head_t *cong_wq = &__get_cpu_var(fscache_object_cong_wait);
  741. DEFINE_WAIT(wait);
  742. if (fscache_object_congested())
  743. return true;
  744. add_wait_queue_exclusive(cong_wq, &wait);
  745. if (!fscache_object_congested())
  746. *timeoutp = schedule_timeout(*timeoutp);
  747. finish_wait(cong_wq, &wait);
  748. return fscache_object_congested();
  749. }
  750. EXPORT_SYMBOL_GPL(fscache_object_sleep_till_congested);
  751. /*
  752. * enqueue the dependents of an object for metadata-type processing
  753. * - the caller must hold the object's lock
  754. * - this may cause an already locked object to wind up being processed again
  755. */
  756. static void fscache_enqueue_dependents(struct fscache_object *object)
  757. {
  758. struct fscache_object *dep;
  759. _enter("{OBJ%x}", object->debug_id);
  760. if (list_empty(&object->dependents))
  761. return;
  762. spin_lock(&object->lock);
  763. while (!list_empty(&object->dependents)) {
  764. dep = list_entry(object->dependents.next,
  765. struct fscache_object, dep_link);
  766. list_del_init(&dep->dep_link);
  767. /* sort onto appropriate lists */
  768. fscache_enqueue_object(dep);
  769. fscache_put_object(dep);
  770. if (!list_empty(&object->dependents))
  771. cond_resched_lock(&object->lock);
  772. }
  773. spin_unlock(&object->lock);
  774. }
  775. /*
  776. * remove an object from whatever queue it's waiting on
  777. * - the caller must hold object->lock
  778. */
  779. void fscache_dequeue_object(struct fscache_object *object)
  780. {
  781. _enter("{OBJ%x}", object->debug_id);
  782. if (!list_empty(&object->dep_link)) {
  783. spin_lock(&object->parent->lock);
  784. list_del_init(&object->dep_link);
  785. spin_unlock(&object->parent->lock);
  786. }
  787. _leave("");
  788. }
  789. /**
  790. * fscache_check_aux - Ask the netfs whether an object on disk is still valid
  791. * @object: The object to ask about
  792. * @data: The auxiliary data for the object
  793. * @datalen: The size of the auxiliary data
  794. *
  795. * This function consults the netfs about the coherency state of an object
  796. */
  797. enum fscache_checkaux fscache_check_aux(struct fscache_object *object,
  798. const void *data, uint16_t datalen)
  799. {
  800. enum fscache_checkaux result;
  801. if (!object->cookie->def->check_aux) {
  802. fscache_stat(&fscache_n_checkaux_none);
  803. return FSCACHE_CHECKAUX_OKAY;
  804. }
  805. result = object->cookie->def->check_aux(object->cookie->netfs_data,
  806. data, datalen);
  807. switch (result) {
  808. /* entry okay as is */
  809. case FSCACHE_CHECKAUX_OKAY:
  810. fscache_stat(&fscache_n_checkaux_okay);
  811. break;
  812. /* entry requires update */
  813. case FSCACHE_CHECKAUX_NEEDS_UPDATE:
  814. fscache_stat(&fscache_n_checkaux_update);
  815. break;
  816. /* entry requires deletion */
  817. case FSCACHE_CHECKAUX_OBSOLETE:
  818. fscache_stat(&fscache_n_checkaux_obsolete);
  819. break;
  820. default:
  821. BUG();
  822. }
  823. return result;
  824. }
  825. EXPORT_SYMBOL(fscache_check_aux);
  826. /*
  827. * Asynchronously invalidate an object.
  828. */
  829. static void fscache_invalidate_object(struct fscache_object *object)
  830. {
  831. struct fscache_operation *op;
  832. struct fscache_cookie *cookie = object->cookie;
  833. _enter("{OBJ%x}", object->debug_id);
  834. /* Reject any new read/write ops and abort any that are pending. */
  835. fscache_invalidate_writes(cookie);
  836. clear_bit(FSCACHE_OBJECT_PENDING_WRITE, &object->flags);
  837. fscache_cancel_all_ops(object);
  838. /* Now we have to wait for in-progress reads and writes */
  839. op = kzalloc(sizeof(*op), GFP_KERNEL);
  840. if (!op) {
  841. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  842. _leave(" [ENOMEM]");
  843. return;
  844. }
  845. fscache_operation_init(op, object->cache->ops->invalidate_object, NULL);
  846. op->flags = FSCACHE_OP_ASYNC | (1 << FSCACHE_OP_EXCLUSIVE);
  847. spin_lock(&cookie->lock);
  848. if (fscache_submit_exclusive_op(object, op) < 0)
  849. goto submit_op_failed;
  850. spin_unlock(&cookie->lock);
  851. fscache_put_operation(op);
  852. /* Once we've completed the invalidation, we know there will be no data
  853. * stored in the cache and thus we can reinstate the data-check-skip
  854. * optimisation.
  855. */
  856. set_bit(FSCACHE_COOKIE_NO_DATA_YET, &cookie->flags);
  857. /* We can allow read and write requests to come in once again. They'll
  858. * queue up behind our exclusive invalidation operation.
  859. */
  860. fscache_invalidation_complete(cookie);
  861. _leave("");
  862. return;
  863. submit_op_failed:
  864. spin_unlock(&cookie->lock);
  865. kfree(op);
  866. fscache_raise_event(object, FSCACHE_OBJECT_EV_ERROR);
  867. _leave(" [EIO]");
  868. }