journal-api.tmpl
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:10k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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