1<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Chapter 6. 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 6. The Linux Journalling API</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="API-debugfs-create-devm-seqfile.html">Prev</a> </td><th width="60%" align="center"> </th><td width="20%" align="right"> <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 6. 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      <code class="email">&lt;<a class="email" href="mailto:rgammans@computer-surgery.co.uk">rgammans@computer-surgery.co.uk</a>&gt;</code><br>
3     </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      <code class="email">&lt;<a class="email" href="mailto:sct@redhat.com">sct@redhat.com</a>&gt;</code><br>
5     </p></div></div></div></div></div><div><p class="copyright">Copyright © 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 jbd2_journal_init_inode() call
10is for journals stored in filesystem inodes, or the jbd2_journal_init_dev()
11call can be used 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 jbd2_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. The journalling layer expects the space for the journal was already
18allocated and initialized properly by the userspace tools.  When loading the
19journal you must call jbd2_journal_load() to process journal contents.  If the
20client file system detects the journal contents does not need to be processed
21(or even need not have valid contents), it may call jbd2_journal_wipe() to
22clear the journal contents before calling jbd2_journal_load().
23</p><p>
24Note that jbd2_journal_wipe(..,0) calls jbd2_journal_skip_recovery() for you if
25it detects any outstanding transactions in the journal and similarly
26jbd2_journal_load() will call jbd2_journal_recover() if necessary.  I would
27advise reading ext4_load_journal() in fs/ext4/super.c for examples on this
28stage.
29</p><p>
30Now you can go ahead and start modifying the underlying
31filesystem. Almost.
32</p><p>
33
34You still need to actually journal your filesystem changes, this
35is done by wrapping them into transactions. Additionally you
36also need to wrap the modification of each of the buffers
37with calls to the journal layer, so it knows what the modifications
38you are actually making are. To do this use jbd2_journal_start() which
39returns a transaction handle.
40</p><p>
41jbd2_journal_start()
42and its counterpart jbd2_journal_stop(), which indicates the end of a
43transaction are nestable calls, so you can reenter a transaction if necessary,
44but remember you must call jbd2_journal_stop() the same number of times as
45jbd2_journal_start() before the transaction is completed (or more accurately
46leaves the update phase). Ext4/VFS makes use of this feature to simplify
47handling of inode dirtying, quota support, etc.
48</p><p>
49Inside each transaction you need to wrap the modifications to the
50individual buffers (blocks). Before you start to modify a buffer you
51need to call jbd2_journal_get_{create,write,undo}_access() as appropriate,
52this allows the journalling layer to copy the unmodified data if it
53needs to. After all the buffer may be part of a previously uncommitted
54transaction.
55At this point you are at last ready to modify a buffer, and once
56you are have done so you need to call jbd2_journal_dirty_{meta,}data().
57Or if you've asked for access to a buffer you now know is now longer
58required to be pushed back on the device you can call jbd2_journal_forget()
59in much the same way as you might have used bforget() in the past.
60</p><p>
61A jbd2_journal_flush() may be called at any time to commit and checkpoint
62all your transactions.
63</p><p>
64Then at umount time , in your put_super() you can then call jbd2_journal_destroy()
65to clean up your in-core journal object.
66</p><p>
67Unfortunately there a couple of ways the journal layer can cause a deadlock.
68The first thing to note is that each task can only have
69a single outstanding transaction at any one time, remember nothing
70commits until the outermost jbd2_journal_stop(). This means
71you must complete the transaction at the end of each file/inode/address
72etc. operation you perform, so that the journalling system isn't re-entered
73on another journal. Since transactions can't be nested/batched
74across differing journals, and another filesystem other than
75yours (say ext4) may be modified in a later syscall.
76</p><p>
77The second case to bear in mind is that jbd2_journal_start() can
78block if there isn't enough space in the journal for your transaction
79(based on the passed nblocks param) - when it blocks it merely(!) needs to
80wait for transactions to complete and be committed from other tasks,
81so essentially we are waiting for jbd2_journal_stop(). So to avoid
82deadlocks you must treat jbd2_journal_start/stop() as if they
83were semaphores and include them in your semaphore ordering rules to prevent
84deadlocks. Note that jbd2_journal_extend() has similar blocking behaviour to
85jbd2_journal_start() so you can deadlock here just as easily as on
86jbd2_journal_start().
87</p><p>
88Try to reserve the right number of blocks the first time. ;-). This will
89be the maximum number of blocks you are going to touch in this transaction.
90I advise having a look at at least ext4_jbd.h to see the basis on which
91ext4 uses to make these decisions.
92</p><p>
93Another wriggle to watch out for is your on-disk block allocation strategy.
94Why? Because, if you do a delete, you need to ensure you haven't reused any
95of the freed blocks until the transaction freeing these blocks commits. If you
96reused these blocks and crash happens, there is no way to restore the contents
97of the reallocated blocks at the end of the last fully committed transaction.
98
99One simple way of doing this is to mark blocks as free in internal in-memory
100block allocation structures only after the transaction freeing them commits.
101Ext4 uses journal commit callback for this purpose.
102</p><p>
103With journal commit callbacks you can ask the journalling layer to call a
104callback function when the transaction is finally committed to disk, so that
105you can do some of your own management. You ask the journalling layer for
106calling the callback by simply setting journal-&gt;j_commit_callback function
107pointer and that function is called after each transaction commit. You can also
108use transaction-&gt;t_private_list for attaching entries to a transaction that
109need processing when the transaction commits.
110</p><p>
111JBD2 also provides a way to block all transaction updates via
112jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a window with a
113clean and stable fs for a moment.  E.g.
114</p><pre class="programlisting">
115
116	jbd2_journal_lock_updates() //stop new stuff happening..
117	jbd2_journal_flush()        // checkpoint everything.
118	..do stuff on stable fs
119	jbd2_journal_unlock_updates() // carry on with filesystem use.
120</pre><p>
121The opportunities for abuse and DOS attacks with this should be obvious,
122if you allow unprivileged userspace to trigger codepaths containing these
123calls.
124</p></div><div class="sect2"><div class="titlepage"><div><div><h3 class="title"><a name="jbd_summary"></a>Summary</h3></div></div></div><p>
125Using the journal is a matter of wrapping the different context changes,
126being each mount, each modification (transaction) and each changed buffer
127to tell the journalling layer about them.
128</p></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> </td><td width="20%" align="center"> </td><td width="40%" align="right"> <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> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> Data Types</td></tr></table></div></body></html>
129