object.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. ====================================================
  2. IN-KERNEL CACHE OBJECT REPRESENTATION AND MANAGEMENT
  3. ====================================================
  4. By: David Howells <dhowells@redhat.com>
  5. Contents:
  6. (*) Representation
  7. (*) Object management state machine.
  8. - Provision of cpu time.
  9. - Locking simplification.
  10. (*) The set of states.
  11. (*) The set of events.
  12. ==============
  13. REPRESENTATION
  14. ==============
  15. FS-Cache maintains an in-kernel representation of each object that a netfs is
  16. currently interested in. Such objects are represented by the fscache_cookie
  17. struct and are referred to as cookies.
  18. FS-Cache also maintains a separate in-kernel representation of the objects that
  19. a cache backend is currently actively caching. Such objects are represented by
  20. the fscache_object struct. The cache backends allocate these upon request, and
  21. are expected to embed them in their own representations. These are referred to
  22. as objects.
  23. There is a 1:N relationship between cookies and objects. A cookie may be
  24. represented by multiple objects - an index may exist in more than one cache -
  25. or even by no objects (it may not be cached).
  26. Furthermore, both cookies and objects are hierarchical. The two hierarchies
  27. correspond, but the cookies tree is a superset of the union of the object trees
  28. of multiple caches:
  29. NETFS INDEX TREE : CACHE 1 : CACHE 2
  30. : :
  31. : +-----------+ :
  32. +----------->| IObject | :
  33. +-----------+ | : +-----------+ :
  34. | ICookie |-------+ : | :
  35. +-----------+ | : | : +-----------+
  36. | +------------------------------>| IObject |
  37. | : | : +-----------+
  38. | : V : |
  39. | : +-----------+ : |
  40. V +----------->| IObject | : |
  41. +-----------+ | : +-----------+ : |
  42. | ICookie |-------+ : | : V
  43. +-----------+ | : | : +-----------+
  44. | +------------------------------>| IObject |
  45. +-----+-----+ : | : +-----------+
  46. | | : | : |
  47. V | : V : |
  48. +-----------+ | : +-----------+ : |
  49. | ICookie |------------------------->| IObject | : |
  50. +-----------+ | : +-----------+ : |
  51. | V : | : V
  52. | +-----------+ : | : +-----------+
  53. | | ICookie |-------------------------------->| IObject |
  54. | +-----------+ : | : +-----------+
  55. V | : V : |
  56. +-----------+ | : +-----------+ : |
  57. | DCookie |------------------------->| DObject | : |
  58. +-----------+ | : +-----------+ : |
  59. | : : |
  60. +-------+-------+ : : |
  61. | | : : |
  62. V V : : V
  63. +-----------+ +-----------+ : : +-----------+
  64. | DCookie | | DCookie |------------------------>| DObject |
  65. +-----------+ +-----------+ : : +-----------+
  66. : :
  67. In the above illustration, ICookie and IObject represent indices and DCookie
  68. and DObject represent data storage objects. Indices may have representation in
  69. multiple caches, but currently, non-index objects may not. Objects of any type
  70. may also be entirely unrepresented.
  71. As far as the netfs API goes, the netfs is only actually permitted to see
  72. pointers to the cookies. The cookies themselves and any objects attached to
  73. those cookies are hidden from it.
  74. ===============================
  75. OBJECT MANAGEMENT STATE MACHINE
  76. ===============================
  77. Within FS-Cache, each active object is managed by its own individual state
  78. machine. The state for an object is kept in the fscache_object struct, in
  79. object->state. A cookie may point to a set of objects that are in different
  80. states.
  81. Each state has an action associated with it that is invoked when the machine
  82. wakes up in that state. There are four logical sets of states:
  83. (1) Preparation: states that wait for the parent objects to become ready. The
  84. representations are hierarchical, and it is expected that an object must
  85. be created or accessed with respect to its parent object.
  86. (2) Initialisation: states that perform lookups in the cache and validate
  87. what's found and that create on disk any missing metadata.
  88. (3) Normal running: states that allow netfs operations on objects to proceed
  89. and that update the state of objects.
  90. (4) Termination: states that detach objects from their netfs cookies, that
  91. delete objects from disk, that handle disk and system errors and that free
  92. up in-memory resources.
  93. In most cases, transitioning between states is in response to signalled events.
  94. When a state has finished processing, it will usually set the mask of events in
  95. which it is interested (object->event_mask) and relinquish the worker thread.
  96. Then when an event is raised (by calling fscache_raise_event()), if the event
  97. is not masked, the object will be queued for processing (by calling
  98. fscache_enqueue_object()).
  99. PROVISION OF CPU TIME
  100. ---------------------
  101. The work to be done by the various states was given CPU time by the threads of
  102. the slow work facility. This was used in preference to the workqueue facility
  103. because:
  104. (1) Threads may be completely occupied for very long periods of time by a
  105. particular work item. These state actions may be doing sequences of
  106. synchronous, journalled disk accesses (lookup, mkdir, create, setxattr,
  107. getxattr, truncate, unlink, rmdir, rename).
  108. (2) Threads may do little actual work, but may rather spend a lot of time
  109. sleeping on I/O. This means that single-threaded and 1-per-CPU-threaded
  110. workqueues don't necessarily have the right numbers of threads.
  111. LOCKING SIMPLIFICATION
  112. ----------------------
  113. Because only one worker thread may be operating on any particular object's
  114. state machine at once, this simplifies the locking, particularly with respect
  115. to disconnecting the netfs's representation of a cache object (fscache_cookie)
  116. from the cache backend's representation (fscache_object) - which may be
  117. requested from either end.
  118. =================
  119. THE SET OF STATES
  120. =================
  121. The object state machine has a set of states that it can be in. There are
  122. preparation states in which the object sets itself up and waits for its parent
  123. object to transit to a state that allows access to its children:
  124. (1) State FSCACHE_OBJECT_INIT.
  125. Initialise the object and wait for the parent object to become active. In
  126. the cache, it is expected that it will not be possible to look an object
  127. up from the parent object, until that parent object itself has been looked
  128. up.
  129. There are initialisation states in which the object sets itself up and accesses
  130. disk for the object metadata:
  131. (2) State FSCACHE_OBJECT_LOOKING_UP.
  132. Look up the object on disk, using the parent as a starting point.
  133. FS-Cache expects the cache backend to probe the cache to see whether this
  134. object is represented there, and if it is, to see if it's valid (coherency
  135. management).
  136. The cache should call fscache_object_lookup_negative() to indicate lookup
  137. failure for whatever reason, and should call fscache_obtained_object() to
  138. indicate success.
  139. At the completion of lookup, FS-Cache will let the netfs go ahead with
  140. read operations, no matter whether the file is yet cached. If not yet
  141. cached, read operations will be immediately rejected with ENODATA until
  142. the first known page is uncached - as to that point there can be no data
  143. to be read out of the cache for that file that isn't currently also held
  144. in the pagecache.
  145. (3) State FSCACHE_OBJECT_CREATING.
  146. Create an object on disk, using the parent as a starting point. This
  147. happens if the lookup failed to find the object, or if the object's
  148. coherency data indicated what's on disk is out of date. In this state,
  149. FS-Cache expects the cache to create
  150. The cache should call fscache_obtained_object() if creation completes
  151. successfully, fscache_object_lookup_negative() otherwise.
  152. At the completion of creation, FS-Cache will start processing write
  153. operations the netfs has queued for an object. If creation failed, the
  154. write ops will be transparently discarded, and nothing recorded in the
  155. cache.
  156. There are some normal running states in which the object spends its time
  157. servicing netfs requests:
  158. (4) State FSCACHE_OBJECT_AVAILABLE.
  159. A transient state in which pending operations are started, child objects
  160. are permitted to advance from FSCACHE_OBJECT_INIT state, and temporary
  161. lookup data is freed.
  162. (5) State FSCACHE_OBJECT_ACTIVE.
  163. The normal running state. In this state, requests the netfs makes will be
  164. passed on to the cache.
  165. (6) State FSCACHE_OBJECT_INVALIDATING.
  166. The object is undergoing invalidation. When the state comes here, it
  167. discards all pending read, write and attribute change operations as it is
  168. going to clear out the cache entirely and reinitialise it. It will then
  169. continue to the FSCACHE_OBJECT_UPDATING state.
  170. (7) State FSCACHE_OBJECT_UPDATING.
  171. The state machine comes here to update the object in the cache from the
  172. netfs's records. This involves updating the auxiliary data that is used
  173. to maintain coherency.
  174. And there are terminal states in which an object cleans itself up, deallocates
  175. memory and potentially deletes stuff from disk:
  176. (8) State FSCACHE_OBJECT_LC_DYING.
  177. The object comes here if it is dying because of a lookup or creation
  178. error. This would be due to a disk error or system error of some sort.
  179. Temporary data is cleaned up, and the parent is released.
  180. (9) State FSCACHE_OBJECT_DYING.
  181. The object comes here if it is dying due to an error, because its parent
  182. cookie has been relinquished by the netfs or because the cache is being
  183. withdrawn.
  184. Any child objects waiting on this one are given CPU time so that they too
  185. can destroy themselves. This object waits for all its children to go away
  186. before advancing to the next state.
  187. (10) State FSCACHE_OBJECT_ABORT_INIT.
  188. The object comes to this state if it was waiting on its parent in
  189. FSCACHE_OBJECT_INIT, but its parent died. The object will destroy itself
  190. so that the parent may proceed from the FSCACHE_OBJECT_DYING state.
  191. (11) State FSCACHE_OBJECT_RELEASING.
  192. (12) State FSCACHE_OBJECT_RECYCLING.
  193. The object comes to one of these two states when dying once it is rid of
  194. all its children, if it is dying because the netfs relinquished its
  195. cookie. In the first state, the cached data is expected to persist, and
  196. in the second it will be deleted.
  197. (13) State FSCACHE_OBJECT_WITHDRAWING.
  198. The object transits to this state if the cache decides it wants to
  199. withdraw the object from service, perhaps to make space, but also due to
  200. error or just because the whole cache is being withdrawn.
  201. (14) State FSCACHE_OBJECT_DEAD.
  202. The object transits to this state when the in-memory object record is
  203. ready to be deleted. The object processor shouldn't ever see an object in
  204. this state.
  205. THE SET OF EVENTS
  206. -----------------
  207. There are a number of events that can be raised to an object state machine:
  208. (*) FSCACHE_OBJECT_EV_UPDATE
  209. The netfs requested that an object be updated. The state machine will ask
  210. the cache backend to update the object, and the cache backend will ask the
  211. netfs for details of the change through its cookie definition ops.
  212. (*) FSCACHE_OBJECT_EV_CLEARED
  213. This is signalled in two circumstances:
  214. (a) when an object's last child object is dropped and
  215. (b) when the last operation outstanding on an object is completed.
  216. This is used to proceed from the dying state.
  217. (*) FSCACHE_OBJECT_EV_ERROR
  218. This is signalled when an I/O error occurs during the processing of some
  219. object.
  220. (*) FSCACHE_OBJECT_EV_RELEASE
  221. (*) FSCACHE_OBJECT_EV_RETIRE
  222. These are signalled when the netfs relinquishes a cookie it was using.
  223. The event selected depends on whether the netfs asks for the backing
  224. object to be retired (deleted) or retained.
  225. (*) FSCACHE_OBJECT_EV_WITHDRAW
  226. This is signalled when the cache backend wants to withdraw an object.
  227. This means that the object will have to be detached from the netfs's
  228. cookie.
  229. Because the withdrawing releasing/retiring events are all handled by the object
  230. state machine, it doesn't matter if there's a collision with both ends trying
  231. to sever the connection at the same time. The state machine can just pick
  232. which one it wants to honour, and that effects the other.