1The Linux Journalling API
2=========================
3
4Overview
5--------
6
7Details
8~~~~~~~
9
10The journalling layer is easy to use. You need to first of all create a
11journal_t data structure. There are two calls to do this dependent on
12how you decide to allocate the physical media on which the journal
13resides. The jbd2_journal_init_inode() call is for journals stored in
14filesystem inodes, or the jbd2_journal_init_dev() call can be used
15for journal stored on a raw device (in a continuous range of blocks). A
16journal_t is a typedef for a struct pointer, so when you are finally
17finished make sure you call jbd2_journal_destroy() on it to free up
18any used kernel memory.
19
20Once you have got your journal_t object you need to 'mount' or load the
21journal file. The journalling layer expects the space for the journal
22was already allocated and initialized properly by the userspace tools.
23When loading the journal you must call jbd2_journal_load() to process
24journal contents. If the client file system detects the journal contents
25does not need to be processed (or even need not have valid contents), it
26may call jbd2_journal_wipe() to clear the journal contents before
27calling jbd2_journal_load().
28
29Note that jbd2_journal_wipe(..,0) calls
30jbd2_journal_skip_recovery() for you if it detects any outstanding
31transactions in the journal and similarly jbd2_journal_load() will
32call jbd2_journal_recover() if necessary. I would advise reading
33ext4_load_journal() in fs/ext4/super.c for examples on this stage.
34
35Now you can go ahead and start modifying the underlying filesystem.
36Almost.
37
38You still need to actually journal your filesystem changes, this is done
39by wrapping them into transactions. Additionally you also need to wrap
40the modification of each of the buffers with calls to the journal layer,
41so it knows what the modifications you are actually making are. To do
42this use jbd2_journal_start() which returns a transaction handle.
43
44jbd2_journal_start() and its counterpart jbd2_journal_stop(),
45which indicates the end of a transaction are nestable calls, so you can
46reenter a transaction if necessary, but remember you must call
47jbd2_journal_stop() the same number of times as
48jbd2_journal_start() before the transaction is completed (or more
49accurately leaves the update phase). Ext4/VFS makes use of this feature to
50simplify handling of inode dirtying, quota support, etc.
51
52Inside each transaction you need to wrap the modifications to the
53individual buffers (blocks). Before you start to modify a buffer you
54need to call jbd2_journal_get_create_access() /
55jbd2_journal_get_write_access() /
56jbd2_journal_get_undo_access() as appropriate, this allows the
57journalling layer to copy the unmodified
58data if it needs to. After all the buffer may be part of a previously
59uncommitted transaction. At this point you are at last ready to modify a
60buffer, and once you are have done so you need to call
61jbd2_journal_dirty_metadata(). Or if you've asked for access to a
62buffer you now know is now longer required to be pushed back on the
63device you can call jbd2_journal_forget() in much the same way as you
64might have used bforget() in the past.
65
66A jbd2_journal_flush() may be called at any time to commit and
67checkpoint all your transactions.
68
69Then at umount time , in your put_super() you can then call
70jbd2_journal_destroy() to clean up your in-core journal object.
71
72Unfortunately there a couple of ways the journal layer can cause a
73deadlock. The first thing to note is that each task can only have a
74single outstanding transaction at any one time, remember nothing commits
75until the outermost jbd2_journal_stop(). This means you must complete
76the transaction at the end of each file/inode/address etc. operation you
77perform, so that the journalling system isn't re-entered on another
78journal. Since transactions can't be nested/batched across differing
79journals, and another filesystem other than yours (say ext4) may be
80modified in a later syscall.
81
82The second case to bear in mind is that jbd2_journal_start() can block
83if there isn't enough space in the journal for your transaction (based
84on the passed nblocks param) - when it blocks it merely(!) needs to wait
85for transactions to complete and be committed from other tasks, so
86essentially we are waiting for jbd2_journal_stop(). So to avoid
87deadlocks you must treat jbd2_journal_start() /
88jbd2_journal_stop() as if they were semaphores and include them in
89your semaphore ordering rules to prevent
90deadlocks. Note that jbd2_journal_extend() has similar blocking
91behaviour to jbd2_journal_start() so you can deadlock here just as
92easily as on jbd2_journal_start().
93
94Try to reserve the right number of blocks the first time. ;-). This will
95be the maximum number of blocks you are going to touch in this
96transaction. I advise having a look at at least ext4_jbd.h to see the
97basis on which ext4 uses to make these decisions.
98
99Another wriggle to watch out for is your on-disk block allocation
100strategy. Why? Because, if you do a delete, you need to ensure you
101haven't reused any of the freed blocks until the transaction freeing
102these blocks commits. If you reused these blocks and crash happens,
103there is no way to restore the contents of the reallocated blocks at the
104end of the last fully committed transaction. One simple way of doing
105this is to mark blocks as free in internal in-memory block allocation
106structures only after the transaction freeing them commits. Ext4 uses
107journal commit callback for this purpose.
108
109With journal commit callbacks you can ask the journalling layer to call
110a callback function when the transaction is finally committed to disk,
111so that you can do some of your own management. You ask the journalling
112layer for calling the callback by simply setting
113``journal->j_commit_callback`` function pointer and that function is
114called after each transaction commit.
115
116JBD2 also provides a way to block all transaction updates via
117jbd2_journal_lock_updates() /
118jbd2_journal_unlock_updates(). Ext4 uses this when it wants a
119window with a clean and stable fs for a moment. E.g.
120
121::
122
123
124        jbd2_journal_lock_updates() //stop new stuff happening..
125        jbd2_journal_flush()        // checkpoint everything.
126        ..do stuff on stable fs
127        jbd2_journal_unlock_updates() // carry on with filesystem use.
128
129The opportunities for abuse and DOS attacks with this should be obvious,
130if you allow unprivileged userspace to trigger codepaths containing
131these calls.
132
133Fast commits
134~~~~~~~~~~~~
135
136JBD2 to also allows you to perform file-system specific delta commits known as
137fast commits. In order to use fast commits, you will need to set following
138callbacks that perform corresponding work:
139
140`journal->j_fc_cleanup_cb`: Cleanup function called after every full commit and
141fast commit.
142
143`journal->j_fc_replay_cb`: Replay function called for replay of fast commit
144blocks.
145
146File system is free to perform fast commits as and when it wants as long as it
147gets permission from JBD2 to do so by calling the function
148:c:func:`jbd2_fc_begin_commit()`. Once a fast commit is done, the client
149file  system should tell JBD2 about it by calling
150:c:func:`jbd2_fc_end_commit()`. If the file system wants JBD2 to perform a full
151commit immediately after stopping the fast commit it can do so by calling
152:c:func:`jbd2_fc_end_commit_fallback()`. This is useful if fast commit operation
153fails for some reason and the only way to guarantee consistency is for JBD2 to
154perform the full traditional commit.
155
156JBD2 helper functions to manage fast commit buffers. File system can use
157:c:func:`jbd2_fc_get_buf()` and :c:func:`jbd2_fc_wait_bufs()` to allocate
158and wait on IO completion of fast commit buffers.
159
160Currently, only Ext4 implements fast commits. For details of its implementation
161of fast commits, please refer to the top level comments in
162fs/ext4/fast_commit.c.
163
164Summary
165~~~~~~~
166
167Using the journal is a matter of wrapping the different context changes,
168being each mount, each modification (transaction) and each changed
169buffer to tell the journalling layer about them.
170
171Data Types
172----------
173
174The journalling layer uses typedefs to 'hide' the concrete definitions
175of the structures used. As a client of the JBD2 layer you can just rely
176on the using the pointer as a magic cookie of some sort. Obviously the
177hiding is not enforced as this is 'C'.
178
179Structures
180~~~~~~~~~~
181
182.. kernel-doc:: include/linux/jbd2.h
183   :internal:
184
185Functions
186---------
187
188The functions here are split into two groups those that affect a journal
189as a whole, and those which are used to manage transactions
190
191Journal Level
192~~~~~~~~~~~~~
193
194.. kernel-doc:: fs/jbd2/journal.c
195   :export:
196
197.. kernel-doc:: fs/jbd2/recovery.c
198   :internal:
199
200Transaction Level
201~~~~~~~~~~~~~~~~~~
202
203.. kernel-doc:: fs/jbd2/transaction.c
204
205See also
206--------
207
208`Journaling the Linux ext2fs Filesystem, LinuxExpo 98, Stephen
209Tweedie <http://kernel.org/pub/linux/kernel/people/sct/ext3/journal-design.ps.gz>`__
210
211`Ext3 Journalling FileSystem, OLS 2000, Dr. Stephen
212Tweedie <http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html>`__
213
214