]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/jbd2/journal.c
ext4: expand commit callback and
[mirror_ubuntu-bionic-kernel.git] / fs / jbd2 / journal.c
CommitLineData
470decc6 1/*
f7f4bccb 2 * linux/fs/jbd2/journal.c
470decc6
DK
3 *
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
5 *
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
7 *
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
11 *
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
14 *
15 * This file manages journals: areas of disk reserved for logging
16 * transactional updates. This includes the kernel journaling thread
17 * which is responsible for scheduling updates to the log.
18 *
19 * We do not actually manage the physical storage of the journal in this
20 * file: that is left to a per-journal policy function, which allows us
21 * to store the journal within a filesystem-specified area for ext2
22 * journaling (ext2 can use a reserved inode for storing the log).
23 */
24
25#include <linux/module.h>
26#include <linux/time.h>
27#include <linux/fs.h>
f7f4bccb 28#include <linux/jbd2.h>
470decc6
DK
29#include <linux/errno.h>
30#include <linux/slab.h>
470decc6
DK
31#include <linux/init.h>
32#include <linux/mm.h>
7dfb7103 33#include <linux/freezer.h>
470decc6
DK
34#include <linux/pagemap.h>
35#include <linux/kthread.h>
36#include <linux/poison.h>
37#include <linux/proc_fs.h>
0f49d5d0 38#include <linux/debugfs.h>
8e85fb3f 39#include <linux/seq_file.h>
c225aa57 40#include <linux/math64.h>
879c5e6b 41#include <linux/hash.h>
d2eecb03
TT
42#include <linux/log2.h>
43#include <linux/vmalloc.h>
47def826 44#include <linux/backing-dev.h>
39e3ac25 45#include <linux/bitops.h>
670be5a7 46#include <linux/ratelimit.h>
879c5e6b
TT
47
48#define CREATE_TRACE_POINTS
49#include <trace/events/jbd2.h>
470decc6
DK
50
51#include <asm/uaccess.h>
52#include <asm/page.h>
39e3ac25 53#include <asm/system.h>
470decc6 54
f7f4bccb
MC
55EXPORT_SYMBOL(jbd2_journal_extend);
56EXPORT_SYMBOL(jbd2_journal_stop);
57EXPORT_SYMBOL(jbd2_journal_lock_updates);
58EXPORT_SYMBOL(jbd2_journal_unlock_updates);
59EXPORT_SYMBOL(jbd2_journal_get_write_access);
60EXPORT_SYMBOL(jbd2_journal_get_create_access);
61EXPORT_SYMBOL(jbd2_journal_get_undo_access);
e06c8227 62EXPORT_SYMBOL(jbd2_journal_set_triggers);
f7f4bccb
MC
63EXPORT_SYMBOL(jbd2_journal_dirty_metadata);
64EXPORT_SYMBOL(jbd2_journal_release_buffer);
65EXPORT_SYMBOL(jbd2_journal_forget);
470decc6
DK
66#if 0
67EXPORT_SYMBOL(journal_sync_buffer);
68#endif
f7f4bccb
MC
69EXPORT_SYMBOL(jbd2_journal_flush);
70EXPORT_SYMBOL(jbd2_journal_revoke);
71
72EXPORT_SYMBOL(jbd2_journal_init_dev);
73EXPORT_SYMBOL(jbd2_journal_init_inode);
74EXPORT_SYMBOL(jbd2_journal_update_format);
75EXPORT_SYMBOL(jbd2_journal_check_used_features);
76EXPORT_SYMBOL(jbd2_journal_check_available_features);
77EXPORT_SYMBOL(jbd2_journal_set_features);
f7f4bccb
MC
78EXPORT_SYMBOL(jbd2_journal_load);
79EXPORT_SYMBOL(jbd2_journal_destroy);
f7f4bccb
MC
80EXPORT_SYMBOL(jbd2_journal_abort);
81EXPORT_SYMBOL(jbd2_journal_errno);
82EXPORT_SYMBOL(jbd2_journal_ack_err);
83EXPORT_SYMBOL(jbd2_journal_clear_err);
84EXPORT_SYMBOL(jbd2_log_wait_commit);
3b799d15 85EXPORT_SYMBOL(jbd2_log_start_commit);
f7f4bccb
MC
86EXPORT_SYMBOL(jbd2_journal_start_commit);
87EXPORT_SYMBOL(jbd2_journal_force_commit_nested);
88EXPORT_SYMBOL(jbd2_journal_wipe);
89EXPORT_SYMBOL(jbd2_journal_blocks_per_page);
90EXPORT_SYMBOL(jbd2_journal_invalidatepage);
91EXPORT_SYMBOL(jbd2_journal_try_to_free_buffers);
92EXPORT_SYMBOL(jbd2_journal_force_commit);
c851ed54
JK
93EXPORT_SYMBOL(jbd2_journal_file_inode);
94EXPORT_SYMBOL(jbd2_journal_init_jbd_inode);
95EXPORT_SYMBOL(jbd2_journal_release_jbd_inode);
96EXPORT_SYMBOL(jbd2_journal_begin_ordered_truncate);
8aefcd55 97EXPORT_SYMBOL(jbd2_inode_cache);
470decc6
DK
98
99static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
100static void __journal_abort_soft (journal_t *journal, int errno);
d2eecb03 101static int jbd2_journal_create_slab(size_t slab_size);
470decc6
DK
102
103/*
104 * Helper function used to manage commit timeouts
105 */
106
107static void commit_timeout(unsigned long __data)
108{
109 struct task_struct * p = (struct task_struct *) __data;
110
111 wake_up_process(p);
112}
113
114/*
f7f4bccb 115 * kjournald2: The main thread function used to manage a logging device
470decc6
DK
116 * journal.
117 *
118 * This kernel thread is responsible for two things:
119 *
120 * 1) COMMIT: Every so often we need to commit the current state of the
121 * filesystem to disk. The journal thread is responsible for writing
122 * all of the metadata buffers to disk.
123 *
124 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
125 * of the data in that part of the log has been rewritten elsewhere on
126 * the disk. Flushing these old buffers to reclaim space in the log is
127 * known as checkpointing, and this thread is responsible for that job.
128 */
129
f7f4bccb 130static int kjournald2(void *arg)
470decc6
DK
131{
132 journal_t *journal = arg;
133 transaction_t *transaction;
134
135 /*
136 * Set up an interval timer which can be used to trigger a commit wakeup
137 * after the commit interval expires
138 */
139 setup_timer(&journal->j_commit_timer, commit_timeout,
140 (unsigned long)current);
141
142 /* Record that the journal thread is running */
143 journal->j_task = current;
144 wake_up(&journal->j_wait_done_commit);
145
470decc6
DK
146 /*
147 * And now, wait forever for commit wakeup events.
148 */
a931da6a 149 write_lock(&journal->j_state_lock);
470decc6
DK
150
151loop:
f7f4bccb 152 if (journal->j_flags & JBD2_UNMOUNT)
470decc6
DK
153 goto end_loop;
154
155 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
156 journal->j_commit_sequence, journal->j_commit_request);
157
158 if (journal->j_commit_sequence != journal->j_commit_request) {
159 jbd_debug(1, "OK, requests differ\n");
a931da6a 160 write_unlock(&journal->j_state_lock);
470decc6 161 del_timer_sync(&journal->j_commit_timer);
f7f4bccb 162 jbd2_journal_commit_transaction(journal);
a931da6a 163 write_lock(&journal->j_state_lock);
470decc6
DK
164 goto loop;
165 }
166
167 wake_up(&journal->j_wait_done_commit);
168 if (freezing(current)) {
169 /*
170 * The simpler the better. Flushing journal isn't a
171 * good idea, because that depends on threads that may
172 * be already stopped.
173 */
f7f4bccb 174 jbd_debug(1, "Now suspending kjournald2\n");
a931da6a 175 write_unlock(&journal->j_state_lock);
a0acae0e 176 try_to_freeze();
a931da6a 177 write_lock(&journal->j_state_lock);
470decc6
DK
178 } else {
179 /*
180 * We assume on resume that commits are already there,
181 * so we don't sleep
182 */
183 DEFINE_WAIT(wait);
184 int should_sleep = 1;
185
186 prepare_to_wait(&journal->j_wait_commit, &wait,
187 TASK_INTERRUPTIBLE);
188 if (journal->j_commit_sequence != journal->j_commit_request)
189 should_sleep = 0;
190 transaction = journal->j_running_transaction;
191 if (transaction && time_after_eq(jiffies,
192 transaction->t_expires))
193 should_sleep = 0;
f7f4bccb 194 if (journal->j_flags & JBD2_UNMOUNT)
470decc6
DK
195 should_sleep = 0;
196 if (should_sleep) {
a931da6a 197 write_unlock(&journal->j_state_lock);
470decc6 198 schedule();
a931da6a 199 write_lock(&journal->j_state_lock);
470decc6
DK
200 }
201 finish_wait(&journal->j_wait_commit, &wait);
202 }
203
f7f4bccb 204 jbd_debug(1, "kjournald2 wakes\n");
470decc6
DK
205
206 /*
207 * Were we woken up by a commit wakeup event?
208 */
209 transaction = journal->j_running_transaction;
210 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
211 journal->j_commit_request = transaction->t_tid;
212 jbd_debug(1, "woke because of timeout\n");
213 }
214 goto loop;
215
216end_loop:
a931da6a 217 write_unlock(&journal->j_state_lock);
470decc6
DK
218 del_timer_sync(&journal->j_commit_timer);
219 journal->j_task = NULL;
220 wake_up(&journal->j_wait_done_commit);
221 jbd_debug(1, "Journal thread exiting.\n");
222 return 0;
223}
224
97f06784 225static int jbd2_journal_start_thread(journal_t *journal)
470decc6 226{
97f06784
PE
227 struct task_struct *t;
228
90576c0b
TT
229 t = kthread_run(kjournald2, journal, "jbd2/%s",
230 journal->j_devname);
97f06784
PE
231 if (IS_ERR(t))
232 return PTR_ERR(t);
233
1076d17a 234 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
97f06784 235 return 0;
470decc6
DK
236}
237
238static void journal_kill_thread(journal_t *journal)
239{
a931da6a 240 write_lock(&journal->j_state_lock);
f7f4bccb 241 journal->j_flags |= JBD2_UNMOUNT;
470decc6
DK
242
243 while (journal->j_task) {
244 wake_up(&journal->j_wait_commit);
a931da6a 245 write_unlock(&journal->j_state_lock);
1076d17a 246 wait_event(journal->j_wait_done_commit, journal->j_task == NULL);
a931da6a 247 write_lock(&journal->j_state_lock);
470decc6 248 }
a931da6a 249 write_unlock(&journal->j_state_lock);
470decc6
DK
250}
251
252/*
f7f4bccb 253 * jbd2_journal_write_metadata_buffer: write a metadata buffer to the journal.
470decc6
DK
254 *
255 * Writes a metadata buffer to a given disk block. The actual IO is not
256 * performed but a new buffer_head is constructed which labels the data
257 * to be written with the correct destination disk block.
258 *
259 * Any magic-number escaping which needs to be done will cause a
260 * copy-out here. If the buffer happens to start with the
f7f4bccb 261 * JBD2_MAGIC_NUMBER, then we can't write it to the log directly: the
470decc6
DK
262 * magic number is only written to the log for descripter blocks. In
263 * this case, we copy the data and replace the first word with 0, and we
264 * return a result code which indicates that this buffer needs to be
265 * marked as an escaped buffer in the corresponding log descriptor
266 * block. The missing word can then be restored when the block is read
267 * during recovery.
268 *
269 * If the source buffer has already been modified by a new transaction
270 * since we took the last commit snapshot, we use the frozen copy of
271 * that data for IO. If we end up using the existing buffer_head's data
272 * for the write, then we *have* to lock the buffer to prevent anyone
273 * else from using and possibly modifying it while the IO is in
274 * progress.
275 *
276 * The function returns a pointer to the buffer_heads to be used for IO.
277 *
278 * We assume that the journal has already been locked in this function.
279 *
280 * Return value:
281 * <0: Error
282 * >=0: Finished OK
283 *
284 * On success:
285 * Bit 0 set == escape performed on the data
286 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
287 */
288
f7f4bccb 289int jbd2_journal_write_metadata_buffer(transaction_t *transaction,
470decc6
DK
290 struct journal_head *jh_in,
291 struct journal_head **jh_out,
18eba7aa 292 unsigned long long blocknr)
470decc6
DK
293{
294 int need_copy_out = 0;
295 int done_copy_out = 0;
296 int do_escape = 0;
297 char *mapped_data;
298 struct buffer_head *new_bh;
299 struct journal_head *new_jh;
300 struct page *new_page;
301 unsigned int new_offset;
302 struct buffer_head *bh_in = jh2bh(jh_in);
96577c43 303 journal_t *journal = transaction->t_journal;
470decc6
DK
304
305 /*
306 * The buffer really shouldn't be locked: only the current committing
307 * transaction is allowed to write it, so nobody else is allowed
308 * to do any IO.
309 *
310 * akpm: except if we're journalling data, and write() output is
311 * also part of a shared mapping, and another thread has
312 * decided to launch a writepage() against this buffer.
313 */
314 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
315
47def826
TT
316retry_alloc:
317 new_bh = alloc_buffer_head(GFP_NOFS);
318 if (!new_bh) {
319 /*
320 * Failure is not an option, but __GFP_NOFAIL is going
321 * away; so we retry ourselves here.
322 */
323 congestion_wait(BLK_RW_ASYNC, HZ/50);
324 goto retry_alloc;
325 }
326
96577c43 327 /* keep subsequent assertions sane */
328 new_bh->b_state = 0;
329 init_buffer(new_bh, NULL, NULL);
330 atomic_set(&new_bh->b_count, 1);
331 new_jh = jbd2_journal_add_journal_head(new_bh); /* This sleeps */
470decc6
DK
332
333 /*
334 * If a new transaction has already done a buffer copy-out, then
335 * we use that version of the data for the commit.
336 */
337 jbd_lock_bh_state(bh_in);
338repeat:
339 if (jh_in->b_frozen_data) {
340 done_copy_out = 1;
341 new_page = virt_to_page(jh_in->b_frozen_data);
342 new_offset = offset_in_page(jh_in->b_frozen_data);
343 } else {
344 new_page = jh2bh(jh_in)->b_page;
345 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
346 }
347
348 mapped_data = kmap_atomic(new_page, KM_USER0);
e06c8227 349 /*
13ceef09
JK
350 * Fire data frozen trigger if data already wasn't frozen. Do this
351 * before checking for escaping, as the trigger may modify the magic
352 * offset. If a copy-out happens afterwards, it will have the correct
353 * data in the buffer.
e06c8227 354 */
13ceef09
JK
355 if (!done_copy_out)
356 jbd2_buffer_frozen_trigger(jh_in, mapped_data + new_offset,
357 jh_in->b_triggers);
e06c8227 358
470decc6
DK
359 /*
360 * Check for escaping
361 */
362 if (*((__be32 *)(mapped_data + new_offset)) ==
f7f4bccb 363 cpu_to_be32(JBD2_MAGIC_NUMBER)) {
470decc6
DK
364 need_copy_out = 1;
365 do_escape = 1;
366 }
367 kunmap_atomic(mapped_data, KM_USER0);
368
369 /*
370 * Do we need to do a data copy?
371 */
372 if (need_copy_out && !done_copy_out) {
373 char *tmp;
374
375 jbd_unlock_bh_state(bh_in);
af1e76d6 376 tmp = jbd2_alloc(bh_in->b_size, GFP_NOFS);
e6ec116b
TT
377 if (!tmp) {
378 jbd2_journal_put_journal_head(new_jh);
379 return -ENOMEM;
380 }
470decc6
DK
381 jbd_lock_bh_state(bh_in);
382 if (jh_in->b_frozen_data) {
af1e76d6 383 jbd2_free(tmp, bh_in->b_size);
470decc6
DK
384 goto repeat;
385 }
386
387 jh_in->b_frozen_data = tmp;
388 mapped_data = kmap_atomic(new_page, KM_USER0);
389 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
390 kunmap_atomic(mapped_data, KM_USER0);
391
392 new_page = virt_to_page(tmp);
393 new_offset = offset_in_page(tmp);
394 done_copy_out = 1;
e06c8227
JB
395
396 /*
397 * This isn't strictly necessary, as we're using frozen
398 * data for the escaping, but it keeps consistency with
399 * b_frozen_data usage.
400 */
401 jh_in->b_frozen_triggers = jh_in->b_triggers;
470decc6
DK
402 }
403
404 /*
405 * Did we need to do an escaping? Now we've done all the
406 * copying, we can finally do so.
407 */
408 if (do_escape) {
409 mapped_data = kmap_atomic(new_page, KM_USER0);
410 *((unsigned int *)(mapped_data + new_offset)) = 0;
411 kunmap_atomic(mapped_data, KM_USER0);
412 }
413
470decc6
DK
414 set_bh_page(new_bh, new_page, new_offset);
415 new_jh->b_transaction = NULL;
416 new_bh->b_size = jh2bh(jh_in)->b_size;
417 new_bh->b_bdev = transaction->t_journal->j_dev;
418 new_bh->b_blocknr = blocknr;
419 set_buffer_mapped(new_bh);
420 set_buffer_dirty(new_bh);
421
422 *jh_out = new_jh;
423
424 /*
425 * The to-be-written buffer needs to get moved to the io queue,
426 * and the original buffer whose contents we are shadowing or
427 * copying is moved to the transaction's shadow queue.
428 */
429 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
96577c43 430 spin_lock(&journal->j_list_lock);
431 __jbd2_journal_file_buffer(jh_in, transaction, BJ_Shadow);
432 spin_unlock(&journal->j_list_lock);
433 jbd_unlock_bh_state(bh_in);
434
470decc6 435 JBUFFER_TRACE(new_jh, "file as BJ_IO");
f7f4bccb 436 jbd2_journal_file_buffer(new_jh, transaction, BJ_IO);
470decc6
DK
437
438 return do_escape | (done_copy_out << 1);
439}
440
441/*
442 * Allocation code for the journal file. Manage the space left in the
443 * journal, so that we can begin checkpointing when appropriate.
444 */
445
446/*
f7f4bccb 447 * __jbd2_log_space_left: Return the number of free blocks left in the journal.
470decc6
DK
448 *
449 * Called with the journal already locked.
450 *
451 * Called under j_state_lock
452 */
453
f7f4bccb 454int __jbd2_log_space_left(journal_t *journal)
470decc6
DK
455{
456 int left = journal->j_free;
457
a931da6a 458 /* assert_spin_locked(&journal->j_state_lock); */
470decc6
DK
459
460 /*
461 * Be pessimistic here about the number of those free blocks which
462 * might be required for log descriptor control blocks.
463 */
464
465#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
466
467 left -= MIN_LOG_RESERVED_BLOCKS;
468
469 if (left <= 0)
470 return 0;
471 left -= (left >> 3);
472 return left;
473}
474
475/*
e4471831
TT
476 * Called with j_state_lock locked for writing.
477 * Returns true if a transaction commit was started.
470decc6 478 */
f7f4bccb 479int __jbd2_log_start_commit(journal_t *journal, tid_t target)
470decc6
DK
480{
481 /*
deeeaf13
TT
482 * The only transaction we can possibly wait upon is the
483 * currently running transaction (if it exists). Otherwise,
484 * the target tid must be an old one.
470decc6 485 */
deeeaf13
TT
486 if (journal->j_running_transaction &&
487 journal->j_running_transaction->t_tid == target) {
470decc6 488 /*
bcf3d0bc 489 * We want a new commit: OK, mark the request and wakeup the
470decc6
DK
490 * commit thread. We do _not_ do the commit ourselves.
491 */
492
493 journal->j_commit_request = target;
f2a44523 494 jbd_debug(1, "JBD2: requesting commit %d/%d\n",
470decc6
DK
495 journal->j_commit_request,
496 journal->j_commit_sequence);
497 wake_up(&journal->j_wait_commit);
498 return 1;
deeeaf13
TT
499 } else if (!tid_geq(journal->j_commit_request, target))
500 /* This should never happen, but if it does, preserve
501 the evidence before kjournald goes into a loop and
502 increments j_commit_sequence beyond all recognition. */
f2a44523 503 WARN_ONCE(1, "JBD2: bad log_start_commit: %u %u %u %u\n",
1be2add6
TT
504 journal->j_commit_request,
505 journal->j_commit_sequence,
506 target, journal->j_running_transaction ?
507 journal->j_running_transaction->t_tid : 0);
470decc6
DK
508 return 0;
509}
510
f7f4bccb 511int jbd2_log_start_commit(journal_t *journal, tid_t tid)
470decc6
DK
512{
513 int ret;
514
a931da6a 515 write_lock(&journal->j_state_lock);
f7f4bccb 516 ret = __jbd2_log_start_commit(journal, tid);
a931da6a 517 write_unlock(&journal->j_state_lock);
470decc6
DK
518 return ret;
519}
520
521/*
522 * Force and wait upon a commit if the calling process is not within
523 * transaction. This is used for forcing out undo-protected data which contains
524 * bitmaps, when the fs is running out of space.
525 *
526 * We can only force the running transaction if we don't have an active handle;
527 * otherwise, we will deadlock.
528 *
529 * Returns true if a transaction was started.
530 */
f7f4bccb 531int jbd2_journal_force_commit_nested(journal_t *journal)
470decc6
DK
532{
533 transaction_t *transaction = NULL;
534 tid_t tid;
e4471831 535 int need_to_start = 0;
470decc6 536
a931da6a 537 read_lock(&journal->j_state_lock);
470decc6
DK
538 if (journal->j_running_transaction && !current->journal_info) {
539 transaction = journal->j_running_transaction;
e4471831
TT
540 if (!tid_geq(journal->j_commit_request, transaction->t_tid))
541 need_to_start = 1;
470decc6
DK
542 } else if (journal->j_committing_transaction)
543 transaction = journal->j_committing_transaction;
544
545 if (!transaction) {
a931da6a 546 read_unlock(&journal->j_state_lock);
470decc6
DK
547 return 0; /* Nothing to retry */
548 }
549
550 tid = transaction->t_tid;
a931da6a 551 read_unlock(&journal->j_state_lock);
e4471831
TT
552 if (need_to_start)
553 jbd2_log_start_commit(journal, tid);
f7f4bccb 554 jbd2_log_wait_commit(journal, tid);
470decc6
DK
555 return 1;
556}
557
558/*
559 * Start a commit of the current running transaction (if any). Returns true
c88ccea3
JK
560 * if a transaction is going to be committed (or is currently already
561 * committing), and fills its tid in at *ptid
470decc6 562 */
f7f4bccb 563int jbd2_journal_start_commit(journal_t *journal, tid_t *ptid)
470decc6
DK
564{
565 int ret = 0;
566
a931da6a 567 write_lock(&journal->j_state_lock);
470decc6
DK
568 if (journal->j_running_transaction) {
569 tid_t tid = journal->j_running_transaction->t_tid;
570
c88ccea3
JK
571 __jbd2_log_start_commit(journal, tid);
572 /* There's a running transaction and we've just made sure
573 * it's commit has been scheduled. */
574 if (ptid)
470decc6 575 *ptid = tid;
c88ccea3
JK
576 ret = 1;
577 } else if (journal->j_committing_transaction) {
470decc6
DK
578 /*
579 * If ext3_write_super() recently started a commit, then we
580 * have to wait for completion of that transaction
581 */
c88ccea3
JK
582 if (ptid)
583 *ptid = journal->j_committing_transaction->t_tid;
470decc6
DK
584 ret = 1;
585 }
a931da6a 586 write_unlock(&journal->j_state_lock);
470decc6
DK
587 return ret;
588}
589
bbd2be36
JK
590/*
591 * Return 1 if a given transaction has not yet sent barrier request
592 * connected with a transaction commit. If 0 is returned, transaction
593 * may or may not have sent the barrier. Used to avoid sending barrier
594 * twice in common cases.
595 */
596int jbd2_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
597{
598 int ret = 0;
599 transaction_t *commit_trans;
600
601 if (!(journal->j_flags & JBD2_BARRIER))
602 return 0;
603 read_lock(&journal->j_state_lock);
604 /* Transaction already committed? */
605 if (tid_geq(journal->j_commit_sequence, tid))
606 goto out;
607 commit_trans = journal->j_committing_transaction;
608 if (!commit_trans || commit_trans->t_tid != tid) {
609 ret = 1;
610 goto out;
611 }
612 /*
613 * Transaction is being committed and we already proceeded to
614 * submitting a flush to fs partition?
615 */
616 if (journal->j_fs_dev != journal->j_dev) {
617 if (!commit_trans->t_need_data_flush ||
618 commit_trans->t_state >= T_COMMIT_DFLUSH)
619 goto out;
620 } else {
621 if (commit_trans->t_state >= T_COMMIT_JFLUSH)
622 goto out;
623 }
624 ret = 1;
625out:
626 read_unlock(&journal->j_state_lock);
627 return ret;
628}
629EXPORT_SYMBOL(jbd2_trans_will_send_data_barrier);
630
470decc6
DK
631/*
632 * Wait for a specified commit to complete.
633 * The caller may not hold the journal lock.
634 */
f7f4bccb 635int jbd2_log_wait_commit(journal_t *journal, tid_t tid)
470decc6
DK
636{
637 int err = 0;
638
a931da6a 639 read_lock(&journal->j_state_lock);
e23291b9 640#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
641 if (!tid_geq(journal->j_commit_request, tid)) {
642 printk(KERN_EMERG
643 "%s: error: j_commit_request=%d, tid=%d\n",
329d291f 644 __func__, journal->j_commit_request, tid);
470decc6 645 }
470decc6 646#endif
470decc6 647 while (tid_gt(tid, journal->j_commit_sequence)) {
f2a44523 648 jbd_debug(1, "JBD2: want %d, j_commit_sequence=%d\n",
470decc6
DK
649 tid, journal->j_commit_sequence);
650 wake_up(&journal->j_wait_commit);
a931da6a 651 read_unlock(&journal->j_state_lock);
470decc6
DK
652 wait_event(journal->j_wait_done_commit,
653 !tid_gt(tid, journal->j_commit_sequence));
a931da6a 654 read_lock(&journal->j_state_lock);
470decc6 655 }
a931da6a 656 read_unlock(&journal->j_state_lock);
470decc6
DK
657
658 if (unlikely(is_journal_aborted(journal))) {
659 printk(KERN_EMERG "journal commit I/O error\n");
660 err = -EIO;
661 }
662 return err;
663}
664
665/*
666 * Log buffer allocation routines:
667 */
668
18eba7aa 669int jbd2_journal_next_log_block(journal_t *journal, unsigned long long *retp)
470decc6
DK
670{
671 unsigned long blocknr;
672
a931da6a 673 write_lock(&journal->j_state_lock);
470decc6
DK
674 J_ASSERT(journal->j_free > 1);
675
676 blocknr = journal->j_head;
677 journal->j_head++;
678 journal->j_free--;
679 if (journal->j_head == journal->j_last)
680 journal->j_head = journal->j_first;
a931da6a 681 write_unlock(&journal->j_state_lock);
f7f4bccb 682 return jbd2_journal_bmap(journal, blocknr, retp);
470decc6
DK
683}
684
685/*
686 * Conversion of logical to physical block numbers for the journal
687 *
688 * On external journals the journal blocks are identity-mapped, so
689 * this is a no-op. If needed, we can use j_blk_offset - everything is
690 * ready.
691 */
f7f4bccb 692int jbd2_journal_bmap(journal_t *journal, unsigned long blocknr,
18eba7aa 693 unsigned long long *retp)
470decc6
DK
694{
695 int err = 0;
18eba7aa 696 unsigned long long ret;
470decc6
DK
697
698 if (journal->j_inode) {
699 ret = bmap(journal->j_inode, blocknr);
700 if (ret)
701 *retp = ret;
702 else {
470decc6
DK
703 printk(KERN_ALERT "%s: journal block not found "
704 "at offset %lu on %s\n",
05496769 705 __func__, blocknr, journal->j_devname);
470decc6
DK
706 err = -EIO;
707 __journal_abort_soft(journal, err);
708 }
709 } else {
710 *retp = blocknr; /* +journal->j_blk_offset */
711 }
712 return err;
713}
714
715/*
716 * We play buffer_head aliasing tricks to write data/metadata blocks to
717 * the journal without copying their contents, but for journal
718 * descriptor blocks we do need to generate bona fide buffers.
719 *
f7f4bccb 720 * After the caller of jbd2_journal_get_descriptor_buffer() has finished modifying
470decc6
DK
721 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
722 * But we don't bother doing that, so there will be coherency problems with
723 * mmaps of blockdevs which hold live JBD-controlled filesystems.
724 */
f7f4bccb 725struct journal_head *jbd2_journal_get_descriptor_buffer(journal_t *journal)
470decc6
DK
726{
727 struct buffer_head *bh;
18eba7aa 728 unsigned long long blocknr;
470decc6
DK
729 int err;
730
f7f4bccb 731 err = jbd2_journal_next_log_block(journal, &blocknr);
470decc6
DK
732
733 if (err)
734 return NULL;
735
736 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
4b905671
JK
737 if (!bh)
738 return NULL;
470decc6
DK
739 lock_buffer(bh);
740 memset(bh->b_data, 0, journal->j_blocksize);
741 set_buffer_uptodate(bh);
742 unlock_buffer(bh);
743 BUFFER_TRACE(bh, "return this buffer");
f7f4bccb 744 return jbd2_journal_add_journal_head(bh);
470decc6
DK
745}
746
8e85fb3f
JL
747struct jbd2_stats_proc_session {
748 journal_t *journal;
749 struct transaction_stats_s *stats;
750 int start;
751 int max;
752};
753
8e85fb3f
JL
754static void *jbd2_seq_info_start(struct seq_file *seq, loff_t *pos)
755{
756 return *pos ? NULL : SEQ_START_TOKEN;
757}
758
759static void *jbd2_seq_info_next(struct seq_file *seq, void *v, loff_t *pos)
760{
761 return NULL;
762}
763
764static int jbd2_seq_info_show(struct seq_file *seq, void *v)
765{
766 struct jbd2_stats_proc_session *s = seq->private;
767
768 if (v != SEQ_START_TOKEN)
769 return 0;
bf699327 770 seq_printf(seq, "%lu transaction, each up to %u blocks\n",
8e85fb3f
JL
771 s->stats->ts_tid,
772 s->journal->j_max_transaction_buffers);
773 if (s->stats->ts_tid == 0)
774 return 0;
775 seq_printf(seq, "average: \n %ums waiting for transaction\n",
bf699327 776 jiffies_to_msecs(s->stats->run.rs_wait / s->stats->ts_tid));
8e85fb3f 777 seq_printf(seq, " %ums running transaction\n",
bf699327 778 jiffies_to_msecs(s->stats->run.rs_running / s->stats->ts_tid));
8e85fb3f 779 seq_printf(seq, " %ums transaction was being locked\n",
bf699327 780 jiffies_to_msecs(s->stats->run.rs_locked / s->stats->ts_tid));
8e85fb3f 781 seq_printf(seq, " %ums flushing data (in ordered mode)\n",
bf699327 782 jiffies_to_msecs(s->stats->run.rs_flushing / s->stats->ts_tid));
8e85fb3f 783 seq_printf(seq, " %ums logging transaction\n",
bf699327 784 jiffies_to_msecs(s->stats->run.rs_logging / s->stats->ts_tid));
c225aa57
SHT
785 seq_printf(seq, " %lluus average transaction commit time\n",
786 div_u64(s->journal->j_average_commit_time, 1000));
8e85fb3f 787 seq_printf(seq, " %lu handles per transaction\n",
bf699327 788 s->stats->run.rs_handle_count / s->stats->ts_tid);
8e85fb3f 789 seq_printf(seq, " %lu blocks per transaction\n",
bf699327 790 s->stats->run.rs_blocks / s->stats->ts_tid);
8e85fb3f 791 seq_printf(seq, " %lu logged blocks per transaction\n",
bf699327 792 s->stats->run.rs_blocks_logged / s->stats->ts_tid);
8e85fb3f
JL
793 return 0;
794}
795
796static void jbd2_seq_info_stop(struct seq_file *seq, void *v)
797{
798}
799
88e9d34c 800static const struct seq_operations jbd2_seq_info_ops = {
8e85fb3f
JL
801 .start = jbd2_seq_info_start,
802 .next = jbd2_seq_info_next,
803 .stop = jbd2_seq_info_stop,
804 .show = jbd2_seq_info_show,
805};
806
807static int jbd2_seq_info_open(struct inode *inode, struct file *file)
808{
809 journal_t *journal = PDE(inode)->data;
810 struct jbd2_stats_proc_session *s;
811 int rc, size;
812
813 s = kmalloc(sizeof(*s), GFP_KERNEL);
814 if (s == NULL)
815 return -ENOMEM;
816 size = sizeof(struct transaction_stats_s);
817 s->stats = kmalloc(size, GFP_KERNEL);
818 if (s->stats == NULL) {
819 kfree(s);
820 return -ENOMEM;
821 }
822 spin_lock(&journal->j_history_lock);
823 memcpy(s->stats, &journal->j_stats, size);
824 s->journal = journal;
825 spin_unlock(&journal->j_history_lock);
826
827 rc = seq_open(file, &jbd2_seq_info_ops);
828 if (rc == 0) {
829 struct seq_file *m = file->private_data;
830 m->private = s;
831 } else {
832 kfree(s->stats);
833 kfree(s);
834 }
835 return rc;
836
837}
838
839static int jbd2_seq_info_release(struct inode *inode, struct file *file)
840{
841 struct seq_file *seq = file->private_data;
842 struct jbd2_stats_proc_session *s = seq->private;
843 kfree(s->stats);
844 kfree(s);
845 return seq_release(inode, file);
846}
847
828c0950 848static const struct file_operations jbd2_seq_info_fops = {
8e85fb3f
JL
849 .owner = THIS_MODULE,
850 .open = jbd2_seq_info_open,
851 .read = seq_read,
852 .llseek = seq_lseek,
853 .release = jbd2_seq_info_release,
854};
855
856static struct proc_dir_entry *proc_jbd2_stats;
857
858static void jbd2_stats_proc_init(journal_t *journal)
859{
05496769 860 journal->j_proc_entry = proc_mkdir(journal->j_devname, proc_jbd2_stats);
8e85fb3f 861 if (journal->j_proc_entry) {
79da3664
DL
862 proc_create_data("info", S_IRUGO, journal->j_proc_entry,
863 &jbd2_seq_info_fops, journal);
8e85fb3f
JL
864 }
865}
866
867static void jbd2_stats_proc_exit(journal_t *journal)
868{
8e85fb3f 869 remove_proc_entry("info", journal->j_proc_entry);
05496769 870 remove_proc_entry(journal->j_devname, proc_jbd2_stats);
8e85fb3f
JL
871}
872
470decc6
DK
873/*
874 * Management for journal control blocks: functions to create and
875 * destroy journal_t structures, and to initialise and read existing
876 * journal blocks from disk. */
877
878/* First: create and setup a journal_t object in memory. We initialise
879 * very few fields yet: that has to wait until we have created the
880 * journal structures from from scratch, or loaded them from disk. */
881
882static journal_t * journal_init_common (void)
883{
884 journal_t *journal;
885 int err;
886
3ebfdf88 887 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
470decc6 888 if (!journal)
b7271b0a 889 return NULL;
470decc6
DK
890
891 init_waitqueue_head(&journal->j_wait_transaction_locked);
892 init_waitqueue_head(&journal->j_wait_logspace);
893 init_waitqueue_head(&journal->j_wait_done_commit);
894 init_waitqueue_head(&journal->j_wait_checkpoint);
895 init_waitqueue_head(&journal->j_wait_commit);
896 init_waitqueue_head(&journal->j_wait_updates);
897 mutex_init(&journal->j_barrier);
898 mutex_init(&journal->j_checkpoint_mutex);
899 spin_lock_init(&journal->j_revoke_lock);
900 spin_lock_init(&journal->j_list_lock);
a931da6a 901 rwlock_init(&journal->j_state_lock);
470decc6 902
cd02ff0b 903 journal->j_commit_interval = (HZ * JBD2_DEFAULT_MAX_COMMIT_AGE);
30773840
TT
904 journal->j_min_batch_time = 0;
905 journal->j_max_batch_time = 15000; /* 15ms */
470decc6
DK
906
907 /* The journal is marked for error until we succeed with recovery! */
f7f4bccb 908 journal->j_flags = JBD2_ABORT;
470decc6
DK
909
910 /* Set up a default-sized revoke table for the new mount. */
f7f4bccb 911 err = jbd2_journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
470decc6
DK
912 if (err) {
913 kfree(journal);
b7271b0a 914 return NULL;
470decc6 915 }
8e85fb3f 916
bf699327 917 spin_lock_init(&journal->j_history_lock);
8e85fb3f 918
470decc6 919 return journal;
470decc6
DK
920}
921
f7f4bccb 922/* jbd2_journal_init_dev and jbd2_journal_init_inode:
470decc6
DK
923 *
924 * Create a journal structure assigned some fixed set of disk blocks to
925 * the journal. We don't actually touch those disk blocks yet, but we
926 * need to set up all of the mapping information to tell the journaling
927 * system where the journal blocks are.
928 *
929 */
930
931/**
5648ba5b 932 * journal_t * jbd2_journal_init_dev() - creates and initialises a journal structure
470decc6
DK
933 * @bdev: Block device on which to create the journal
934 * @fs_dev: Device which hold journalled filesystem for this journal.
935 * @start: Block nr Start of journal.
936 * @len: Length of the journal in blocks.
937 * @blocksize: blocksize of journalling device
5648ba5b
RD
938 *
939 * Returns: a newly created journal_t *
470decc6 940 *
f7f4bccb 941 * jbd2_journal_init_dev creates a journal which maps a fixed contiguous
470decc6
DK
942 * range of blocks on an arbitrary block device.
943 *
944 */
f7f4bccb 945journal_t * jbd2_journal_init_dev(struct block_device *bdev,
470decc6 946 struct block_device *fs_dev,
18eba7aa 947 unsigned long long start, int len, int blocksize)
470decc6
DK
948{
949 journal_t *journal = journal_init_common();
950 struct buffer_head *bh;
05496769 951 char *p;
470decc6
DK
952 int n;
953
954 if (!journal)
955 return NULL;
956
957 /* journal descriptor can store up to n blocks -bzzz */
958 journal->j_blocksize = blocksize;
0587aa3d 959 journal->j_dev = bdev;
960 journal->j_fs_dev = fs_dev;
961 journal->j_blk_offset = start;
962 journal->j_maxlen = len;
963 bdevname(journal->j_dev, journal->j_devname);
964 p = journal->j_devname;
965 while ((p = strchr(p, '/')))
966 *p = '!';
4b905671 967 jbd2_stats_proc_init(journal);
470decc6
DK
968 n = journal->j_blocksize / sizeof(journal_block_tag_t);
969 journal->j_wbufsize = n;
970 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
971 if (!journal->j_wbuf) {
25985edc 972 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
329d291f 973 __func__);
4b905671 974 goto out_err;
470decc6 975 }
470decc6
DK
976
977 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
4b905671
JK
978 if (!bh) {
979 printk(KERN_ERR
980 "%s: Cannot get buffer for journal superblock\n",
981 __func__);
982 goto out_err;
983 }
470decc6
DK
984 journal->j_sb_buffer = bh;
985 journal->j_superblock = (journal_superblock_t *)bh->b_data;
4b905671 986
470decc6 987 return journal;
4b905671 988out_err:
7b02bec0 989 kfree(journal->j_wbuf);
4b905671
JK
990 jbd2_stats_proc_exit(journal);
991 kfree(journal);
992 return NULL;
470decc6
DK
993}
994
995/**
f7f4bccb 996 * journal_t * jbd2_journal_init_inode () - creates a journal which maps to a inode.
470decc6
DK
997 * @inode: An inode to create the journal in
998 *
f7f4bccb 999 * jbd2_journal_init_inode creates a journal which maps an on-disk inode as
470decc6
DK
1000 * the journal. The inode must exist already, must support bmap() and
1001 * must have all data blocks preallocated.
1002 */
f7f4bccb 1003journal_t * jbd2_journal_init_inode (struct inode *inode)
470decc6
DK
1004{
1005 struct buffer_head *bh;
1006 journal_t *journal = journal_init_common();
05496769 1007 char *p;
470decc6
DK
1008 int err;
1009 int n;
18eba7aa 1010 unsigned long long blocknr;
470decc6
DK
1011
1012 if (!journal)
1013 return NULL;
1014
1015 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
1016 journal->j_inode = inode;
05496769
TT
1017 bdevname(journal->j_dev, journal->j_devname);
1018 p = journal->j_devname;
1019 while ((p = strchr(p, '/')))
1020 *p = '!';
1021 p = journal->j_devname + strlen(journal->j_devname);
90576c0b 1022 sprintf(p, "-%lu", journal->j_inode->i_ino);
470decc6
DK
1023 jbd_debug(1,
1024 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
1025 journal, inode->i_sb->s_id, inode->i_ino,
1026 (long long) inode->i_size,
1027 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
1028
1029 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
1030 journal->j_blocksize = inode->i_sb->s_blocksize;
8e85fb3f 1031 jbd2_stats_proc_init(journal);
470decc6
DK
1032
1033 /* journal descriptor can store up to n blocks -bzzz */
1034 n = journal->j_blocksize / sizeof(journal_block_tag_t);
1035 journal->j_wbufsize = n;
1036 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
1037 if (!journal->j_wbuf) {
25985edc 1038 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
329d291f 1039 __func__);
4b905671 1040 goto out_err;
470decc6
DK
1041 }
1042
f7f4bccb 1043 err = jbd2_journal_bmap(journal, 0, &blocknr);
470decc6
DK
1044 /* If that failed, give up */
1045 if (err) {
3c26bdb4 1046 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
329d291f 1047 __func__);
4b905671 1048 goto out_err;
470decc6
DK
1049 }
1050
1051 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
4b905671
JK
1052 if (!bh) {
1053 printk(KERN_ERR
1054 "%s: Cannot get buffer for journal superblock\n",
1055 __func__);
1056 goto out_err;
1057 }
470decc6
DK
1058 journal->j_sb_buffer = bh;
1059 journal->j_superblock = (journal_superblock_t *)bh->b_data;
1060
1061 return journal;
4b905671 1062out_err:
7b02bec0 1063 kfree(journal->j_wbuf);
4b905671
JK
1064 jbd2_stats_proc_exit(journal);
1065 kfree(journal);
1066 return NULL;
470decc6
DK
1067}
1068
1069/*
1070 * If the journal init or create aborts, we need to mark the journal
1071 * superblock as being NULL to prevent the journal destroy from writing
1072 * back a bogus superblock.
1073 */
1074static void journal_fail_superblock (journal_t *journal)
1075{
1076 struct buffer_head *bh = journal->j_sb_buffer;
1077 brelse(bh);
1078 journal->j_sb_buffer = NULL;
1079}
1080
1081/*
1082 * Given a journal_t structure, initialise the various fields for
1083 * startup of a new journaling session. We use this both when creating
1084 * a journal, and after recovering an old journal to reset it for
1085 * subsequent use.
1086 */
1087
1088static int journal_reset(journal_t *journal)
1089{
1090 journal_superblock_t *sb = journal->j_superblock;
18eba7aa 1091 unsigned long long first, last;
470decc6
DK
1092
1093 first = be32_to_cpu(sb->s_first);
1094 last = be32_to_cpu(sb->s_maxlen);
f6f50e28 1095 if (first + JBD2_MIN_JOURNAL_BLOCKS > last + 1) {
f2a44523 1096 printk(KERN_ERR "JBD2: Journal too short (blocks %llu-%llu).\n",
f6f50e28
JK
1097 first, last);
1098 journal_fail_superblock(journal);
1099 return -EINVAL;
1100 }
470decc6
DK
1101
1102 journal->j_first = first;
1103 journal->j_last = last;
1104
1105 journal->j_head = first;
1106 journal->j_tail = first;
1107 journal->j_free = last - first;
1108
1109 journal->j_tail_sequence = journal->j_transaction_sequence;
1110 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
1111 journal->j_commit_request = journal->j_commit_sequence;
1112
1113 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
1114
1115 /* Add the dynamic fields and write it to disk. */
f7f4bccb 1116 jbd2_journal_update_superblock(journal, 1);
97f06784 1117 return jbd2_journal_start_thread(journal);
470decc6
DK
1118}
1119
470decc6 1120/**
f7f4bccb 1121 * void jbd2_journal_update_superblock() - Update journal sb on disk.
470decc6
DK
1122 * @journal: The journal to update.
1123 * @wait: Set to '0' if you don't want to wait for IO completion.
1124 *
1125 * Update a journal's dynamic superblock fields and write it to disk,
1126 * optionally waiting for the IO to complete.
1127 */
f7f4bccb 1128void jbd2_journal_update_superblock(journal_t *journal, int wait)
470decc6
DK
1129{
1130 journal_superblock_t *sb = journal->j_superblock;
1131 struct buffer_head *bh = journal->j_sb_buffer;
1132
1133 /*
1134 * As a special case, if the on-disk copy is already marked as needing
1135 * no recovery (s_start == 0) and there are no outstanding transactions
1136 * in the filesystem, then we can safely defer the superblock update
f7f4bccb 1137 * until the next commit by setting JBD2_FLUSHED. This avoids
470decc6
DK
1138 * attempting a write to a potential-readonly device.
1139 */
1140 if (sb->s_start == 0 && journal->j_tail_sequence ==
1141 journal->j_transaction_sequence) {
f2a44523 1142 jbd_debug(1, "JBD2: Skipping superblock update on recovered sb "
470decc6
DK
1143 "(start %ld, seq %d, errno %d)\n",
1144 journal->j_tail, journal->j_tail_sequence,
1145 journal->j_errno);
1146 goto out;
1147 }
1148
914258bf
TT
1149 if (buffer_write_io_error(bh)) {
1150 /*
1151 * Oh, dear. A previous attempt to write the journal
1152 * superblock failed. This could happen because the
1153 * USB device was yanked out. Or it could happen to
1154 * be a transient write error and maybe the block will
1155 * be remapped. Nothing we can do but to retry the
1156 * write and hope for the best.
1157 */
1158 printk(KERN_ERR "JBD2: previous I/O error detected "
1159 "for journal superblock update for %s.\n",
1160 journal->j_devname);
1161 clear_buffer_write_io_error(bh);
1162 set_buffer_uptodate(bh);
1163 }
1164
a931da6a 1165 read_lock(&journal->j_state_lock);
f2a44523 1166 jbd_debug(1, "JBD2: updating superblock (start %ld, seq %d, errno %d)\n",
470decc6
DK
1167 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1168
1169 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1170 sb->s_start = cpu_to_be32(journal->j_tail);
1171 sb->s_errno = cpu_to_be32(journal->j_errno);
a931da6a 1172 read_unlock(&journal->j_state_lock);
470decc6
DK
1173
1174 BUFFER_TRACE(bh, "marking dirty");
1175 mark_buffer_dirty(bh);
914258bf 1176 if (wait) {
470decc6 1177 sync_dirty_buffer(bh);
914258bf
TT
1178 if (buffer_write_io_error(bh)) {
1179 printk(KERN_ERR "JBD2: I/O error detected "
1180 "when updating journal superblock for %s.\n",
1181 journal->j_devname);
1182 clear_buffer_write_io_error(bh);
1183 set_buffer_uptodate(bh);
1184 }
1185 } else
9cb569d6 1186 write_dirty_buffer(bh, WRITE);
470decc6 1187
2201c590
SA
1188 trace_jbd2_update_superblock_end(journal, wait);
1189
470decc6
DK
1190out:
1191 /* If we have just flushed the log (by marking s_start==0), then
1192 * any future commit will have to be careful to update the
1193 * superblock again to re-record the true start of the log. */
1194
a931da6a 1195 write_lock(&journal->j_state_lock);
470decc6 1196 if (sb->s_start)
f7f4bccb 1197 journal->j_flags &= ~JBD2_FLUSHED;
470decc6 1198 else
f7f4bccb 1199 journal->j_flags |= JBD2_FLUSHED;
a931da6a 1200 write_unlock(&journal->j_state_lock);
470decc6
DK
1201}
1202
1203/*
1204 * Read the superblock for a given journal, performing initial
1205 * validation of the format.
1206 */
1207
1208static int journal_get_superblock(journal_t *journal)
1209{
1210 struct buffer_head *bh;
1211 journal_superblock_t *sb;
1212 int err = -EIO;
1213
1214 bh = journal->j_sb_buffer;
1215
1216 J_ASSERT(bh != NULL);
1217 if (!buffer_uptodate(bh)) {
1218 ll_rw_block(READ, 1, &bh);
1219 wait_on_buffer(bh);
1220 if (!buffer_uptodate(bh)) {
f2a44523
EG
1221 printk(KERN_ERR
1222 "JBD2: IO error reading journal superblock\n");
470decc6
DK
1223 goto out;
1224 }
1225 }
1226
1227 sb = journal->j_superblock;
1228
1229 err = -EINVAL;
1230
f7f4bccb 1231 if (sb->s_header.h_magic != cpu_to_be32(JBD2_MAGIC_NUMBER) ||
470decc6 1232 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
f2a44523 1233 printk(KERN_WARNING "JBD2: no valid journal superblock found\n");
470decc6
DK
1234 goto out;
1235 }
1236
1237 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
f7f4bccb 1238 case JBD2_SUPERBLOCK_V1:
470decc6
DK
1239 journal->j_format_version = 1;
1240 break;
f7f4bccb 1241 case JBD2_SUPERBLOCK_V2:
470decc6
DK
1242 journal->j_format_version = 2;
1243 break;
1244 default:
f2a44523 1245 printk(KERN_WARNING "JBD2: unrecognised superblock format ID\n");
470decc6
DK
1246 goto out;
1247 }
1248
1249 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1250 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1251 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
f2a44523 1252 printk(KERN_WARNING "JBD2: journal file too short\n");
470decc6
DK
1253 goto out;
1254 }
1255
8762202d
EG
1256 if (be32_to_cpu(sb->s_first) == 0 ||
1257 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1258 printk(KERN_WARNING
1259 "JBD2: Invalid start block of journal: %u\n",
1260 be32_to_cpu(sb->s_first));
1261 goto out;
1262 }
1263
470decc6
DK
1264 return 0;
1265
1266out:
1267 journal_fail_superblock(journal);
1268 return err;
1269}
1270
1271/*
1272 * Load the on-disk journal superblock and read the key fields into the
1273 * journal_t.
1274 */
1275
1276static int load_superblock(journal_t *journal)
1277{
1278 int err;
1279 journal_superblock_t *sb;
1280
1281 err = journal_get_superblock(journal);
1282 if (err)
1283 return err;
1284
1285 sb = journal->j_superblock;
1286
1287 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1288 journal->j_tail = be32_to_cpu(sb->s_start);
1289 journal->j_first = be32_to_cpu(sb->s_first);
1290 journal->j_last = be32_to_cpu(sb->s_maxlen);
1291 journal->j_errno = be32_to_cpu(sb->s_errno);
1292
1293 return 0;
1294}
1295
1296
1297/**
f7f4bccb 1298 * int jbd2_journal_load() - Read journal from disk.
470decc6
DK
1299 * @journal: Journal to act on.
1300 *
1301 * Given a journal_t structure which tells us which disk blocks contain
1302 * a journal, read the journal from disk to initialise the in-memory
1303 * structures.
1304 */
f7f4bccb 1305int jbd2_journal_load(journal_t *journal)
470decc6
DK
1306{
1307 int err;
1308 journal_superblock_t *sb;
1309
1310 err = load_superblock(journal);
1311 if (err)
1312 return err;
1313
1314 sb = journal->j_superblock;
1315 /* If this is a V2 superblock, then we have to check the
1316 * features flags on it. */
1317
1318 if (journal->j_format_version >= 2) {
1319 if ((sb->s_feature_ro_compat &
f7f4bccb 1320 ~cpu_to_be32(JBD2_KNOWN_ROCOMPAT_FEATURES)) ||
470decc6 1321 (sb->s_feature_incompat &
f7f4bccb 1322 ~cpu_to_be32(JBD2_KNOWN_INCOMPAT_FEATURES))) {
f2a44523
EG
1323 printk(KERN_WARNING
1324 "JBD2: Unrecognised features on journal\n");
470decc6
DK
1325 return -EINVAL;
1326 }
1327 }
1328
d2eecb03
TT
1329 /*
1330 * Create a slab for this blocksize
1331 */
1332 err = jbd2_journal_create_slab(be32_to_cpu(sb->s_blocksize));
1333 if (err)
1334 return err;
1335
470decc6
DK
1336 /* Let the recovery code check whether it needs to recover any
1337 * data from the journal. */
f7f4bccb 1338 if (jbd2_journal_recover(journal))
470decc6
DK
1339 goto recovery_error;
1340
e6a47428
TT
1341 if (journal->j_failed_commit) {
1342 printk(KERN_ERR "JBD2: journal transaction %u on %s "
1343 "is corrupt.\n", journal->j_failed_commit,
1344 journal->j_devname);
1345 return -EIO;
1346 }
1347
470decc6
DK
1348 /* OK, we've finished with the dynamic journal bits:
1349 * reinitialise the dynamic contents of the superblock in memory
1350 * and reset them on disk. */
1351 if (journal_reset(journal))
1352 goto recovery_error;
1353
f7f4bccb
MC
1354 journal->j_flags &= ~JBD2_ABORT;
1355 journal->j_flags |= JBD2_LOADED;
470decc6
DK
1356 return 0;
1357
1358recovery_error:
f2a44523 1359 printk(KERN_WARNING "JBD2: recovery failed\n");
470decc6
DK
1360 return -EIO;
1361}
1362
1363/**
f7f4bccb 1364 * void jbd2_journal_destroy() - Release a journal_t structure.
470decc6
DK
1365 * @journal: Journal to act on.
1366 *
1367 * Release a journal_t structure once it is no longer in use by the
1368 * journaled object.
44519faf 1369 * Return <0 if we couldn't clean up the journal.
470decc6 1370 */
44519faf 1371int jbd2_journal_destroy(journal_t *journal)
470decc6 1372{
44519faf
HK
1373 int err = 0;
1374
470decc6
DK
1375 /* Wait for the commit thread to wake up and die. */
1376 journal_kill_thread(journal);
1377
1378 /* Force a final log commit */
1379 if (journal->j_running_transaction)
f7f4bccb 1380 jbd2_journal_commit_transaction(journal);
470decc6
DK
1381
1382 /* Force any old transactions to disk */
1383
1384 /* Totally anal locking here... */
1385 spin_lock(&journal->j_list_lock);
1386 while (journal->j_checkpoint_transactions != NULL) {
1387 spin_unlock(&journal->j_list_lock);
1a0d3786 1388 mutex_lock(&journal->j_checkpoint_mutex);
f7f4bccb 1389 jbd2_log_do_checkpoint(journal);
1a0d3786 1390 mutex_unlock(&journal->j_checkpoint_mutex);
470decc6
DK
1391 spin_lock(&journal->j_list_lock);
1392 }
1393
1394 J_ASSERT(journal->j_running_transaction == NULL);
1395 J_ASSERT(journal->j_committing_transaction == NULL);
1396 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1397 spin_unlock(&journal->j_list_lock);
1398
470decc6 1399 if (journal->j_sb_buffer) {
44519faf
HK
1400 if (!is_journal_aborted(journal)) {
1401 /* We can now mark the journal as empty. */
1402 journal->j_tail = 0;
1403 journal->j_tail_sequence =
1404 ++journal->j_transaction_sequence;
1405 jbd2_journal_update_superblock(journal, 1);
1406 } else {
1407 err = -EIO;
1408 }
470decc6
DK
1409 brelse(journal->j_sb_buffer);
1410 }
1411
8e85fb3f
JL
1412 if (journal->j_proc_entry)
1413 jbd2_stats_proc_exit(journal);
470decc6
DK
1414 if (journal->j_inode)
1415 iput(journal->j_inode);
1416 if (journal->j_revoke)
f7f4bccb 1417 jbd2_journal_destroy_revoke(journal);
470decc6
DK
1418 kfree(journal->j_wbuf);
1419 kfree(journal);
44519faf
HK
1420
1421 return err;
470decc6
DK
1422}
1423
1424
1425/**
f7f4bccb 1426 *int jbd2_journal_check_used_features () - Check if features specified are used.
470decc6
DK
1427 * @journal: Journal to check.
1428 * @compat: bitmask of compatible features
1429 * @ro: bitmask of features that force read-only mount
1430 * @incompat: bitmask of incompatible features
1431 *
1432 * Check whether the journal uses all of a given set of
1433 * features. Return true (non-zero) if it does.
1434 **/
1435
f7f4bccb 1436int jbd2_journal_check_used_features (journal_t *journal, unsigned long compat,
470decc6
DK
1437 unsigned long ro, unsigned long incompat)
1438{
1439 journal_superblock_t *sb;
1440
1441 if (!compat && !ro && !incompat)
1442 return 1;
1113e1b5
PL
1443 /* Load journal superblock if it is not loaded yet. */
1444 if (journal->j_format_version == 0 &&
1445 journal_get_superblock(journal) != 0)
1446 return 0;
470decc6
DK
1447 if (journal->j_format_version == 1)
1448 return 0;
1449
1450 sb = journal->j_superblock;
1451
1452 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1453 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1454 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1455 return 1;
1456
1457 return 0;
1458}
1459
1460/**
f7f4bccb 1461 * int jbd2_journal_check_available_features() - Check feature set in journalling layer
470decc6
DK
1462 * @journal: Journal to check.
1463 * @compat: bitmask of compatible features
1464 * @ro: bitmask of features that force read-only mount
1465 * @incompat: bitmask of incompatible features
1466 *
1467 * Check whether the journaling code supports the use of
1468 * all of a given set of features on this journal. Return true
1469 * (non-zero) if it can. */
1470
f7f4bccb 1471int jbd2_journal_check_available_features (journal_t *journal, unsigned long compat,
470decc6
DK
1472 unsigned long ro, unsigned long incompat)
1473{
470decc6
DK
1474 if (!compat && !ro && !incompat)
1475 return 1;
1476
470decc6
DK
1477 /* We can support any known requested features iff the
1478 * superblock is in version 2. Otherwise we fail to support any
1479 * extended sb features. */
1480
1481 if (journal->j_format_version != 2)
1482 return 0;
1483
f7f4bccb
MC
1484 if ((compat & JBD2_KNOWN_COMPAT_FEATURES) == compat &&
1485 (ro & JBD2_KNOWN_ROCOMPAT_FEATURES) == ro &&
1486 (incompat & JBD2_KNOWN_INCOMPAT_FEATURES) == incompat)
470decc6
DK
1487 return 1;
1488
1489 return 0;
1490}
1491
1492/**
f7f4bccb 1493 * int jbd2_journal_set_features () - Mark a given journal feature in the superblock
470decc6
DK
1494 * @journal: Journal to act on.
1495 * @compat: bitmask of compatible features
1496 * @ro: bitmask of features that force read-only mount
1497 * @incompat: bitmask of incompatible features
1498 *
1499 * Mark a given journal feature as present on the
1500 * superblock. Returns true if the requested features could be set.
1501 *
1502 */
1503
f7f4bccb 1504int jbd2_journal_set_features (journal_t *journal, unsigned long compat,
470decc6
DK
1505 unsigned long ro, unsigned long incompat)
1506{
1507 journal_superblock_t *sb;
1508
f7f4bccb 1509 if (jbd2_journal_check_used_features(journal, compat, ro, incompat))
470decc6
DK
1510 return 1;
1511
f7f4bccb 1512 if (!jbd2_journal_check_available_features(journal, compat, ro, incompat))
470decc6
DK
1513 return 0;
1514
1515 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1516 compat, ro, incompat);
1517
1518 sb = journal->j_superblock;
1519
1520 sb->s_feature_compat |= cpu_to_be32(compat);
1521 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1522 sb->s_feature_incompat |= cpu_to_be32(incompat);
1523
1524 return 1;
1525}
1526
818d276c
GS
1527/*
1528 * jbd2_journal_clear_features () - Clear a given journal feature in the
1529 * superblock
1530 * @journal: Journal to act on.
1531 * @compat: bitmask of compatible features
1532 * @ro: bitmask of features that force read-only mount
1533 * @incompat: bitmask of incompatible features
1534 *
1535 * Clear a given journal feature as present on the
1536 * superblock.
1537 */
1538void jbd2_journal_clear_features(journal_t *journal, unsigned long compat,
1539 unsigned long ro, unsigned long incompat)
1540{
1541 journal_superblock_t *sb;
1542
1543 jbd_debug(1, "Clear features 0x%lx/0x%lx/0x%lx\n",
1544 compat, ro, incompat);
1545
1546 sb = journal->j_superblock;
1547
1548 sb->s_feature_compat &= ~cpu_to_be32(compat);
1549 sb->s_feature_ro_compat &= ~cpu_to_be32(ro);
1550 sb->s_feature_incompat &= ~cpu_to_be32(incompat);
1551}
1552EXPORT_SYMBOL(jbd2_journal_clear_features);
470decc6
DK
1553
1554/**
f7f4bccb 1555 * int jbd2_journal_update_format () - Update on-disk journal structure.
470decc6
DK
1556 * @journal: Journal to act on.
1557 *
1558 * Given an initialised but unloaded journal struct, poke about in the
1559 * on-disk structure to update it to the most recent supported version.
1560 */
f7f4bccb 1561int jbd2_journal_update_format (journal_t *journal)
470decc6
DK
1562{
1563 journal_superblock_t *sb;
1564 int err;
1565
1566 err = journal_get_superblock(journal);
1567 if (err)
1568 return err;
1569
1570 sb = journal->j_superblock;
1571
1572 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
f7f4bccb 1573 case JBD2_SUPERBLOCK_V2:
470decc6 1574 return 0;
f7f4bccb 1575 case JBD2_SUPERBLOCK_V1:
470decc6
DK
1576 return journal_convert_superblock_v1(journal, sb);
1577 default:
1578 break;
1579 }
1580 return -EINVAL;
1581}
1582
1583static int journal_convert_superblock_v1(journal_t *journal,
1584 journal_superblock_t *sb)
1585{
1586 int offset, blocksize;
1587 struct buffer_head *bh;
1588
1589 printk(KERN_WARNING
f2a44523 1590 "JBD2: Converting superblock from version 1 to 2.\n");
470decc6
DK
1591
1592 /* Pre-initialise new fields to zero */
1593 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1594 blocksize = be32_to_cpu(sb->s_blocksize);
1595 memset(&sb->s_feature_compat, 0, blocksize-offset);
1596
1597 sb->s_nr_users = cpu_to_be32(1);
f7f4bccb 1598 sb->s_header.h_blocktype = cpu_to_be32(JBD2_SUPERBLOCK_V2);
470decc6
DK
1599 journal->j_format_version = 2;
1600
1601 bh = journal->j_sb_buffer;
1602 BUFFER_TRACE(bh, "marking dirty");
1603 mark_buffer_dirty(bh);
1604 sync_dirty_buffer(bh);
1605 return 0;
1606}
1607
1608
1609/**
f7f4bccb 1610 * int jbd2_journal_flush () - Flush journal
470decc6
DK
1611 * @journal: Journal to act on.
1612 *
1613 * Flush all data for a given journal to disk and empty the journal.
1614 * Filesystems can use this when remounting readonly to ensure that
1615 * recovery does not need to happen on remount.
1616 */
1617
f7f4bccb 1618int jbd2_journal_flush(journal_t *journal)
470decc6
DK
1619{
1620 int err = 0;
1621 transaction_t *transaction = NULL;
1622 unsigned long old_tail;
1623
a931da6a 1624 write_lock(&journal->j_state_lock);
470decc6
DK
1625
1626 /* Force everything buffered to the log... */
1627 if (journal->j_running_transaction) {
1628 transaction = journal->j_running_transaction;
f7f4bccb 1629 __jbd2_log_start_commit(journal, transaction->t_tid);
470decc6
DK
1630 } else if (journal->j_committing_transaction)
1631 transaction = journal->j_committing_transaction;
1632
1633 /* Wait for the log commit to complete... */
1634 if (transaction) {
1635 tid_t tid = transaction->t_tid;
1636
a931da6a 1637 write_unlock(&journal->j_state_lock);
f7f4bccb 1638 jbd2_log_wait_commit(journal, tid);
470decc6 1639 } else {
a931da6a 1640 write_unlock(&journal->j_state_lock);
470decc6
DK
1641 }
1642
1643 /* ...and flush everything in the log out to disk. */
1644 spin_lock(&journal->j_list_lock);
1645 while (!err && journal->j_checkpoint_transactions != NULL) {
1646 spin_unlock(&journal->j_list_lock);
44519faf 1647 mutex_lock(&journal->j_checkpoint_mutex);
f7f4bccb 1648 err = jbd2_log_do_checkpoint(journal);
44519faf 1649 mutex_unlock(&journal->j_checkpoint_mutex);
470decc6
DK
1650 spin_lock(&journal->j_list_lock);
1651 }
1652 spin_unlock(&journal->j_list_lock);
44519faf
HK
1653
1654 if (is_journal_aborted(journal))
1655 return -EIO;
1656
f7f4bccb 1657 jbd2_cleanup_journal_tail(journal);
470decc6
DK
1658
1659 /* Finally, mark the journal as really needing no recovery.
1660 * This sets s_start==0 in the underlying superblock, which is
1661 * the magic code for a fully-recovered superblock. Any future
1662 * commits of data to the journal will restore the current
1663 * s_start value. */
a931da6a 1664 write_lock(&journal->j_state_lock);
470decc6
DK
1665 old_tail = journal->j_tail;
1666 journal->j_tail = 0;
a931da6a 1667 write_unlock(&journal->j_state_lock);
f7f4bccb 1668 jbd2_journal_update_superblock(journal, 1);
a931da6a 1669 write_lock(&journal->j_state_lock);
470decc6
DK
1670 journal->j_tail = old_tail;
1671
1672 J_ASSERT(!journal->j_running_transaction);
1673 J_ASSERT(!journal->j_committing_transaction);
1674 J_ASSERT(!journal->j_checkpoint_transactions);
1675 J_ASSERT(journal->j_head == journal->j_tail);
1676 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
a931da6a 1677 write_unlock(&journal->j_state_lock);
44519faf 1678 return 0;
470decc6
DK
1679}
1680
1681/**
f7f4bccb 1682 * int jbd2_journal_wipe() - Wipe journal contents
470decc6
DK
1683 * @journal: Journal to act on.
1684 * @write: flag (see below)
1685 *
1686 * Wipe out all of the contents of a journal, safely. This will produce
1687 * a warning if the journal contains any valid recovery information.
f7f4bccb 1688 * Must be called between journal_init_*() and jbd2_journal_load().
470decc6
DK
1689 *
1690 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1691 * we merely suppress recovery.
1692 */
1693
f7f4bccb 1694int jbd2_journal_wipe(journal_t *journal, int write)
470decc6 1695{
470decc6
DK
1696 int err = 0;
1697
f7f4bccb 1698 J_ASSERT (!(journal->j_flags & JBD2_LOADED));
470decc6
DK
1699
1700 err = load_superblock(journal);
1701 if (err)
1702 return err;
1703
470decc6
DK
1704 if (!journal->j_tail)
1705 goto no_recovery;
1706
f2a44523 1707 printk(KERN_WARNING "JBD2: %s recovery information on journal\n",
470decc6
DK
1708 write ? "Clearing" : "Ignoring");
1709
f7f4bccb 1710 err = jbd2_journal_skip_recovery(journal);
470decc6 1711 if (write)
f7f4bccb 1712 jbd2_journal_update_superblock(journal, 1);
470decc6
DK
1713
1714 no_recovery:
1715 return err;
1716}
1717
470decc6
DK
1718/*
1719 * Journal abort has very specific semantics, which we describe
1720 * for journal abort.
1721 *
bfcd3555 1722 * Two internal functions, which provide abort to the jbd layer
470decc6
DK
1723 * itself are here.
1724 */
1725
1726/*
1727 * Quick version for internal journal use (doesn't lock the journal).
1728 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1729 * and don't attempt to make any other journal updates.
1730 */
f7f4bccb 1731void __jbd2_journal_abort_hard(journal_t *journal)
470decc6
DK
1732{
1733 transaction_t *transaction;
470decc6 1734
f7f4bccb 1735 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1736 return;
1737
1738 printk(KERN_ERR "Aborting journal on device %s.\n",
05496769 1739 journal->j_devname);
470decc6 1740
a931da6a 1741 write_lock(&journal->j_state_lock);
f7f4bccb 1742 journal->j_flags |= JBD2_ABORT;
470decc6
DK
1743 transaction = journal->j_running_transaction;
1744 if (transaction)
f7f4bccb 1745 __jbd2_log_start_commit(journal, transaction->t_tid);
a931da6a 1746 write_unlock(&journal->j_state_lock);
470decc6
DK
1747}
1748
1749/* Soft abort: record the abort error status in the journal superblock,
1750 * but don't do any other IO. */
1751static void __journal_abort_soft (journal_t *journal, int errno)
1752{
f7f4bccb 1753 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1754 return;
1755
1756 if (!journal->j_errno)
1757 journal->j_errno = errno;
1758
f7f4bccb 1759 __jbd2_journal_abort_hard(journal);
470decc6
DK
1760
1761 if (errno)
f7f4bccb 1762 jbd2_journal_update_superblock(journal, 1);
470decc6
DK
1763}
1764
1765/**
f7f4bccb 1766 * void jbd2_journal_abort () - Shutdown the journal immediately.
470decc6
DK
1767 * @journal: the journal to shutdown.
1768 * @errno: an error number to record in the journal indicating
1769 * the reason for the shutdown.
1770 *
1771 * Perform a complete, immediate shutdown of the ENTIRE
1772 * journal (not of a single transaction). This operation cannot be
1773 * undone without closing and reopening the journal.
1774 *
f7f4bccb 1775 * The jbd2_journal_abort function is intended to support higher level error
470decc6
DK
1776 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1777 * mode.
1778 *
1779 * Journal abort has very specific semantics. Any existing dirty,
1780 * unjournaled buffers in the main filesystem will still be written to
1781 * disk by bdflush, but the journaling mechanism will be suspended
1782 * immediately and no further transaction commits will be honoured.
1783 *
1784 * Any dirty, journaled buffers will be written back to disk without
1785 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1786 * filesystem, but we _do_ attempt to leave as much data as possible
1787 * behind for fsck to use for cleanup.
1788 *
1789 * Any attempt to get a new transaction handle on a journal which is in
1790 * ABORT state will just result in an -EROFS error return. A
f7f4bccb 1791 * jbd2_journal_stop on an existing handle will return -EIO if we have
470decc6
DK
1792 * entered abort state during the update.
1793 *
1794 * Recursive transactions are not disturbed by journal abort until the
f7f4bccb 1795 * final jbd2_journal_stop, which will receive the -EIO error.
470decc6 1796 *
f7f4bccb 1797 * Finally, the jbd2_journal_abort call allows the caller to supply an errno
470decc6
DK
1798 * which will be recorded (if possible) in the journal superblock. This
1799 * allows a client to record failure conditions in the middle of a
1800 * transaction without having to complete the transaction to record the
1801 * failure to disk. ext3_error, for example, now uses this
1802 * functionality.
1803 *
1804 * Errors which originate from within the journaling layer will NOT
1805 * supply an errno; a null errno implies that absolutely no further
1806 * writes are done to the journal (unless there are any already in
1807 * progress).
1808 *
1809 */
1810
f7f4bccb 1811void jbd2_journal_abort(journal_t *journal, int errno)
470decc6
DK
1812{
1813 __journal_abort_soft(journal, errno);
1814}
1815
1816/**
f7f4bccb 1817 * int jbd2_journal_errno () - returns the journal's error state.
470decc6
DK
1818 * @journal: journal to examine.
1819 *
bfcd3555 1820 * This is the errno number set with jbd2_journal_abort(), the last
470decc6
DK
1821 * time the journal was mounted - if the journal was stopped
1822 * without calling abort this will be 0.
1823 *
1824 * If the journal has been aborted on this mount time -EROFS will
1825 * be returned.
1826 */
f7f4bccb 1827int jbd2_journal_errno(journal_t *journal)
470decc6
DK
1828{
1829 int err;
1830
a931da6a 1831 read_lock(&journal->j_state_lock);
f7f4bccb 1832 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1833 err = -EROFS;
1834 else
1835 err = journal->j_errno;
a931da6a 1836 read_unlock(&journal->j_state_lock);
470decc6
DK
1837 return err;
1838}
1839
1840/**
f7f4bccb 1841 * int jbd2_journal_clear_err () - clears the journal's error state
470decc6
DK
1842 * @journal: journal to act on.
1843 *
bfcd3555 1844 * An error must be cleared or acked to take a FS out of readonly
470decc6
DK
1845 * mode.
1846 */
f7f4bccb 1847int jbd2_journal_clear_err(journal_t *journal)
470decc6
DK
1848{
1849 int err = 0;
1850
a931da6a 1851 write_lock(&journal->j_state_lock);
f7f4bccb 1852 if (journal->j_flags & JBD2_ABORT)
470decc6
DK
1853 err = -EROFS;
1854 else
1855 journal->j_errno = 0;
a931da6a 1856 write_unlock(&journal->j_state_lock);
470decc6
DK
1857 return err;
1858}
1859
1860/**
f7f4bccb 1861 * void jbd2_journal_ack_err() - Ack journal err.
470decc6
DK
1862 * @journal: journal to act on.
1863 *
bfcd3555 1864 * An error must be cleared or acked to take a FS out of readonly
470decc6
DK
1865 * mode.
1866 */
f7f4bccb 1867void jbd2_journal_ack_err(journal_t *journal)
470decc6 1868{
a931da6a 1869 write_lock(&journal->j_state_lock);
470decc6 1870 if (journal->j_errno)
f7f4bccb 1871 journal->j_flags |= JBD2_ACK_ERR;
a931da6a 1872 write_unlock(&journal->j_state_lock);
470decc6
DK
1873}
1874
f7f4bccb 1875int jbd2_journal_blocks_per_page(struct inode *inode)
470decc6
DK
1876{
1877 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1878}
1879
b517bea1
ZB
1880/*
1881 * helper functions to deal with 32 or 64bit block numbers.
1882 */
1883size_t journal_tag_bytes(journal_t *journal)
1884{
1885 if (JBD2_HAS_INCOMPAT_FEATURE(journal, JBD2_FEATURE_INCOMPAT_64BIT))
cd02ff0b 1886 return JBD2_TAG_SIZE64;
b517bea1 1887 else
cd02ff0b 1888 return JBD2_TAG_SIZE32;
b517bea1
ZB
1889}
1890
d2eecb03
TT
1891/*
1892 * JBD memory management
1893 *
1894 * These functions are used to allocate block-sized chunks of memory
1895 * used for making copies of buffer_head data. Very often it will be
1896 * page-sized chunks of data, but sometimes it will be in
1897 * sub-page-size chunks. (For example, 16k pages on Power systems
1898 * with a 4k block file system.) For blocks smaller than a page, we
1899 * use a SLAB allocator. There are slab caches for each block size,
1900 * which are allocated at mount time, if necessary, and we only free
1901 * (all of) the slab caches when/if the jbd2 module is unloaded. For
1902 * this reason we don't need to a mutex to protect access to
1903 * jbd2_slab[] allocating or releasing memory; only in
1904 * jbd2_journal_create_slab().
1905 */
1906#define JBD2_MAX_SLABS 8
1907static struct kmem_cache *jbd2_slab[JBD2_MAX_SLABS];
d2eecb03
TT
1908
1909static const char *jbd2_slab_names[JBD2_MAX_SLABS] = {
1910 "jbd2_1k", "jbd2_2k", "jbd2_4k", "jbd2_8k",
1911 "jbd2_16k", "jbd2_32k", "jbd2_64k", "jbd2_128k"
1912};
1913
1914
1915static void jbd2_journal_destroy_slabs(void)
1916{
1917 int i;
1918
1919 for (i = 0; i < JBD2_MAX_SLABS; i++) {
1920 if (jbd2_slab[i])
1921 kmem_cache_destroy(jbd2_slab[i]);
1922 jbd2_slab[i] = NULL;
1923 }
1924}
1925
1926static int jbd2_journal_create_slab(size_t size)
1927{
51dfacde 1928 static DEFINE_MUTEX(jbd2_slab_create_mutex);
d2eecb03
TT
1929 int i = order_base_2(size) - 10;
1930 size_t slab_size;
1931
1932 if (size == PAGE_SIZE)
1933 return 0;
1934
1935 if (i >= JBD2_MAX_SLABS)
1936 return -EINVAL;
1937
1938 if (unlikely(i < 0))
1939 i = 0;
51dfacde 1940 mutex_lock(&jbd2_slab_create_mutex);
d2eecb03 1941 if (jbd2_slab[i]) {
51dfacde 1942 mutex_unlock(&jbd2_slab_create_mutex);
d2eecb03
TT
1943 return 0; /* Already created */
1944 }
1945
1946 slab_size = 1 << (i+10);
1947 jbd2_slab[i] = kmem_cache_create(jbd2_slab_names[i], slab_size,
1948 slab_size, 0, NULL);
51dfacde 1949 mutex_unlock(&jbd2_slab_create_mutex);
d2eecb03
TT
1950 if (!jbd2_slab[i]) {
1951 printk(KERN_EMERG "JBD2: no memory for jbd2_slab cache\n");
1952 return -ENOMEM;
1953 }
1954 return 0;
1955}
1956
1957static struct kmem_cache *get_slab(size_t size)
1958{
1959 int i = order_base_2(size) - 10;
1960
1961 BUG_ON(i >= JBD2_MAX_SLABS);
1962 if (unlikely(i < 0))
1963 i = 0;
8ac97b74 1964 BUG_ON(jbd2_slab[i] == NULL);
d2eecb03
TT
1965 return jbd2_slab[i];
1966}
1967
1968void *jbd2_alloc(size_t size, gfp_t flags)
1969{
1970 void *ptr;
1971
1972 BUG_ON(size & (size-1)); /* Must be a power of 2 */
1973
1974 flags |= __GFP_REPEAT;
1975 if (size == PAGE_SIZE)
1976 ptr = (void *)__get_free_pages(flags, 0);
1977 else if (size > PAGE_SIZE) {
1978 int order = get_order(size);
1979
1980 if (order < 3)
1981 ptr = (void *)__get_free_pages(flags, order);
1982 else
1983 ptr = vmalloc(size);
1984 } else
1985 ptr = kmem_cache_alloc(get_slab(size), flags);
1986
1987 /* Check alignment; SLUB has gotten this wrong in the past,
1988 * and this can lead to user data corruption! */
1989 BUG_ON(((unsigned long) ptr) & (size-1));
1990
1991 return ptr;
1992}
1993
1994void jbd2_free(void *ptr, size_t size)
1995{
1996 if (size == PAGE_SIZE) {
1997 free_pages((unsigned long)ptr, 0);
1998 return;
1999 }
2000 if (size > PAGE_SIZE) {
2001 int order = get_order(size);
2002
2003 if (order < 3)
2004 free_pages((unsigned long)ptr, order);
2005 else
2006 vfree(ptr);
2007 return;
2008 }
2009 kmem_cache_free(get_slab(size), ptr);
2010};
2011
470decc6
DK
2012/*
2013 * Journal_head storage management
2014 */
e18b890b 2015static struct kmem_cache *jbd2_journal_head_cache;
e23291b9 2016#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
2017static atomic_t nr_journal_heads = ATOMIC_INIT(0);
2018#endif
2019
f7f4bccb 2020static int journal_init_jbd2_journal_head_cache(void)
470decc6
DK
2021{
2022 int retval;
2023
1076d17a 2024 J_ASSERT(jbd2_journal_head_cache == NULL);
a920e941 2025 jbd2_journal_head_cache = kmem_cache_create("jbd2_journal_head",
470decc6
DK
2026 sizeof(struct journal_head),
2027 0, /* offset */
77160957 2028 SLAB_TEMPORARY, /* flags */
20c2df83 2029 NULL); /* ctor */
470decc6 2030 retval = 0;
1076d17a 2031 if (!jbd2_journal_head_cache) {
470decc6 2032 retval = -ENOMEM;
f2a44523 2033 printk(KERN_EMERG "JBD2: no memory for journal_head cache\n");
470decc6
DK
2034 }
2035 return retval;
2036}
2037
f7f4bccb 2038static void jbd2_journal_destroy_jbd2_journal_head_cache(void)
470decc6 2039{
8a9362eb
DG
2040 if (jbd2_journal_head_cache) {
2041 kmem_cache_destroy(jbd2_journal_head_cache);
2042 jbd2_journal_head_cache = NULL;
2043 }
470decc6
DK
2044}
2045
2046/*
2047 * journal_head splicing and dicing
2048 */
2049static struct journal_head *journal_alloc_journal_head(void)
2050{
2051 struct journal_head *ret;
470decc6 2052
e23291b9 2053#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
2054 atomic_inc(&nr_journal_heads);
2055#endif
f7f4bccb 2056 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
1076d17a 2057 if (!ret) {
470decc6 2058 jbd_debug(1, "out of memory for journal_head\n");
670be5a7 2059 pr_notice_ratelimited("ENOMEM in %s, retrying.\n", __func__);
1076d17a 2060 while (!ret) {
470decc6 2061 yield();
f7f4bccb 2062 ret = kmem_cache_alloc(jbd2_journal_head_cache, GFP_NOFS);
470decc6
DK
2063 }
2064 }
2065 return ret;
2066}
2067
2068static void journal_free_journal_head(struct journal_head *jh)
2069{
e23291b9 2070#ifdef CONFIG_JBD2_DEBUG
470decc6 2071 atomic_dec(&nr_journal_heads);
cd02ff0b 2072 memset(jh, JBD2_POISON_FREE, sizeof(*jh));
470decc6 2073#endif
f7f4bccb 2074 kmem_cache_free(jbd2_journal_head_cache, jh);
470decc6
DK
2075}
2076
2077/*
2078 * A journal_head is attached to a buffer_head whenever JBD has an
2079 * interest in the buffer.
2080 *
2081 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
2082 * is set. This bit is tested in core kernel code where we need to take
2083 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
2084 * there.
2085 *
2086 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
2087 *
2088 * When a buffer has its BH_JBD bit set it is immune from being released by
2089 * core kernel code, mainly via ->b_count.
2090 *
de1b7941
JK
2091 * A journal_head is detached from its buffer_head when the journal_head's
2092 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
2093 * transaction (b_cp_transaction) hold their references to b_jcount.
470decc6
DK
2094 *
2095 * Various places in the kernel want to attach a journal_head to a buffer_head
2096 * _before_ attaching the journal_head to a transaction. To protect the
f7f4bccb 2097 * journal_head in this situation, jbd2_journal_add_journal_head elevates the
470decc6 2098 * journal_head's b_jcount refcount by one. The caller must call
f7f4bccb 2099 * jbd2_journal_put_journal_head() to undo this.
470decc6
DK
2100 *
2101 * So the typical usage would be:
2102 *
2103 * (Attach a journal_head if needed. Increments b_jcount)
f7f4bccb 2104 * struct journal_head *jh = jbd2_journal_add_journal_head(bh);
470decc6 2105 * ...
de1b7941
JK
2106 * (Get another reference for transaction)
2107 * jbd2_journal_grab_journal_head(bh);
470decc6 2108 * jh->b_transaction = xxx;
de1b7941 2109 * (Put original reference)
f7f4bccb 2110 * jbd2_journal_put_journal_head(jh);
470decc6
DK
2111 */
2112
2113/*
2114 * Give a buffer_head a journal_head.
2115 *
470decc6
DK
2116 * May sleep.
2117 */
f7f4bccb 2118struct journal_head *jbd2_journal_add_journal_head(struct buffer_head *bh)
470decc6
DK
2119{
2120 struct journal_head *jh;
2121 struct journal_head *new_jh = NULL;
2122
2123repeat:
2124 if (!buffer_jbd(bh)) {
2125 new_jh = journal_alloc_journal_head();
2126 memset(new_jh, 0, sizeof(*new_jh));
2127 }
2128
2129 jbd_lock_bh_journal_head(bh);
2130 if (buffer_jbd(bh)) {
2131 jh = bh2jh(bh);
2132 } else {
2133 J_ASSERT_BH(bh,
2134 (atomic_read(&bh->b_count) > 0) ||
2135 (bh->b_page && bh->b_page->mapping));
2136
2137 if (!new_jh) {
2138 jbd_unlock_bh_journal_head(bh);
2139 goto repeat;
2140 }
2141
2142 jh = new_jh;
2143 new_jh = NULL; /* We consumed it */
2144 set_buffer_jbd(bh);
2145 bh->b_private = jh;
2146 jh->b_bh = bh;
2147 get_bh(bh);
2148 BUFFER_TRACE(bh, "added journal_head");
2149 }
2150 jh->b_jcount++;
2151 jbd_unlock_bh_journal_head(bh);
2152 if (new_jh)
2153 journal_free_journal_head(new_jh);
2154 return bh->b_private;
2155}
2156
2157/*
2158 * Grab a ref against this buffer_head's journal_head. If it ended up not
2159 * having a journal_head, return NULL
2160 */
f7f4bccb 2161struct journal_head *jbd2_journal_grab_journal_head(struct buffer_head *bh)
470decc6
DK
2162{
2163 struct journal_head *jh = NULL;
2164
2165 jbd_lock_bh_journal_head(bh);
2166 if (buffer_jbd(bh)) {
2167 jh = bh2jh(bh);
2168 jh->b_jcount++;
2169 }
2170 jbd_unlock_bh_journal_head(bh);
2171 return jh;
2172}
2173
2174static void __journal_remove_journal_head(struct buffer_head *bh)
2175{
2176 struct journal_head *jh = bh2jh(bh);
2177
2178 J_ASSERT_JH(jh, jh->b_jcount >= 0);
de1b7941
JK
2179 J_ASSERT_JH(jh, jh->b_transaction == NULL);
2180 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
2181 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
2182 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
2183 J_ASSERT_BH(bh, buffer_jbd(bh));
2184 J_ASSERT_BH(bh, jh2bh(jh) == bh);
2185 BUFFER_TRACE(bh, "remove journal_head");
2186 if (jh->b_frozen_data) {
2187 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
2188 jbd2_free(jh->b_frozen_data, bh->b_size);
470decc6 2189 }
de1b7941
JK
2190 if (jh->b_committed_data) {
2191 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
2192 jbd2_free(jh->b_committed_data, bh->b_size);
2193 }
2194 bh->b_private = NULL;
2195 jh->b_bh = NULL; /* debug, really */
2196 clear_buffer_jbd(bh);
2197 journal_free_journal_head(jh);
470decc6
DK
2198}
2199
2200/*
de1b7941 2201 * Drop a reference on the passed journal_head. If it fell to zero then
470decc6
DK
2202 * release the journal_head from the buffer_head.
2203 */
f7f4bccb 2204void jbd2_journal_put_journal_head(struct journal_head *jh)
470decc6
DK
2205{
2206 struct buffer_head *bh = jh2bh(jh);
2207
2208 jbd_lock_bh_journal_head(bh);
2209 J_ASSERT_JH(jh, jh->b_jcount > 0);
2210 --jh->b_jcount;
de1b7941 2211 if (!jh->b_jcount) {
470decc6 2212 __journal_remove_journal_head(bh);
de1b7941 2213 jbd_unlock_bh_journal_head(bh);
470decc6 2214 __brelse(bh);
de1b7941
JK
2215 } else
2216 jbd_unlock_bh_journal_head(bh);
470decc6
DK
2217}
2218
c851ed54
JK
2219/*
2220 * Initialize jbd inode head
2221 */
2222void jbd2_journal_init_jbd_inode(struct jbd2_inode *jinode, struct inode *inode)
2223{
2224 jinode->i_transaction = NULL;
2225 jinode->i_next_transaction = NULL;
2226 jinode->i_vfs_inode = inode;
2227 jinode->i_flags = 0;
2228 INIT_LIST_HEAD(&jinode->i_list);
2229}
2230
2231/*
2232 * Function to be called before we start removing inode from memory (i.e.,
2233 * clear_inode() is a fine place to be called from). It removes inode from
2234 * transaction's lists.
2235 */
2236void jbd2_journal_release_jbd_inode(journal_t *journal,
2237 struct jbd2_inode *jinode)
2238{
c851ed54
JK
2239 if (!journal)
2240 return;
2241restart:
2242 spin_lock(&journal->j_list_lock);
2243 /* Is commit writing out inode - we have to wait */
39e3ac25 2244 if (test_bit(__JI_COMMIT_RUNNING, &jinode->i_flags)) {
c851ed54
JK
2245 wait_queue_head_t *wq;
2246 DEFINE_WAIT_BIT(wait, &jinode->i_flags, __JI_COMMIT_RUNNING);
2247 wq = bit_waitqueue(&jinode->i_flags, __JI_COMMIT_RUNNING);
2248 prepare_to_wait(wq, &wait.wait, TASK_UNINTERRUPTIBLE);
2249 spin_unlock(&journal->j_list_lock);
2250 schedule();
2251 finish_wait(wq, &wait.wait);
2252 goto restart;
2253 }
2254
c851ed54
JK
2255 if (jinode->i_transaction) {
2256 list_del(&jinode->i_list);
2257 jinode->i_transaction = NULL;
2258 }
2259 spin_unlock(&journal->j_list_lock);
2260}
2261
470decc6 2262/*
0f49d5d0 2263 * debugfs tunables
470decc6 2264 */
6f38c74f
JS
2265#ifdef CONFIG_JBD2_DEBUG
2266u8 jbd2_journal_enable_debug __read_mostly;
f7f4bccb 2267EXPORT_SYMBOL(jbd2_journal_enable_debug);
470decc6 2268
0f49d5d0 2269#define JBD2_DEBUG_NAME "jbd2-debug"
470decc6 2270
6f38c74f
JS
2271static struct dentry *jbd2_debugfs_dir;
2272static struct dentry *jbd2_debug;
470decc6 2273
0f49d5d0
JS
2274static void __init jbd2_create_debugfs_entry(void)
2275{
2276 jbd2_debugfs_dir = debugfs_create_dir("jbd2", NULL);
2277 if (jbd2_debugfs_dir)
765f8361
YK
2278 jbd2_debug = debugfs_create_u8(JBD2_DEBUG_NAME,
2279 S_IRUGO | S_IWUSR,
0f49d5d0
JS
2280 jbd2_debugfs_dir,
2281 &jbd2_journal_enable_debug);
470decc6
DK
2282}
2283
0f49d5d0 2284static void __exit jbd2_remove_debugfs_entry(void)
470decc6 2285{
6f38c74f
JS
2286 debugfs_remove(jbd2_debug);
2287 debugfs_remove(jbd2_debugfs_dir);
470decc6
DK
2288}
2289
0f49d5d0 2290#else
470decc6 2291
0f49d5d0 2292static void __init jbd2_create_debugfs_entry(void)
470decc6 2293{
470decc6
DK
2294}
2295
0f49d5d0 2296static void __exit jbd2_remove_debugfs_entry(void)
470decc6 2297{
470decc6
DK
2298}
2299
470decc6
DK
2300#endif
2301
8e85fb3f
JL
2302#ifdef CONFIG_PROC_FS
2303
2304#define JBD2_STATS_PROC_NAME "fs/jbd2"
2305
2306static void __init jbd2_create_jbd_stats_proc_entry(void)
2307{
2308 proc_jbd2_stats = proc_mkdir(JBD2_STATS_PROC_NAME, NULL);
2309}
2310
2311static void __exit jbd2_remove_jbd_stats_proc_entry(void)
2312{
2313 if (proc_jbd2_stats)
2314 remove_proc_entry(JBD2_STATS_PROC_NAME, NULL);
2315}
2316
2317#else
2318
2319#define jbd2_create_jbd_stats_proc_entry() do {} while (0)
2320#define jbd2_remove_jbd_stats_proc_entry() do {} while (0)
2321
2322#endif
2323
8aefcd55 2324struct kmem_cache *jbd2_handle_cache, *jbd2_inode_cache;
470decc6
DK
2325
2326static int __init journal_init_handle_cache(void)
2327{
8aefcd55 2328 jbd2_handle_cache = KMEM_CACHE(jbd2_journal_handle, SLAB_TEMPORARY);
f7f4bccb 2329 if (jbd2_handle_cache == NULL) {
8aefcd55
TT
2330 printk(KERN_EMERG "JBD2: failed to create handle cache\n");
2331 return -ENOMEM;
2332 }
2333 jbd2_inode_cache = KMEM_CACHE(jbd2_inode, 0);
2334 if (jbd2_inode_cache == NULL) {
2335 printk(KERN_EMERG "JBD2: failed to create inode cache\n");
2336 kmem_cache_destroy(jbd2_handle_cache);
470decc6
DK
2337 return -ENOMEM;
2338 }
2339 return 0;
2340}
2341
f7f4bccb 2342static void jbd2_journal_destroy_handle_cache(void)
470decc6 2343{
f7f4bccb
MC
2344 if (jbd2_handle_cache)
2345 kmem_cache_destroy(jbd2_handle_cache);
8aefcd55
TT
2346 if (jbd2_inode_cache)
2347 kmem_cache_destroy(jbd2_inode_cache);
2348
470decc6
DK
2349}
2350
2351/*
2352 * Module startup and shutdown
2353 */
2354
2355static int __init journal_init_caches(void)
2356{
2357 int ret;
2358
f7f4bccb 2359 ret = jbd2_journal_init_revoke_caches();
470decc6 2360 if (ret == 0)
f7f4bccb 2361 ret = journal_init_jbd2_journal_head_cache();
470decc6
DK
2362 if (ret == 0)
2363 ret = journal_init_handle_cache();
2364 return ret;
2365}
2366
f7f4bccb 2367static void jbd2_journal_destroy_caches(void)
470decc6 2368{
f7f4bccb
MC
2369 jbd2_journal_destroy_revoke_caches();
2370 jbd2_journal_destroy_jbd2_journal_head_cache();
2371 jbd2_journal_destroy_handle_cache();
d2eecb03 2372 jbd2_journal_destroy_slabs();
470decc6
DK
2373}
2374
2375static int __init journal_init(void)
2376{
2377 int ret;
2378
2379 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2380
2381 ret = journal_init_caches();
620de4e1
DG
2382 if (ret == 0) {
2383 jbd2_create_debugfs_entry();
2384 jbd2_create_jbd_stats_proc_entry();
2385 } else {
f7f4bccb 2386 jbd2_journal_destroy_caches();
620de4e1 2387 }
470decc6
DK
2388 return ret;
2389}
2390
2391static void __exit journal_exit(void)
2392{
e23291b9 2393#ifdef CONFIG_JBD2_DEBUG
470decc6
DK
2394 int n = atomic_read(&nr_journal_heads);
2395 if (n)
f2a44523 2396 printk(KERN_EMERG "JBD2: leaked %d journal_heads!\n", n);
470decc6 2397#endif
0f49d5d0 2398 jbd2_remove_debugfs_entry();
8e85fb3f 2399 jbd2_remove_jbd_stats_proc_entry();
f7f4bccb 2400 jbd2_journal_destroy_caches();
470decc6
DK
2401}
2402
2403MODULE_LICENSE("GPL");
2404module_init(journal_init);
2405module_exit(journal_exit);
2406