aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Kara <jack@suse.com>2015-07-14 08:55:05 -0400
committerJan Kara <jack@suse.com>2015-07-23 14:59:39 -0400
commit82ff50b222d8ac645cdeba974c612c9eef01c3dd (patch)
treeff36126763b2315c15906b79fb85b3d532d82d56
parentacc84b05b1f463952a638689335ca47e70d5ea30 (diff)
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>
-rw-r--r--Documentation/DocBook/filesystems.tmpl178
1 files changed, 67 insertions, 111 deletions
diff --git a/Documentation/DocBook/filesystems.tmpl b/Documentation/DocBook/filesystems.tmpl
index bcdfdb9a9277..6006b6358c86 100644
--- a/Documentation/DocBook/filesystems.tmpl
+++ b/Documentation/DocBook/filesystems.tmpl
@@ -146,36 +146,30 @@
146The journalling layer is easy to use. You need to 146The journalling layer is easy to use. You need to
147first of all create a journal_t data structure. There are 147first of all create a journal_t data structure. There are
148two calls to do this dependent on how you decide to allocate the physical 148two calls to do this dependent on how you decide to allocate the physical
149media on which the journal resides. The journal_init_inode() call 149media on which the journal resides. The jbd2_journal_init_inode() call
150is for journals stored in filesystem inodes, or the journal_init_dev() 150is for journals stored in filesystem inodes, or the jbd2_journal_init_dev()
151call can be use for journal stored on a raw device (in a continuous range 151call can be used for journal stored on a raw device (in a continuous range
152of blocks). A journal_t is a typedef for a struct pointer, so when 152of blocks). A journal_t is a typedef for a struct pointer, so when
153you are finally finished make sure you call journal_destroy() on it 153you are finally finished make sure you call jbd2_journal_destroy() on it
154to free up any used kernel memory. 154to free up any used kernel memory.
155</para> 155</para>
156 156
157<para> 157<para>
158Once you have got your journal_t object you need to 'mount' or load the journal 158Once you have got your journal_t object you need to 'mount' or load the journal
159file, unless of course you haven't initialised it yet - in which case you 159file. The journalling layer expects the space for the journal was already
160need to call journal_create(). 160allocated and initialized properly by the userspace tools. When loading the
161journal you must call jbd2_journal_load() to process journal contents. If the
162client 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
164clear the journal contents before calling jbd2_journal_load().
161</para> 165</para>
162 166
163<para> 167<para>
164Most of the time however your journal file will already have been created, but 168Note that jbd2_journal_wipe(..,0) calls jbd2_journal_skip_recovery() for you if
165before you load it you must call journal_wipe() to empty the journal file. 169it detects any outstanding transactions in the journal and similarly
166Hang on, you say , what if the filesystem wasn't cleanly umount()'d . Well, it is the 170jbd2_journal_load() will call jbd2_journal_recover() if necessary. I would
167job of the client file system to detect this and skip the call to journal_wipe(). 171advise reading ext4_load_journal() in fs/ext4/super.c for examples on this
168</para> 172stage.
169
170<para>
171In either case the next call should be to journal_load() which prepares the
172journal file for use. Note that journal_wipe(..,0) calls journal_skip_recovery()
173for you if it detects any outstanding transactions in the journal and similarly
174journal_load() will call journal_recover() if necessary.
175I 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
177complicate the API. Or isn't a good idea for the journal layer to hide
178dirty mounts from the client fs]
179</para> 173</para>
180 174
181<para> 175<para>
@@ -189,41 +183,41 @@ You still need to actually journal your filesystem changes, this
189is done by wrapping them into transactions. Additionally you 183is done by wrapping them into transactions. Additionally you
190also need to wrap the modification of each of the buffers 184also need to wrap the modification of each of the buffers
191with calls to the journal layer, so it knows what the modifications 185with calls to the journal layer, so it knows what the modifications
192you are actually making are. To do this use journal_start() which 186you are actually making are. To do this use jbd2_journal_start() which
193returns a transaction handle. 187returns a transaction handle.
194</para> 188</para>
195 189
196<para> 190<para>
197journal_start() 191jbd2_journal_start()
198and its counterpart journal_stop(), which indicates the end of a transaction 192and its counterpart jbd2_journal_stop(), which indicates the end of a
199are nestable calls, so you can reenter a transaction if necessary, 193transaction are nestable calls, so you can reenter a transaction if necessary,
200but remember you must call journal_stop() the same number of times as 194but remember you must call jbd2_journal_stop() the same number of times as
201journal_start() before the transaction is completed (or more accurately 195jbd2_journal_start() before the transaction is completed (or more accurately
202leaves the update phase). Ext3/VFS makes use of this feature to simplify 196leaves the update phase). Ext4/VFS makes use of this feature to simplify
203quota support. 197handling of inode dirtying, quota support, etc.
204</para> 198</para>
205 199
206<para> 200<para>
207Inside each transaction you need to wrap the modifications to the 201Inside each transaction you need to wrap the modifications to the
208individual buffers (blocks). Before you start to modify a buffer you 202individual buffers (blocks). Before you start to modify a buffer you
209need to call journal_get_{create,write,undo}_access() as appropriate, 203need to call jbd2_journal_get_{create,write,undo}_access() as appropriate,
210this allows the journalling layer to copy the unmodified data if it 204this allows the journalling layer to copy the unmodified data if it
211needs to. After all the buffer may be part of a previously uncommitted 205needs to. After all the buffer may be part of a previously uncommitted
212transaction. 206transaction.
213At this point you are at last ready to modify a buffer, and once 207At this point you are at last ready to modify a buffer, and once
214you are have done so you need to call journal_dirty_{meta,}data(). 208you are have done so you need to call jbd2_journal_dirty_{meta,}data().
215Or if you've asked for access to a buffer you now know is now longer 209Or if you've asked for access to a buffer you now know is now longer
216required to be pushed back on the device you can call journal_forget() 210required to be pushed back on the device you can call jbd2_journal_forget()
217in much the same way as you might have used bforget() in the past. 211in much the same way as you might have used bforget() in the past.
218</para> 212</para>
219 213
220<para> 214<para>
221A journal_flush() may be called at any time to commit and checkpoint 215A jbd2_journal_flush() may be called at any time to commit and checkpoint
222all your transactions. 216all your transactions.
223</para> 217</para>
224 218
225<para> 219<para>
226Then at umount time , in your put_super() you can then call journal_destroy() 220Then at umount time , in your put_super() you can then call jbd2_journal_destroy()
227to clean up your in-core journal object. 221to clean up your in-core journal object.
228</para> 222</para>
229 223
@@ -231,53 +225,68 @@ to clean up your in-core journal object.
231Unfortunately there a couple of ways the journal layer can cause a deadlock. 225Unfortunately there a couple of ways the journal layer can cause a deadlock.
232The first thing to note is that each task can only have 226The first thing to note is that each task can only have
233a single outstanding transaction at any one time, remember nothing 227a single outstanding transaction at any one time, remember nothing
234commits until the outermost journal_stop(). This means 228commits until the outermost jbd2_journal_stop(). This means
235you must complete the transaction at the end of each file/inode/address 229you must complete the transaction at the end of each file/inode/address
236etc. operation you perform, so that the journalling system isn't re-entered 230etc. operation you perform, so that the journalling system isn't re-entered
237on another journal. Since transactions can't be nested/batched 231on another journal. Since transactions can't be nested/batched
238across differing journals, and another filesystem other than 232across differing journals, and another filesystem other than
239yours (say ext3) may be modified in a later syscall. 233yours (say ext4) may be modified in a later syscall.
240</para> 234</para>
241 235
242<para> 236<para>
243The second case to bear in mind is that journal_start() can 237The second case to bear in mind is that jbd2_journal_start() can
244block if there isn't enough space in the journal for your transaction 238block if there isn't enough space in the journal for your transaction
245(based on the passed nblocks param) - when it blocks it merely(!) needs to 239(based on the passed nblocks param) - when it blocks it merely(!) needs to
246wait for transactions to complete and be committed from other tasks, 240wait for transactions to complete and be committed from other tasks,
247so essentially we are waiting for journal_stop(). So to avoid 241so essentially we are waiting for jbd2_journal_stop(). So to avoid
248deadlocks you must treat journal_start/stop() as if they 242deadlocks you must treat jbd2_journal_start/stop() as if they
249were semaphores and include them in your semaphore ordering rules to prevent 243were semaphores and include them in your semaphore ordering rules to prevent
250deadlocks. Note that journal_extend() has similar blocking behaviour to 244deadlocks. Note that jbd2_journal_extend() has similar blocking behaviour to
251journal_start() so you can deadlock here just as easily as on journal_start(). 245jbd2_journal_start() so you can deadlock here just as easily as on
246jbd2_journal_start().
252</para> 247</para>
253 248
254<para> 249<para>
255Try to reserve the right number of blocks the first time. ;-). This will 250Try to reserve the right number of blocks the first time. ;-). This will
256be the maximum number of blocks you are going to touch in this transaction. 251be the maximum number of blocks you are going to touch in this transaction.
257I advise having a look at at least ext3_jbd.h to see the basis on which 252I advise having a look at at least ext4_jbd.h to see the basis on which
258ext3 uses to make these decisions. 253ext4 uses to make these decisions.
259</para> 254</para>
260 255
261<para> 256<para>
262Another wriggle to watch out for is your on-disk block allocation strategy. 257Another wriggle to watch out for is your on-disk block allocation strategy.
263why? Because, if you undo a delete, you need to ensure you haven't reused any 258Why? Because, if you do a delete, you need to ensure you haven't reused any
264of the freed blocks in a later transaction. One simple way of doing this 259of the freed blocks until the transaction freeing these blocks commits. If you
265is make sure any blocks you allocate only have checkpointed transactions 260reused these blocks and crash happens, there is no way to restore the contents
266listed against them. Ext3 does this in ext3_test_allocatable(). 261of the reallocated blocks at the end of the last fully committed transaction.
262
263One simple way of doing this is to mark blocks as free in internal in-memory
264block allocation structures only after the transaction freeing them commits.
265Ext4 uses journal commit callback for this purpose.
266</para>
267
268<para>
269With journal commit callbacks you can ask the journalling layer to call a
270callback function when the transaction is finally committed to disk, so that
271you can do some of your own management. You ask the journalling layer for
272calling the callback by simply setting journal->j_commit_callback function
273pointer and that function is called after each transaction commit. You can also
274use transaction->t_private_list for attaching entries to a transaction that
275need processing when the transaction commits.
267</para> 276</para>
268 277
269<para> 278<para>
270Lock is also providing through journal_{un,}lock_updates(), 279JBD2 also provides a way to block all transaction updates via
271ext3 uses this when it wants a window with a clean and stable fs for a moment. 280jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a window with a
272eg. 281clean and stable fs for a moment. E.g.
273</para> 282</para>
274 283
275<programlisting> 284<programlisting>
276 285
277 journal_lock_updates() //stop new stuff happening.. 286 jbd2_journal_lock_updates() //stop new stuff happening..
278 journal_flush() // checkpoint everything. 287 jbd2_journal_flush() // checkpoint everything.
279 ..do stuff on stable fs 288 ..do stuff on stable fs
280 journal_unlock_updates() // carry on with filesystem use. 289 jbd2_journal_unlock_updates() // carry on with filesystem use.
281</programlisting> 290</programlisting>
282 291
283<para> 292<para>
@@ -286,29 +295,6 @@ if you allow unprivileged userspace to trigger codepaths containing these
286calls. 295calls.
287</para> 296</para>
288 297
289<para>
290A new feature of jbd since 2.5.25 is commit callbacks with the new
291journal_callback_set() function you can now ask the journalling layer
292to call you back when the transaction is finally committed to disk, so that
293you can do some of your own management. The key to this is the journal_callback
294struct, this maintains the internal callback information but you can
295extend 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>
308this would be useful if you needed to know when data was committed to a
309particular inode.
310</para>
311
312 </sect2> 298 </sect2>
313 299
314 <sect2 id="jbd_summary"> 300 <sect2 id="jbd_summary">
@@ -319,36 +305,6 @@ being each mount, each modification (transaction) and each changed buffer
319to tell the journalling layer about them. 305to tell the journalling layer about them.
320</para> 306</para>
321 307
322<para>
323Here is a some pseudo code to give you an idea of how it works, as
324an 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>
352 </sect2> 308 </sect2>
353 309
354 </sect1> 310 </sect1>
@@ -357,13 +313,13 @@ an example.
357 <title>Data Types</title> 313 <title>Data Types</title>
358 <para> 314 <para>
359 The journalling layer uses typedefs to 'hide' the concrete definitions 315 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
361 just rely on the using the pointer as a magic cookie of some sort. 317 just rely on the using the pointer as a magic cookie of some sort.
362 318
363 Obviously the hiding is not enforced as this is 'C'. 319 Obviously the hiding is not enforced as this is 'C'.
364 </para> 320 </para>
365 <sect2 id="structures"><title>Structures</title> 321 <sect2 id="structures"><title>Structures</title>
366!Iinclude/linux/jbd.h 322!Iinclude/linux/jbd2.h
367 </sect2> 323 </sect2>
368 </sect1> 324 </sect1>
369 325
@@ -375,11 +331,11 @@ an example.
375 manage transactions 331 manage transactions
376 </para> 332 </para>
377 <sect2 id="journal_level"><title>Journal Level</title> 333 <sect2 id="journal_level"><title>Journal Level</title>
378!Efs/jbd/journal.c 334!Efs/jbd2/journal.c
379!Ifs/jbd/recovery.c 335!Ifs/jbd2/recovery.c
380 </sect2> 336 </sect2>
381 <sect2 id="transaction_level"><title>Transasction Level</title> 337 <sect2 id="transaction_level"><title>Transasction Level</title>
382!Efs/jbd/transaction.c 338!Efs/jbd2/transaction.c
383 </sect2> 339 </sect2>
384 </sect1> 340 </sect1>
385 <sect1 id="see_also"> 341 <sect1 id="see_also">