1<html><head><meta http-equiv="Content-Type" content="text/html; charset=ANSI_X3.4-1968"><title>Chapter&#160;6.&#160;The Linux Journalling API</title><meta name="generator" content="DocBook XSL Stylesheets V1.78.1"><link rel="home" href="index.html" title="Linux Filesystems API"><link rel="up" href="index.html" title="Linux Filesystems API"><link rel="prev" href="API-debugfs-create-devm-seqfile.html" title="debugfs_create_devm_seqfile"><link rel="next" href="data_types.html" title="Data Types"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&#160;6.&#160;The Linux Journalling API</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="API-debugfs-create-devm-seqfile.html">Prev</a>&#160;</td><th width="60%" align="center">&#160;</th><td width="20%" align="right">&#160;<a accesskey="n" href="data_types.html">Next</a></td></tr></table><hr></div><div class="chapter"><div class="titlepage"><div><div><h1 class="title"><a name="LinuxJDBAPI"></a>Chapter&#160;6.&#160;The Linux Journalling API</h1></div><div><div class="authorgroup"><div class="author"><h3 class="author"><span class="firstname">Roger</span> <span class="surname">Gammans</span></h3><div class="affiliation"><div class="address"><p><br>
2&#160;&#160;&#160;&#160;&#160;&#160;<code class="email">&lt;<a class="email" href="mailto:rgammans@computer-surgery.co.uk">rgammans@computer-surgery.co.uk</a>&gt;</code><br>
3&#160;&#160;&#160;&#160;&#160;</p></div></div></div></div></div><div><div class="authorgroup"><div class="author"><h3 class="author"><span class="firstname">Stephen</span> <span class="surname">Tweedie</span></h3><div class="affiliation"><div class="address"><p><br>
4&#160;&#160;&#160;&#160;&#160;&#160;<code class="email">&lt;<a class="email" href="mailto:sct@redhat.com">sct@redhat.com</a>&gt;</code><br>
5&#160;&#160;&#160;&#160;&#160;</p></div></div></div></div></div><div><p class="copyright">Copyright &#169; 2002 Roger Gammans</p></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl class="toc"><dt><span class="sect1"><a href="LinuxJDBAPI.html#journaling_overview">Overview</a></span></dt><dd><dl><dt><span class="sect2"><a href="LinuxJDBAPI.html#journaling_details">Details</a></span></dt><dt><span class="sect2"><a href="LinuxJDBAPI.html#jbd_summary">Summary</a></span></dt></dl></dd><dt><span class="sect1"><a href="data_types.html">Data Types</a></span></dt><dd><dl><dt><span class="sect2"><a href="data_types.html#structures">Structures</a></span></dt></dl></dd><dt><span class="sect1"><a href="functions.html">Functions</a></span></dt><dd><dl><dt><span class="sect2"><a href="functions.html#journal_level">Journal Level</a></span></dt><dt><span class="sect2"><a href="functions.html#transaction_level">Transasction Level</a></span></dt></dl></dd><dt><span class="sect1"><a href="see_also.html">See also</a></span></dt></dl></div><div class="sect1"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="journaling_overview"></a>Overview</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="LinuxJDBAPI.html#journaling_details">Details</a></span></dt><dt><span class="sect2"><a href="LinuxJDBAPI.html#jbd_summary">Summary</a></span></dt></dl></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="journaling_details"></a>Details</h3></div></div></div><p>
6The journalling layer is  easy to use. You need to
7first of all create a journal_t data structure. There are
8two calls to do this dependent on how you decide to allocate the physical
9media on which the journal resides. The journal_init_inode() call
10is for journals stored in filesystem inodes, or the journal_init_dev()
11call can be use for journal stored on a raw device (in a continuous range
12of blocks). A journal_t is a typedef for a struct pointer, so when
13you are finally finished make sure you call journal_destroy() on it
14to free up any used kernel memory.
15</p><p>
16Once you have got your journal_t object you need to 'mount' or load the journal
17file, unless of course you haven't initialised it yet - in which case you
18need to call journal_create().
19</p><p>
20Most of the time however your journal file will already have been created, but
21before you load it you must call journal_wipe() to empty the journal file.
22Hang on, you say , what if the filesystem wasn't cleanly umount()'d . Well, it is the
23job of the client file system to detect this and skip the call to journal_wipe().
24</p><p>
25In either case the next call should be to journal_load() which prepares the
26journal file for use. Note that journal_wipe(..,0) calls journal_skip_recovery()
27for you if it detects any outstanding transactions in the journal and similarly
28journal_load() will call journal_recover() if necessary.
29I would advise reading fs/ext3/super.c for examples on this stage.
30[RGG: Why is the journal_wipe() call necessary - doesn't this needlessly
31complicate the API. Or isn't a good idea for the journal layer to hide
32dirty mounts from the client fs]
33</p><p>
34Now you can go ahead and start modifying the underlying
35filesystem. Almost.
36</p><p>
37
38You still need to actually journal your filesystem changes, this
39is done by wrapping them into transactions. Additionally you
40also need to wrap the modification of each of the buffers
41with calls to the journal layer, so it knows what the modifications
42you are actually making are. To do this use  journal_start() which
43returns a transaction handle.
44</p><p>
45journal_start()
46and its counterpart journal_stop(), which indicates the end of a transaction
47are nestable calls, so you can reenter a transaction if necessary,
48but remember you must call journal_stop() the same number of times as
49journal_start() before the transaction is completed (or more accurately
50leaves the update phase). Ext3/VFS makes use of this feature to simplify
51quota support.
52</p><p>
53Inside each transaction you need to wrap the modifications to the
54individual buffers (blocks). Before you start to modify a buffer you
55need to call journal_get_{create,write,undo}_access() as appropriate,
56this allows the journalling layer to copy the unmodified data if it
57needs to. After all the buffer may be part of a previously uncommitted
58transaction.
59At this point you are at last ready to modify a buffer, and once
60you are have done so you need to call journal_dirty_{meta,}data().
61Or if you've asked for access to a buffer you now know is now longer
62required to be pushed back on the device you can call journal_forget()
63in much the same way as you might have used bforget() in the past.
64</p><p>
65A journal_flush() may be called at any time to commit and checkpoint
66all your transactions.
67</p><p>
68Then at umount time , in your put_super() you can then call journal_destroy()
69to clean up your in-core journal object.
70</p><p>
71Unfortunately there a couple of ways the journal layer can cause a deadlock.
72The first thing to note is that each task can only have
73a single outstanding transaction at any one time, remember nothing
74commits until the outermost journal_stop(). This means
75you must complete the transaction at the end of each file/inode/address
76etc. operation you perform, so that the journalling system isn't re-entered
77on another journal. Since transactions can't be nested/batched
78across differing journals, and another filesystem other than
79yours (say ext3) may be modified in a later syscall.
80</p><p>
81The second case to bear in mind is that journal_start() can
82block if there isn't enough space in the journal for your transaction
83(based on the passed nblocks param) - when it blocks it merely(!) needs to
84wait for transactions to complete and be committed from other tasks,
85so essentially we are waiting for journal_stop(). So to avoid
86deadlocks you must treat journal_start/stop() as if they
87were semaphores and include them in your semaphore ordering rules to prevent
88deadlocks. Note that journal_extend() has similar blocking behaviour to
89journal_start() so you can deadlock here just as easily as on journal_start().
90</p><p>
91Try to reserve the right number of blocks the first time. ;-). This will
92be the maximum number of blocks you are going to touch in this transaction.
93I advise having a look at at least ext3_jbd.h to see the basis on which
94ext3 uses to make these decisions.
95</p><p>
96Another wriggle to watch out for is your on-disk block allocation strategy.
97why? Because, if you undo a delete, you need to ensure you haven't reused any
98of the freed blocks in a later transaction. One simple way of doing this
99is make sure any blocks you allocate only have checkpointed transactions
100listed against them. Ext3 does this in ext3_test_allocatable().
101</p><p>
102Lock is also providing through journal_{un,}lock_updates(),
103ext3 uses this when it wants a window with a clean and stable fs for a moment.
104eg.
105</p><pre class="programlisting">
106
107	journal_lock_updates() //stop new stuff happening..
108	journal_flush()        // checkpoint everything.
109	..do stuff on stable fs
110	journal_unlock_updates() // carry on with filesystem use.
111</pre><p>
112The opportunities for abuse and DOS attacks with this should be obvious,
113if you allow unprivileged userspace to trigger codepaths containing these
114calls.
115</p><p>
116A new feature of jbd since 2.5.25 is commit callbacks with the new
117journal_callback_set() function you can now ask the journalling layer
118to call you back when the transaction is finally committed to disk, so that
119you can do some of your own management. The key to this is the journal_callback
120struct, this maintains the internal callback information but you can
121extend it like this:-
122</p><pre class="programlisting">
123	struct  myfs_callback_s {
124		//Data structure element required by jbd..
125		struct journal_callback for_jbd;
126		// Stuff for myfs allocated together.
127		myfs_inode*    i_commited;
128
129	}
130</pre><p>
131this would be useful if you needed to know when data was committed to a
132particular inode.
133</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="jbd_summary"></a>Summary</h3></div></div></div><p>
134Using the journal is a matter of wrapping the different context changes,
135being each mount, each modification (transaction) and each changed buffer
136to tell the journalling layer about them.
137</p><p>
138Here is a some pseudo code to give you an idea of how it works, as
139an example.
140</p><pre class="programlisting">
141  journal_t* my_jnrl = journal_create();
142  journal_init_{dev,inode}(jnrl,...)
143  if (clean) journal_wipe();
144  journal_load();
145
146   foreach(transaction) { /*transactions must be
147                            completed before
148                            a syscall returns to
149                            userspace*/
150
151          handle_t * xct=journal_start(my_jnrl);
152          foreach(bh) {
153                journal_get_{create,write,undo}_access(xact,bh);
154                if ( myfs_modify(bh) ) { /* returns true
155                                        if makes changes */
156                           journal_dirty_{meta,}data(xact,bh);
157                } else {
158                           journal_forget(bh);
159                }
160          }
161          journal_stop(xct);
162   }
163   journal_destroy(my_jrnl);
164</pre></div></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="API-debugfs-create-devm-seqfile.html">Prev</a>&#160;</td><td width="20%" align="center">&#160;</td><td width="40%" align="right">&#160;<a accesskey="n" href="data_types.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="phrase">debugfs_create_devm_seqfile</span>&#160;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&#160;Data Types</td></tr></table></div></body></html>
165