Skip to content

Commit 82ff50b

Browse files
author
Jan Kara
committed
doc: Update doc about journalling layer
Documentation of journalling layer in Documentation/DocBook/filesystems.tmpl speaks about JBD layer. Since that is going away, update the documentation to speak about JBD2. Also update the parts that have changed since someone last touched the document and remove some parts which are just misleading and outdated. Signed-off-by: Jan Kara <jack@suse.com>
1 parent acc84b0 commit 82ff50b

File tree

1 file changed

+67
-111
lines changed

1 file changed

+67
-111
lines changed

Documentation/DocBook/filesystems.tmpl

Lines changed: 67 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -146,36 +146,30 @@
146146
The journalling layer is easy to use. You need to
147147
first of all create a journal_t data structure. There are
148148
two calls to do this dependent on how you decide to allocate the physical
149-
media on which the journal resides. The journal_init_inode() call
150-
is for journals stored in filesystem inodes, or the journal_init_dev()
151-
call can be use for journal stored on a raw device (in a continuous range
149+
media on which the journal resides. The jbd2_journal_init_inode() call
150+
is for journals stored in filesystem inodes, or the jbd2_journal_init_dev()
151+
call can be used for journal stored on a raw device (in a continuous range
152152
of blocks). A journal_t is a typedef for a struct pointer, so when
153-
you are finally finished make sure you call journal_destroy() on it
153+
you are finally finished make sure you call jbd2_journal_destroy() on it
154154
to free up any used kernel memory.
155155
</para>
156156

157157
<para>
158158
Once you have got your journal_t object you need to 'mount' or load the journal
159-
file, unless of course you haven't initialised it yet - in which case you
160-
need to call journal_create().
159+
file. The journalling layer expects the space for the journal was already
160+
allocated and initialized properly by the userspace tools. When loading the
161+
journal you must call jbd2_journal_load() to process journal contents. If the
162+
client file system detects the journal contents does not need to be processed
163+
(or even need not have valid contents), it may call jbd2_journal_wipe() to
164+
clear the journal contents before calling jbd2_journal_load().
161165
</para>
162166

163167
<para>
164-
Most of the time however your journal file will already have been created, but
165-
before you load it you must call journal_wipe() to empty the journal file.
166-
Hang on, you say , what if the filesystem wasn't cleanly umount()'d . Well, it is the
167-
job of the client file system to detect this and skip the call to journal_wipe().
168-
</para>
169-
170-
<para>
171-
In either case the next call should be to journal_load() which prepares the
172-
journal file for use. Note that journal_wipe(..,0) calls journal_skip_recovery()
173-
for you if it detects any outstanding transactions in the journal and similarly
174-
journal_load() will call journal_recover() if necessary.
175-
I would advise reading fs/ext3/super.c for examples on this stage.
176-
[RGG: Why is the journal_wipe() call necessary - doesn't this needlessly
177-
complicate the API. Or isn't a good idea for the journal layer to hide
178-
dirty mounts from the client fs]
168+
Note that jbd2_journal_wipe(..,0) calls jbd2_journal_skip_recovery() for you if
169+
it detects any outstanding transactions in the journal and similarly
170+
jbd2_journal_load() will call jbd2_journal_recover() if necessary. I would
171+
advise reading ext4_load_journal() in fs/ext4/super.c for examples on this
172+
stage.
179173
</para>
180174

181175
<para>
@@ -189,124 +183,116 @@ You still need to actually journal your filesystem changes, this
189183
is done by wrapping them into transactions. Additionally you
190184
also need to wrap the modification of each of the buffers
191185
with calls to the journal layer, so it knows what the modifications
192-
you are actually making are. To do this use journal_start() which
186+
you are actually making are. To do this use jbd2_journal_start() which
193187
returns a transaction handle.
194188
</para>
195189

196190
<para>
197-
journal_start()
198-
and its counterpart journal_stop(), which indicates the end of a transaction
199-
are nestable calls, so you can reenter a transaction if necessary,
200-
but remember you must call journal_stop() the same number of times as
201-
journal_start() before the transaction is completed (or more accurately
202-
leaves the update phase). Ext3/VFS makes use of this feature to simplify
203-
quota support.
191+
jbd2_journal_start()
192+
and its counterpart jbd2_journal_stop(), which indicates the end of a
193+
transaction are nestable calls, so you can reenter a transaction if necessary,
194+
but remember you must call jbd2_journal_stop() the same number of times as
195+
jbd2_journal_start() before the transaction is completed (or more accurately
196+
leaves the update phase). Ext4/VFS makes use of this feature to simplify
197+
handling of inode dirtying, quota support, etc.
204198
</para>
205199

206200
<para>
207201
Inside each transaction you need to wrap the modifications to the
208202
individual buffers (blocks). Before you start to modify a buffer you
209-
need to call journal_get_{create,write,undo}_access() as appropriate,
203+
need to call jbd2_journal_get_{create,write,undo}_access() as appropriate,
210204
this allows the journalling layer to copy the unmodified data if it
211205
needs to. After all the buffer may be part of a previously uncommitted
212206
transaction.
213207
At this point you are at last ready to modify a buffer, and once
214-
you are have done so you need to call journal_dirty_{meta,}data().
208+
you are have done so you need to call jbd2_journal_dirty_{meta,}data().
215209
Or if you've asked for access to a buffer you now know is now longer
216-
required to be pushed back on the device you can call journal_forget()
210+
required to be pushed back on the device you can call jbd2_journal_forget()
217211
in much the same way as you might have used bforget() in the past.
218212
</para>
219213

220214
<para>
221-
A journal_flush() may be called at any time to commit and checkpoint
215+
A jbd2_journal_flush() may be called at any time to commit and checkpoint
222216
all your transactions.
223217
</para>
224218

225219
<para>
226-
Then at umount time , in your put_super() you can then call journal_destroy()
220+
Then at umount time , in your put_super() you can then call jbd2_journal_destroy()
227221
to clean up your in-core journal object.
228222
</para>
229223

230224
<para>
231225
Unfortunately there a couple of ways the journal layer can cause a deadlock.
232226
The first thing to note is that each task can only have
233227
a single outstanding transaction at any one time, remember nothing
234-
commits until the outermost journal_stop(). This means
228+
commits until the outermost jbd2_journal_stop(). This means
235229
you must complete the transaction at the end of each file/inode/address
236230
etc. operation you perform, so that the journalling system isn't re-entered
237231
on another journal. Since transactions can't be nested/batched
238232
across differing journals, and another filesystem other than
239-
yours (say ext3) may be modified in a later syscall.
233+
yours (say ext4) may be modified in a later syscall.
240234
</para>
241235

242236
<para>
243-
The second case to bear in mind is that journal_start() can
237+
The second case to bear in mind is that jbd2_journal_start() can
244238
block if there isn't enough space in the journal for your transaction
245239
(based on the passed nblocks param) - when it blocks it merely(!) needs to
246240
wait for transactions to complete and be committed from other tasks,
247-
so essentially we are waiting for journal_stop(). So to avoid
248-
deadlocks you must treat journal_start/stop() as if they
241+
so essentially we are waiting for jbd2_journal_stop(). So to avoid
242+
deadlocks you must treat jbd2_journal_start/stop() as if they
249243
were semaphores and include them in your semaphore ordering rules to prevent
250-
deadlocks. Note that journal_extend() has similar blocking behaviour to
251-
journal_start() so you can deadlock here just as easily as on journal_start().
244+
deadlocks. Note that jbd2_journal_extend() has similar blocking behaviour to
245+
jbd2_journal_start() so you can deadlock here just as easily as on
246+
jbd2_journal_start().
252247
</para>
253248

254249
<para>
255250
Try to reserve the right number of blocks the first time. ;-). This will
256251
be the maximum number of blocks you are going to touch in this transaction.
257-
I advise having a look at at least ext3_jbd.h to see the basis on which
258-
ext3 uses to make these decisions.
252+
I advise having a look at at least ext4_jbd.h to see the basis on which
253+
ext4 uses to make these decisions.
259254
</para>
260255

