Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

Jan Kara 82ff50b2 acc84b05

+67 -111
+67 -111
Documentation/DocBook/filesystems.tmpl
··· 146 146 The journalling layer is easy to use. You need to 147 147 first of all create a journal_t data structure. There are 148 148 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 152 152 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 154 154 to free up any used kernel memory. 155 155 </para> 156 156 157 157 <para> 158 158 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(). 161 165 </para> 162 166 163 167 <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. 179 173 </para> 180 174 181 175 <para> ··· 183 189 is done by wrapping them into transactions. Additionally you 184 190 also need to wrap the modification of each of the buffers 185 191 with calls to the journal layer, so it knows what the modifications 186 - you are actually making are. To do this use journal_start() which 192 + you are actually making are. To do this use jbd2_journal_start() which 187 193 returns a transaction handle. 188 194 </para> 189 195 190 196 <para> 191 - journal_start() 192 - and its counterpart journal_stop(), which indicates the end of a transaction 193 - are nestable calls, so you can reenter a transaction if necessary, 194 - but remember you must call journal_stop() the same number of times as 195 - journal_start() before the transaction is completed (or more accurately 196 - leaves the update phase). Ext3/VFS makes use of this feature to simplify 197 - quota support. 197 + jbd2_journal_start() 198 + and its counterpart jbd2_journal_stop(), which indicates the end of a 199 + transaction are nestable calls, so you can reenter a transaction if necessary, 200 + but remember you must call jbd2_journal_stop() the same number of times as 201 + jbd2_journal_start() before the transaction is completed (or more accurately 202 + leaves the update phase). Ext4/VFS makes use of this feature to simplify 203 + handling of inode dirtying, quota support, etc. 198 204 </para> 199 205 200 206 <para> 201 207 Inside each transaction you need to wrap the modifications to the 202 208 individual buffers (blocks). Before you start to modify a buffer you 203 - need to call journal_get_{create,write,undo}_access() as appropriate, 209 + need to call jbd2_journal_get_{create,write,undo}_access() as appropriate, 204 210 this allows the journalling layer to copy the unmodified data if it 205 211 needs to. After all the buffer may be part of a previously uncommitted 206 212 transaction. 207 213 At this point you are at last ready to modify a buffer, and once 208 - you are have done so you need to call journal_dirty_{meta,}data(). 214 + you are have done so you need to call jbd2_journal_dirty_{meta,}data(). 209 215 Or if you've asked for access to a buffer you now know is now longer 210 - required to be pushed back on the device you can call journal_forget() 216 + required to be pushed back on the device you can call jbd2_journal_forget() 211 217 in much the same way as you might have used bforget() in the past. 212 218 </para> 213 219 214 220 <para> 215 - A journal_flush() may be called at any time to commit and checkpoint 221 + A jbd2_journal_flush() may be called at any time to commit and checkpoint 216 222 all your transactions. 217 223 </para> 218 224 219 225 <para> 220 - Then at umount time , in your put_super() you can then call journal_destroy() 226 + Then at umount time , in your put_super() you can then call jbd2_journal_destroy() 221 227 to clean up your in-core journal object. 222 228 </para> 223 229 ··· 225 231 Unfortunately there a couple of ways the journal layer can cause a deadlock. 226 232 The first thing to note is that each task can only have 227 233 a single outstanding transaction at any one time, remember nothing 228 - commits until the outermost journal_stop(). This means 234 + commits until the outermost jbd2_journal_stop(). This means 229 235 you must complete the transaction at the end of each file/inode/address 230 236 etc. operation you perform, so that the journalling system isn't re-entered 231 237 on another journal. Since transactions can't be nested/batched 232 238 across differing journals, and another filesystem other than 233 - yours (say ext3) may be modified in a later syscall. 239 + yours (say ext4) may be modified in a later syscall. 234 240 </para> 235 241 236 242 <para> 237 - The second case to bear in mind is that journal_start() can 243 + The second case to bear in mind is that jbd2_journal_start() can 238 244 block if there isn't enough space in the journal for your transaction 239 245 (based on the passed nblocks param) - when it blocks it merely(!) needs to 240 246 wait for transactions to complete and be committed from other tasks, 241 - so essentially we are waiting for journal_stop(). So to avoid 242 - deadlocks you must treat journal_start/stop() as if they 247 + so essentially we are waiting for jbd2_journal_stop(). So to avoid 248 + deadlocks you must treat jbd2_journal_start/stop() as if they 243 249 were semaphores and include them in your semaphore ordering rules to prevent 244 - deadlocks. Note that journal_extend() has similar blocking behaviour to 245 - journal_start() so you can deadlock here just as easily as on journal_start(). 250 + deadlocks. Note that jbd2_journal_extend() has similar blocking behaviour to 251 + jbd2_journal_start() so you can deadlock here just as easily as on 252 + jbd2_journal_start(). 246 253 </para> 247 254 248 255 <para> 249 256 Try to reserve the right number of blocks the first time. ;-). This will 250 257 be the maximum number of blocks you are going to touch in this transaction. 251 - I advise having a look at at least ext3_jbd.h to see the basis on which 252 - ext3 uses to make these decisions. 258 + I advise having a look at at least ext4_jbd.h to see the basis on which 259 + ext4 uses to make these decisions. 253 260 </para> 254 261 255 262 <para> 256 263 Another wriggle to watch out for is your on-disk block allocation strategy. 257 - why? Because, if you undo a delete, you need to ensure you haven't reused any 258 - of the freed blocks in a later transaction. One simple way of doing this 259 - is make sure any blocks you allocate only have checkpointed transactions 260 - listed against them. Ext3 does this in ext3_test_allocatable(). 264 + Why? Because, if you do a delete, you need to ensure you haven't reused any 265 + of the freed blocks until the transaction freeing these blocks commits. If you 266 + reused these blocks and crash happens, there is no way to restore the contents 267 + of the reallocated blocks at the end of the last fully committed transaction. 268 + 269 + One simple way of doing this is to mark blocks as free in internal in-memory 270 + block allocation structures only after the transaction freeing them commits. 271 + Ext4 uses journal commit callback for this purpose. 261 272 </para> 262 273 263 274 <para> 264 - Lock is also providing through journal_{un,}lock_updates(), 265 - ext3 uses this when it wants a window with a clean and stable fs for a moment. 266 - eg. 275 + With journal commit callbacks you can ask the journalling layer to call a 276 + callback function when the transaction is finally committed to disk, so that 277 + you can do some of your own management. You ask the journalling layer for 278 + calling the callback by simply setting journal->j_commit_callback function 279 + pointer and that function is called after each transaction commit. You can also 280 + use transaction->t_private_list for attaching entries to a transaction that 281 + need processing when the transaction commits. 282 + </para> 283 + 284 + <para> 285 + JBD2 also provides a way to block all transaction updates via 286 + jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a window with a 287 + clean and stable fs for a moment. E.g. 267 288 </para> 268 289 269 290 <programlisting> 270 291 271 - journal_lock_updates() //stop new stuff happening.. 272 - journal_flush() // checkpoint everything. 292 + jbd2_journal_lock_updates() //stop new stuff happening.. 293 + jbd2_journal_flush() // checkpoint everything. 273 294 ..do stuff on stable fs 274 - journal_unlock_updates() // carry on with filesystem use. 295 + jbd2_journal_unlock_updates() // carry on with filesystem use. 275 296 </programlisting> 276 297 277 298 <para> 278 299 The opportunities for abuse and DOS attacks with this should be obvious, 279 300 if you allow unprivileged userspace to trigger codepaths containing these 280 301 calls. 281 - </para> 282 - 283 - <para> 284 - A new feature of jbd since 2.5.25 is commit callbacks with the new 285 - journal_callback_set() function you can now ask the journalling layer 286 - to call you back when the transaction is finally committed to disk, so that 287 - you can do some of your own management. The key to this is the journal_callback 288 - struct, this maintains the internal callback information but you can 289 - extend it like this:- 290 - </para> 291 - <programlisting> 292 - struct myfs_callback_s { 293 - //Data structure element required by jbd.. 294 - struct journal_callback for_jbd; 295 - // Stuff for myfs allocated together. 296 - myfs_inode* i_commited; 297 - 298 - } 299 - </programlisting> 300 - 301 - <para> 302 - this would be useful if you needed to know when data was committed to a 303 - particular inode. 304 302 </para> 305 303 306 304 </sect2> ··· 305 319 to tell the journalling layer about them. 306 320 </para> 307 321 308 - <para> 309 - Here is a some pseudo code to give you an idea of how it works, as 310 - an example. 311 - </para> 312 - 313 - <programlisting> 314 - journal_t* my_jnrl = journal_create(); 315 - journal_init_{dev,inode}(jnrl,...) 316 - if (clean) journal_wipe(); 317 - journal_load(); 318 - 319 - foreach(transaction) { /*transactions must be 320 - completed before 321 - a syscall returns to 322 - userspace*/ 323 - 324 - handle_t * xct=journal_start(my_jnrl); 325 - foreach(bh) { 326 - journal_get_{create,write,undo}_access(xact,bh); 327 - if ( myfs_modify(bh) ) { /* returns true 328 - if makes changes */ 329 - journal_dirty_{meta,}data(xact,bh); 330 - } else { 331 - journal_forget(bh); 332 - } 333 - } 334 - journal_stop(xct); 335 - } 336 - journal_destroy(my_jrnl); 337 - </programlisting> 338 322 </sect2> 339 323 340 324 </sect1> ··· 313 357 <title>Data Types</title> 314 358 <para> 315 359 The journalling layer uses typedefs to 'hide' the concrete definitions 316 - of the structures used. As a client of the JBD layer you can 360 + of the structures used. As a client of the JBD2 layer you can 317 361 just rely on the using the pointer as a magic cookie of some sort. 318 362 319 363 Obviously the hiding is not enforced as this is 'C'. 320 364 </para> 321 365 <sect2 id="structures"><title>Structures</title> 322 - !Iinclude/linux/jbd.h 366 + !Iinclude/linux/jbd2.h 323 367 </sect2> 324 368 </sect1> 325 369 ··· 331 375 manage transactions 332 376 </para> 333 377 <sect2 id="journal_level"><title>Journal Level</title> 334 - !Efs/jbd/journal.c 335 - !Ifs/jbd/recovery.c 378 + !Efs/jbd2/journal.c 379 + !Ifs/jbd2/recovery.c 336 380 </sect2> 337 381 <sect2 id="transaction_level"><title>Transasction Level</title> 338 - !Efs/jbd/transaction.c 382 + !Efs/jbd2/transaction.c 339 383 </sect2> 340 384 </sect1> 341 385 <sect1 id="see_also">