journal-api.tmpl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
  4. <book id="LinuxJBDAPI">
  5. <bookinfo>
  6. <title>The Linux Journalling API</title>
  7. <authorgroup>
  8. <author>
  9. <firstname>Roger</firstname>
  10. <surname>Gammans</surname>
  11. <affiliation>
  12. <address>
  13. <email>rgammans@computer-surgery.co.uk</email>
  14. </address>
  15. </affiliation>
  16. </author>
  17. </authorgroup>
  18. <authorgroup>
  19. <author>
  20. <firstname>Stephen</firstname>
  21. <surname>Tweedie</surname>
  22. <affiliation>
  23. <address>
  24. <email>sct@redhat.com</email>
  25. </address>
  26. </affiliation>
  27. </author>
  28. </authorgroup>
  29. <copyright>
  30. <year>2002</year>
  31. <holder>Roger Gammans</holder>
  32. </copyright>
  33. <legalnotice>
  34. <para>
  35. This documentation is free software; you can redistribute
  36. it and/or modify it under the terms of the GNU General Public
  37. License as published by the Free Software Foundation; either
  38. version 2 of the License, or (at your option) any later
  39. version.
  40. </para>
  41. <para>
  42. This program is distributed in the hope that it will be
  43. useful, but WITHOUT ANY WARRANTY; without even the implied
  44. warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  45. See the GNU General Public License for more details.
  46. </para>
  47. <para>
  48. You should have received a copy of the GNU General Public
  49. License along with this program; if not, write to the Free
  50. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  51. MA 02111-1307 USA
  52. </para>
  53. <para>
  54. For more details see the file COPYING in the source
  55. distribution of Linux.
  56. </para>
  57. </legalnotice>
  58. </bookinfo>
  59. <toc></toc>
  60. <chapter id="Overview">
  61. <title>Overview</title>
  62. <sect1>
  63. <title>Details</title>
  64. <para>
  65. The journalling layer is easy to use. You need to
  66. first of all create a journal_t data structure. There are
  67. two calls to do this dependent on how you decide to allocate the physical
  68. media on which the journal resides. The journal_init_inode() call
  69. is for journals stored in filesystem inodes, or the journal_init_dev()
  70. call can be use for journal stored on a raw device (in a continuous range
  71. of blocks). A journal_t is a typedef for a struct pointer, so when
  72. you are finally finished make sure you call journal_destroy() on it
  73. to free up any used kernel memory.
  74. </para>
  75. <para>
  76. Once you have got your journal_t object you need to 'mount' or load the journal
  77. file, unless of course you haven't initialised it yet - in which case you
  78. need to call journal_create().
  79. </para>
  80. <para>
  81. Most of the time however your journal file will already have been created, but
  82. before you load it you must call journal_wipe() to empty the journal file.
  83. Hang on, you say , what if the filesystem wasn't cleanly umount()'d . Well, it is the
  84. job of the client file system to detect this and skip the call to journal_wipe().
  85. </para>
  86. <para>
  87. In either case the next call should be to journal_load() which prepares the
  88. journal file for use. Note that journal_wipe(..,0) calls journal_skip_recovery()
  89. for you if it detects any outstanding transactions in the journal and similarly
  90. journal_load() will call journal_recover() if necessary.
  91. I would advise reading fs/ext3/super.c for examples on this stage.
  92. [RGG: Why is the journal_wipe() call necessary - doesn't this needlessly
  93. complicate the API. Or isn't a good idea for the journal layer to hide
  94. dirty mounts from the client fs]
  95. </para>
  96. <para>
  97. Now you can go ahead and start modifying the underlying
  98. filesystem. Almost.
  99. </para>
  100. <para>
  101. You still need to actually journal your filesystem changes, this
  102. is done by wrapping them into transactions. Additionally you
  103. also need to wrap the modification of each of the buffers
  104. with calls to the journal layer, so it knows what the modifications
  105. you are actually making are. To do this use journal_start() which
  106. returns a transaction handle.
  107. </para>
  108. <para>
  109. journal_start()
  110. and its counterpart journal_stop(), which indicates the end of a transaction
  111. are nestable calls, so you can reenter a transaction if necessary,
  112. but remember you must call journal_stop() the same number of times as
  113. journal_start() before the transaction is completed (or more accurately
  114. leaves the update phase). Ext3/VFS makes use of this feature to simplify
  115. quota support.
  116. </para>
  117. <para>
  118. Inside each transaction you need to wrap the modifications to the
  119. individual buffers (blocks). Before you start to modify a buffer you
  120. need to call journal_get_{create,write,undo}_access() as appropriate,
  121. this allows the journalling layer to copy the unmodified data if it
  122. needs to. After all the buffer may be part of a previously uncommitted
  123. transaction.
  124. At this point you are at last ready to modify a buffer, and once
  125. you are have done so you need to call journal_dirty_{meta,}data().
  126. Or if you've asked for access to a buffer you now know is now longer
  127. required to be pushed back on the device you can call journal_forget()
  128. in much the same way as you might have used bforget() in the past.
  129. </para>
  130. <para>
  131. A journal_flush() may be called at any time to commit and checkpoint
  132. all your transactions.
  133. </para>
  134. <para>
  135. Then at umount time , in your put_super() (2.4) or write_super() (2.5)
  136. you can then call journal_destroy() to clean up your in-core journal object.
  137. </para>
  138. <para>
  139. Unfortunately there a couple of ways the journal layer can cause a deadlock.
  140. The first thing to note is that each task can only have
  141. a single outstanding transaction at any one time, remember nothing
  142. commits until the outermost journal_stop(). This means
  143. you must complete the transaction at the end of each file/inode/address
  144. etc. operation you perform, so that the journalling system isn't re-entered
  145. on another journal. Since transactions can't be nested/batched
  146. across differing journals, and another filesystem other than
  147. yours (say ext3) may be modified in a later syscall.
  148. </para>
  149. <para>
  150. The second case to bear in mind is that journal_start() can
  151. block if there isn't enough space in the journal for your transaction
  152. (based on the passed nblocks param) - when it blocks it merely(!) needs to
  153. wait for transactions to complete and be committed from other tasks,
  154. so essentially we are waiting for journal_stop(). So to avoid
  155. deadlocks you must treat journal_start/stop() as if they
  156. were semaphores and include them in your semaphore ordering rules to prevent
  157. deadlocks. Note that journal_extend() has similar blocking behaviour to
  158. journal_start() so you can deadlock here just as easily as on journal_start().
  159. </para>
  160. <para>
  161. Try to reserve the right number of blocks the first time. ;-). This will
  162. be the maximum number of blocks you are going to touch in this transaction.
  163. I advise having a look at at least ext3_jbd.h to see the basis on which
  164. ext3 uses to make these decisions.
  165. </para>
  166. <para>
  167. Another wriggle to watch out for is your on-disk block allocation strategy.
  168. why? Because, if you undo a delete, you need to ensure you haven't reused any
  169. of the freed blocks in a later transaction. One simple way of doing this
  170. is make sure any blocks you allocate only have checkpointed transactions
  171. listed against them. Ext3 does this in ext3_test_allocatable().
  172. </para>
  173. <para>
  174. Lock is also providing through journal_{un,}lock_updates(),
  175. ext3 uses this when it wants a window with a clean and stable fs for a moment.
  176. eg.
  177. </para>
  178. <programlisting>
  179. journal_lock_updates() //stop new stuff happening..
  180. journal_flush() // checkpoint everything.
  181. ..do stuff on stable fs
  182. journal_unlock_updates() // carry on with filesystem use.
  183. </programlisting>
  184. <para>
  185. The opportunities for abuse and DOS attacks with this should be obvious,
  186. if you allow unprivileged userspace to trigger codepaths containing these
  187. calls.
  188. </para>
  189. <para>
  190. A new feature of jbd since 2.5.25 is commit callbacks with the new
  191. journal_callback_set() function you can now ask the journalling layer
  192. to call you back when the transaction is finally committed to disk, so that
  193. you can do some of your own management. The key to this is the journal_callback
  194. struct, this maintains the internal callback information but you can
  195. extend it like this:-
  196. </para>
  197. <programlisting>
  198. struct myfs_callback_s {
  199. //Data structure element required by jbd..
  200. struct journal_callback for_jbd;
  201. // Stuff for myfs allocated together.
  202. myfs_inode* i_commited;
  203. }
  204. </programlisting>
  205. <para>
  206. this would be useful if you needed to know when data was committed to a
  207. particular inode.
  208. </para>
  209. </sect1>
  210. <sect1>
  211. <title>Summary</title>
  212. <para>
  213. Using the journal is a matter of wrapping the different context changes,
  214. being each mount, each modification (transaction) and each changed buffer
  215. to tell the journalling layer about them.
  216. </para>
  217. <para>
  218. Here is a some pseudo code to give you an idea of how it works, as
  219. an example.
  220. </para>
  221. <programlisting>
  222. journal_t* my_jnrl = journal_create();
  223. journal_init_{dev,inode}(jnrl,...)
  224. if (clean) journal_wipe();
  225. journal_load();
  226. foreach(transaction) { /*transactions must be
  227. completed before
  228. a syscall returns to
  229. userspace*/
  230. handle_t * xct=journal_start(my_jnrl);
  231. foreach(bh) {
  232. journal_get_{create,write,undo}_access(xact,bh);
  233. if ( myfs_modify(bh) ) { /* returns true
  234. if makes changes */
  235. journal_dirty_{meta,}data(xact,bh);
  236. } else {
  237. journal_forget(bh);
  238. }
  239. }
  240. journal_stop(xct);
  241. }
  242. journal_destroy(my_jrnl);
  243. </programlisting>
  244. </sect1>
  245. </chapter>
  246. <chapter id="adt">
  247. <title>Data Types</title>
  248. <para>
  249. The journalling layer uses typedefs to 'hide' the concrete definitions
  250. of the structures used. As a client of the JBD layer you can
  251. just rely on the using the pointer as a magic cookie of some sort.
  252. Obviously the hiding is not enforced as this is 'C'.
  253. </para>
  254. <sect1><title>Structures</title>
  255. !Iinclude/linux/jbd.h
  256. </sect1>
  257. </chapter>
  258. <chapter id="calls">
  259. <title>Functions</title>
  260. <para>
  261. The functions here are split into two groups those that
  262. affect a journal as a whole, and those which are used to
  263. manage transactions
  264. </para>
  265. <sect1><title>Journal Level</title>
  266. !Efs/jbd/journal.c
  267. !Ifs/jbd/recovery.c
  268. </sect1>
  269. <sect1><title>Transasction Level</title>
  270. !Efs/jbd/transaction.c
  271. </sect1>
  272. </chapter>
  273. <chapter>
  274. <title>See also</title>
  275. <para>
  276. <citation>
  277. <ulink url="ftp://ftp.uk.linux.org/pub/linux/sct/fs/jfs/journal-design.ps.gz">
  278. Journaling the Linux ext2fs Filesystem,LinuxExpo 98, Stephen Tweedie
  279. </ulink>
  280. </citation>
  281. </para>
  282. <para>
  283. <citation>
  284. <ulink url="http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html">
  285. Ext3 Journalling FileSystem , OLS 2000, Dr. Stephen Tweedie
  286. </ulink>
  287. </citation>
  288. </para>
  289. </chapter>
  290. </book>