261256
<para>
262257
Another wriggle to watch out for is your on-disk block allocation strategy.
263-
why? Because, if you undo a delete, you need to ensure you haven't reused any
264-
of the freed blocks in a later transaction. One simple way of doing this
265-
is make sure any blocks you allocate only have checkpointed transactions
266-
listed against them. Ext3 does this in ext3_test_allocatable().
258+
Why? Because, if you do a delete, you need to ensure you haven't reused any
259+
of the freed blocks until the transaction freeing these blocks commits. If you
260+
reused these blocks and crash happens, there is no way to restore the contents
261+
of the reallocated blocks at the end of the last fully committed transaction.
262+
263+
One simple way of doing this is to mark blocks as free in internal in-memory
264+
block allocation structures only after the transaction freeing them commits.
265+
Ext4 uses journal commit callback for this purpose.
266+
</para>
267+
268+
<para>
269+
With journal commit callbacks you can ask the journalling layer to call a
270+
callback function when the transaction is finally committed to disk, so that
271+
you can do some of your own management. You ask the journalling layer for
272+
calling the callback by simply setting journal->j_commit_callback function
273+
pointer and that function is called after each transaction commit. You can also
274+
use transaction->t_private_list for attaching entries to a transaction that
275+
need processing when the transaction commits.
267276
</para>
268277

