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