269278
<para>
270-
Lock is also providing through journal_{un,}lock_updates(),
271-
ext3 uses this when it wants a window with a clean and stable fs for a moment.
272-
eg.
279+
JBD2 also provides a way to block all transaction updates via
280+
jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a window with a
281+
clean and stable fs for a moment. E.g.
273282
</para>
274283

275284
<programlisting>
276285

277-
journal_lock_updates() //stop new stuff happening..
278-
journal_flush() // checkpoint everything.
286+
jbd2_journal_lock_updates() //stop new stuff happening..
287+
jbd2_journal_flush() // checkpoint everything.
279288
..do stuff on stable fs
280-
journal_unlock_updates() // carry on with filesystem use.
289+
jbd2_journal_unlock_updates() // carry on with filesystem use.
281290
</programlisting>
282291

283292
<para>
284293
The opportunities for abuse and DOS attacks with this should be obvious,
285294
if you allow unprivileged userspace to trigger codepaths containing these
286295
calls.
287-
</para>
288-
289-
<para>
290-
A new feature of jbd since 2.5.25 is commit callbacks with the new
291-
journal_callback_set() function you can now ask the journalling layer
292-
to call you back when the transaction is finally committed to disk, so that
293-
you can do some of your own management. The key to this is the journal_callback
294-
struct, this maintains the internal callback information but you can
295-
extend it like this:-
296-
</para>
297-
<programlisting>
298-
struct myfs_callback_s {
299-
//Data structure element required by jbd..
300-
struct journal_callback for_jbd;
301-
// Stuff for myfs allocated together.
302-
myfs_inode* i_commited;
303-
304-
}
305-
</programlisting>
306-
307-
<para>
308-
this would be useful if you needed to know when data was committed to a
309-
particular inode.
310296
</para>
311297

312298
</sect2>
@@ -319,36 +305,6 @@ being each mount, each modification (transaction) and each changed buffer
319305
to tell the journalling layer about them.
320306
</para>
321307

322-
<para>
323-
Here is a some pseudo code to give you an idea of how it works, as
324-
an example.
325-
</para>
326-
327-
<programlisting>
328-
journal_t* my_jnrl = journal_create();
329-
journal_init_{dev,inode}(jnrl,...)
330-
if (clean) journal_wipe();
331-
journal_load();
332-
333-
foreach(transaction) { /*transactions must be
334-
completed before
335-
a syscall returns to
336-
userspace*/
337-
338-
handle_t * xct=journal_start(my_jnrl);
339-
foreach(bh) {
340-
journal_get_{create,write,undo}_access(xact,bh);
341-
if ( myfs_modify(bh) ) { /* returns true
342-
if makes changes */
343-
journal_dirty_{meta,}data(xact,bh);
344-
} else {
345-
journal_forget(bh);
346-
}
347-
}
348-
journal_stop(xct);
349-
}
350-
journal_destroy(my_jrnl);
351-
</programlisting>
352308
</sect2>
353309

354310
</sect1>
@@ -357,13 +313,13 @@ an example.
357313
<title>Data Types</title>
358314
<para>
359315
The journalling layer uses typedefs to 'hide' the concrete definitions
360-
of the structures used. As a client of the JBD layer you can
316+
of the structures used. As a client of the JBD2 layer you can
361317
just rely on the using the pointer as a magic cookie of some sort.
362318

363319
Obviously the hiding is not enforced as this is 'C'.
364320
</para>
365321
<sect2 id="structures"><title>Structures</title>
366-
!Iinclude/linux/jbd.h
322+
!Iinclude/linux/jbd2.h
367323
</sect2>
368324
</sect1>
369325

@@ -375,11 +331,11 @@ an example.
375331
manage transactions
376332
</para>
377333
<sect2 id="journal_level"><title>Journal Level</title>
378-
!Efs/jbd/journal.c
379-
!Ifs/jbd/recovery.c
334+
!Efs/jbd2/journal.c
335+
!Ifs/jbd2/recovery.c
380336
</sect2>
381337
<sect2 id="transaction_level"><title>Transasction Level</title>
382-
!Efs/jbd/transaction.c
338+
!Efs/jbd2/transaction.c
383339
</sect2>
384340
</sect1>
385341
<sect1 id="see_also">

0 commit comments

Comments
 (0)