jbd: protect all log tail updates with j_checkpoint_mutex
[linux-2.6-block.git] / fs / jbd / journal.c
CommitLineData
1da177e4 1/*
f30c2269 2 * linux/fs/jbd/journal.c
1da177e4
LT
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>
28#include <linux/jbd.h>
29#include <linux/errno.h>
30#include <linux/slab.h>
1da177e4
LT
31#include <linux/init.h>
32#include <linux/mm.h>
7dfb7103 33#include <linux/freezer.h>
1da177e4 34#include <linux/pagemap.h>
8d8c8511 35#include <linux/kthread.h>
c9cf5528 36#include <linux/poison.h>
8d8c8511 37#include <linux/proc_fs.h>
c2a9159c 38#include <linux/debugfs.h>
f81e3d45 39#include <linux/ratelimit.h>
8d8c8511 40
99cb1a31
LC
41#define CREATE_TRACE_POINTS
42#include <trace/events/jbd.h>
43
1da177e4
LT
44#include <asm/uaccess.h>
45#include <asm/page.h>
1da177e4
LT
46
47EXPORT_SYMBOL(journal_start);
48EXPORT_SYMBOL(journal_restart);
49EXPORT_SYMBOL(journal_extend);
50EXPORT_SYMBOL(journal_stop);
51EXPORT_SYMBOL(journal_lock_updates);
52EXPORT_SYMBOL(journal_unlock_updates);
53EXPORT_SYMBOL(journal_get_write_access);
54EXPORT_SYMBOL(journal_get_create_access);
55EXPORT_SYMBOL(journal_get_undo_access);
56EXPORT_SYMBOL(journal_dirty_data);
57EXPORT_SYMBOL(journal_dirty_metadata);
58EXPORT_SYMBOL(journal_release_buffer);
59EXPORT_SYMBOL(journal_forget);
60#if 0
61EXPORT_SYMBOL(journal_sync_buffer);
62#endif
63EXPORT_SYMBOL(journal_flush);
64EXPORT_SYMBOL(journal_revoke);
65
66EXPORT_SYMBOL(journal_init_dev);
67EXPORT_SYMBOL(journal_init_inode);
68EXPORT_SYMBOL(journal_update_format);
69EXPORT_SYMBOL(journal_check_used_features);
70EXPORT_SYMBOL(journal_check_available_features);
71EXPORT_SYMBOL(journal_set_features);
72EXPORT_SYMBOL(journal_create);
73EXPORT_SYMBOL(journal_load);
74EXPORT_SYMBOL(journal_destroy);
1da177e4
LT
75EXPORT_SYMBOL(journal_abort);
76EXPORT_SYMBOL(journal_errno);
77EXPORT_SYMBOL(journal_ack_err);
78EXPORT_SYMBOL(journal_clear_err);
79EXPORT_SYMBOL(log_wait_commit);
ff5e4b51 80EXPORT_SYMBOL(log_start_commit);
1da177e4
LT
81EXPORT_SYMBOL(journal_start_commit);
82EXPORT_SYMBOL(journal_force_commit_nested);
83EXPORT_SYMBOL(journal_wipe);
84EXPORT_SYMBOL(journal_blocks_per_page);
85EXPORT_SYMBOL(journal_invalidatepage);
86EXPORT_SYMBOL(journal_try_to_free_buffers);
87EXPORT_SYMBOL(journal_force_commit);
88
89static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
022a4a7b 90static void __journal_abort_soft (journal_t *journal, int errno);
dff6825e 91static const char *journal_dev_name(journal_t *journal, char *buffer);
1da177e4
LT
92
93/*
94 * Helper function used to manage commit timeouts
95 */
96
97static void commit_timeout(unsigned long __data)
98{
99 struct task_struct * p = (struct task_struct *) __data;
100
101 wake_up_process(p);
102}
103
1da177e4
LT
104/*
105 * kjournald: The main thread function used to manage a logging device
106 * journal.
107 *
108 * This kernel thread is responsible for two things:
109 *
110 * 1) COMMIT: Every so often we need to commit the current state of the
111 * filesystem to disk. The journal thread is responsible for writing
112 * all of the metadata buffers to disk.
113 *
114 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115 * of the data in that part of the log has been rewritten elsewhere on
116 * the disk. Flushing these old buffers to reclaim space in the log is
117 * known as checkpointing, and this thread is responsible for that job.
118 */
119
022a4a7b 120static int kjournald(void *arg)
1da177e4 121{
e3df1898 122 journal_t *journal = arg;
1da177e4 123 transaction_t *transaction;
1da177e4 124
e3df1898
AM
125 /*
126 * Set up an interval timer which can be used to trigger a commit wakeup
127 * after the commit interval expires
128 */
129 setup_timer(&journal->j_commit_timer, commit_timeout,
130 (unsigned long)current);
1da177e4 131
35c80422
NC
132 set_freezable();
133
1da177e4
LT
134 /* Record that the journal thread is running */
135 journal->j_task = current;
136 wake_up(&journal->j_wait_done_commit);
137
138 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
139 journal->j_commit_interval / HZ);
140
141 /*
142 * And now, wait forever for commit wakeup events.
143 */
144 spin_lock(&journal->j_state_lock);
145
146loop:
147 if (journal->j_flags & JFS_UNMOUNT)
148 goto end_loop;
149
150 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
151 journal->j_commit_sequence, journal->j_commit_request);
152
153 if (journal->j_commit_sequence != journal->j_commit_request) {
154 jbd_debug(1, "OK, requests differ\n");
155 spin_unlock(&journal->j_state_lock);
e3df1898 156 del_timer_sync(&journal->j_commit_timer);
1da177e4
LT
157 journal_commit_transaction(journal);
158 spin_lock(&journal->j_state_lock);
159 goto loop;
160 }
161
162 wake_up(&journal->j_wait_done_commit);
3e1d1d28 163 if (freezing(current)) {
1da177e4
LT
164 /*
165 * The simpler the better. Flushing journal isn't a
166 * good idea, because that depends on threads that may
167 * be already stopped.
168 */
169 jbd_debug(1, "Now suspending kjournald\n");
170 spin_unlock(&journal->j_state_lock);
a0acae0e 171 try_to_freeze();
1da177e4
LT
172 spin_lock(&journal->j_state_lock);
173 } else {
174 /*
175 * We assume on resume that commits are already there,
176 * so we don't sleep
177 */
178 DEFINE_WAIT(wait);
179 int should_sleep = 1;
180
181 prepare_to_wait(&journal->j_wait_commit, &wait,
182 TASK_INTERRUPTIBLE);
183 if (journal->j_commit_sequence != journal->j_commit_request)
184 should_sleep = 0;
185 transaction = journal->j_running_transaction;
186 if (transaction && time_after_eq(jiffies,
187 transaction->t_expires))
188 should_sleep = 0;
cbf0d27a 189 if (journal->j_flags & JFS_UNMOUNT)
e9ad5620 190 should_sleep = 0;
1da177e4
LT
191 if (should_sleep) {
192 spin_unlock(&journal->j_state_lock);
193 schedule();
194 spin_lock(&journal->j_state_lock);
195 }
196 finish_wait(&journal->j_wait_commit, &wait);
197 }
198
199 jbd_debug(1, "kjournald wakes\n");
200
201 /*
202 * Were we woken up by a commit wakeup event?
203 */
204 transaction = journal->j_running_transaction;
205 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
206 journal->j_commit_request = transaction->t_tid;
207 jbd_debug(1, "woke because of timeout\n");
208 }
209 goto loop;
210
211end_loop:
212 spin_unlock(&journal->j_state_lock);
e3df1898 213 del_timer_sync(&journal->j_commit_timer);
1da177e4
LT
214 journal->j_task = NULL;
215 wake_up(&journal->j_wait_done_commit);
216 jbd_debug(1, "Journal thread exiting.\n");
217 return 0;
218}
219
97f06784 220static int journal_start_thread(journal_t *journal)
1da177e4 221{
97f06784
PE
222 struct task_struct *t;
223
224 t = kthread_run(kjournald, journal, "kjournald");
225 if (IS_ERR(t))
226 return PTR_ERR(t);
227
c80544dc 228 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
97f06784 229 return 0;
1da177e4
LT
230}
231
232static void journal_kill_thread(journal_t *journal)
233{
234 spin_lock(&journal->j_state_lock);
235 journal->j_flags |= JFS_UNMOUNT;
236
237 while (journal->j_task) {
238 wake_up(&journal->j_wait_commit);
239 spin_unlock(&journal->j_state_lock);
c80544dc
SH
240 wait_event(journal->j_wait_done_commit,
241 journal->j_task == NULL);
1da177e4
LT
242 spin_lock(&journal->j_state_lock);
243 }
244 spin_unlock(&journal->j_state_lock);
245}
246
247/*
248 * journal_write_metadata_buffer: write a metadata buffer to the journal.
249 *
250 * Writes a metadata buffer to a given disk block. The actual IO is not
251 * performed but a new buffer_head is constructed which labels the data
252 * to be written with the correct destination disk block.
253 *
254 * Any magic-number escaping which needs to be done will cause a
255 * copy-out here. If the buffer happens to start with the
256 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
257 * magic number is only written to the log for descripter blocks. In
258 * this case, we copy the data and replace the first word with 0, and we
259 * return a result code which indicates that this buffer needs to be
260 * marked as an escaped buffer in the corresponding log descriptor
261 * block. The missing word can then be restored when the block is read
262 * during recovery.
263 *
264 * If the source buffer has already been modified by a new transaction
265 * since we took the last commit snapshot, we use the frozen copy of
266 * that data for IO. If we end up using the existing buffer_head's data
267 * for the write, then we *have* to lock the buffer to prevent anyone
268 * else from using and possibly modifying it while the IO is in
269 * progress.
270 *
271 * The function returns a pointer to the buffer_heads to be used for IO.
272 *
273 * We assume that the journal has already been locked in this function.
274 *
275 * Return value:
276 * <0: Error
277 * >=0: Finished OK
278 *
279 * On success:
280 * Bit 0 set == escape performed on the data
281 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282 */
283
284int journal_write_metadata_buffer(transaction_t *transaction,
285 struct journal_head *jh_in,
286 struct journal_head **jh_out,
9c28cbcc 287 unsigned int blocknr)
1da177e4
LT
288{
289 int need_copy_out = 0;
290 int done_copy_out = 0;
291 int do_escape = 0;
292 char *mapped_data;
293 struct buffer_head *new_bh;
294 struct journal_head *new_jh;
295 struct page *new_page;
296 unsigned int new_offset;
297 struct buffer_head *bh_in = jh2bh(jh_in);
f1015c44 298 journal_t *journal = transaction->t_journal;
1da177e4
LT
299
300 /*
301 * The buffer really shouldn't be locked: only the current committing
302 * transaction is allowed to write it, so nobody else is allowed
303 * to do any IO.
304 *
305 * akpm: except if we're journalling data, and write() output is
306 * also part of a shared mapping, and another thread has
307 * decided to launch a writepage() against this buffer.
308 */
309 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
310
311 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
f1015c44 312 /* keep subsequent assertions sane */
313 new_bh->b_state = 0;
314 init_buffer(new_bh, NULL, NULL);
315 atomic_set(&new_bh->b_count, 1);
316 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
1da177e4
LT
317
318 /*
319 * If a new transaction has already done a buffer copy-out, then
320 * we use that version of the data for the commit.
321 */
322 jbd_lock_bh_state(bh_in);
323repeat:
324 if (jh_in->b_frozen_data) {
325 done_copy_out = 1;
326 new_page = virt_to_page(jh_in->b_frozen_data);
327 new_offset = offset_in_page(jh_in->b_frozen_data);
328 } else {
329 new_page = jh2bh(jh_in)->b_page;
330 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
331 }
332
8fb53c46 333 mapped_data = kmap_atomic(new_page);
1da177e4
LT
334 /*
335 * Check for escaping
336 */
337 if (*((__be32 *)(mapped_data + new_offset)) ==
338 cpu_to_be32(JFS_MAGIC_NUMBER)) {
339 need_copy_out = 1;
340 do_escape = 1;
341 }
8fb53c46 342 kunmap_atomic(mapped_data);
1da177e4
LT
343
344 /*
345 * Do we need to do a data copy?
346 */
347 if (need_copy_out && !done_copy_out) {
348 char *tmp;
349
350 jbd_unlock_bh_state(bh_in);
c089d490 351 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
1da177e4
LT
352 jbd_lock_bh_state(bh_in);
353 if (jh_in->b_frozen_data) {
c089d490 354 jbd_free(tmp, bh_in->b_size);
1da177e4
LT
355 goto repeat;
356 }
357
358 jh_in->b_frozen_data = tmp;
8fb53c46 359 mapped_data = kmap_atomic(new_page);
1da177e4 360 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
8fb53c46 361 kunmap_atomic(mapped_data);
1da177e4
LT
362
363 new_page = virt_to_page(tmp);
364 new_offset = offset_in_page(tmp);
365 done_copy_out = 1;
366 }
367
368 /*
369 * Did we need to do an escaping? Now we've done all the
370 * copying, we can finally do so.
371 */
372 if (do_escape) {
8fb53c46 373 mapped_data = kmap_atomic(new_page);
1da177e4 374 *((unsigned int *)(mapped_data + new_offset)) = 0;
8fb53c46 375 kunmap_atomic(mapped_data);
1da177e4
LT
376 }
377
1da177e4
LT
378 set_bh_page(new_bh, new_page, new_offset);
379 new_jh->b_transaction = NULL;
380 new_bh->b_size = jh2bh(jh_in)->b_size;
381 new_bh->b_bdev = transaction->t_journal->j_dev;
382 new_bh->b_blocknr = blocknr;
383 set_buffer_mapped(new_bh);
384 set_buffer_dirty(new_bh);
385
386 *jh_out = new_jh;
387
388 /*
389 * The to-be-written buffer needs to get moved to the io queue,
390 * and the original buffer whose contents we are shadowing or
391 * copying is moved to the transaction's shadow queue.
392 */
393 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
f1015c44 394 spin_lock(&journal->j_list_lock);
395 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
396 spin_unlock(&journal->j_list_lock);
397 jbd_unlock_bh_state(bh_in);
398
1da177e4
LT
399 JBUFFER_TRACE(new_jh, "file as BJ_IO");
400 journal_file_buffer(new_jh, transaction, BJ_IO);
401
402 return do_escape | (done_copy_out << 1);
403}
404
405/*
406 * Allocation code for the journal file. Manage the space left in the
407 * journal, so that we can begin checkpointing when appropriate.
408 */
409
410/*
411 * __log_space_left: Return the number of free blocks left in the journal.
412 *
413 * Called with the journal already locked.
414 *
415 * Called under j_state_lock
416 */
417
418int __log_space_left(journal_t *journal)
419{
420 int left = journal->j_free;
421
422 assert_spin_locked(&journal->j_state_lock);
423
424 /*
425 * Be pessimistic here about the number of those free blocks which
426 * might be required for log descriptor control blocks.
427 */
428
429#define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
430
431 left -= MIN_LOG_RESERVED_BLOCKS;
432
433 if (left <= 0)
434 return 0;
435 left -= (left >> 3);
436 return left;
437}
438
439/*
8fe4cd0d 440 * Called under j_state_lock. Returns true if a transaction commit was started.
1da177e4
LT
441 */
442int __log_start_commit(journal_t *journal, tid_t target)
443{
444 /*
d9b01934
TT
445 * The only transaction we can possibly wait upon is the
446 * currently running transaction (if it exists). Otherwise,
447 * the target tid must be an old one.
1da177e4 448 */
d9b01934
TT
449 if (journal->j_running_transaction &&
450 journal->j_running_transaction->t_tid == target) {
1da177e4 451 /*
bcf3d0bc 452 * We want a new commit: OK, mark the request and wakeup the
1da177e4
LT
453 * commit thread. We do _not_ do the commit ourselves.
454 */
455
456 journal->j_commit_request = target;
457 jbd_debug(1, "JBD: requesting commit %d/%d\n",
458 journal->j_commit_request,
459 journal->j_commit_sequence);
460 wake_up(&journal->j_wait_commit);
461 return 1;
d9b01934
TT
462 } else if (!tid_geq(journal->j_commit_request, target))
463 /* This should never happen, but if it does, preserve
464 the evidence before kjournald goes into a loop and
465 increments j_commit_sequence beyond all recognition. */
466 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
467 journal->j_commit_request, journal->j_commit_sequence,
468 target, journal->j_running_transaction ?
469 journal->j_running_transaction->t_tid : 0);
1da177e4
LT
470 return 0;
471}
472
473int log_start_commit(journal_t *journal, tid_t tid)
474{
475 int ret;
476
477 spin_lock(&journal->j_state_lock);
478 ret = __log_start_commit(journal, tid);
479 spin_unlock(&journal->j_state_lock);
480 return ret;
481}
482
483/*
484 * Force and wait upon a commit if the calling process is not within
485 * transaction. This is used for forcing out undo-protected data which contains
486 * bitmaps, when the fs is running out of space.
487 *
488 * We can only force the running transaction if we don't have an active handle;
489 * otherwise, we will deadlock.
490 *
491 * Returns true if a transaction was started.
492 */
493int journal_force_commit_nested(journal_t *journal)
494{
495 transaction_t *transaction = NULL;
496 tid_t tid;
497
498 spin_lock(&journal->j_state_lock);
499 if (journal->j_running_transaction && !current->journal_info) {
500 transaction = journal->j_running_transaction;
501 __log_start_commit(journal, transaction->t_tid);
502 } else if (journal->j_committing_transaction)
503 transaction = journal->j_committing_transaction;
504
505 if (!transaction) {
506 spin_unlock(&journal->j_state_lock);
507 return 0; /* Nothing to retry */
508 }
509
510 tid = transaction->t_tid;
511 spin_unlock(&journal->j_state_lock);
512 log_wait_commit(journal, tid);
513 return 1;
514}
515
516/*
517 * Start a commit of the current running transaction (if any). Returns true
8fe4cd0d
JK
518 * if a transaction is going to be committed (or is currently already
519 * committing), and fills its tid in at *ptid
1da177e4
LT
520 */
521int journal_start_commit(journal_t *journal, tid_t *ptid)
522{
523 int ret = 0;
524
525 spin_lock(&journal->j_state_lock);
526 if (journal->j_running_transaction) {
527 tid_t tid = journal->j_running_transaction->t_tid;
528
8fe4cd0d
JK
529 __log_start_commit(journal, tid);
530 /* There's a running transaction and we've just made sure
531 * it's commit has been scheduled. */
532 if (ptid)
1da177e4 533 *ptid = tid;
8fe4cd0d
JK
534 ret = 1;
535 } else if (journal->j_committing_transaction) {
1da177e4
LT
536 /*
537 * If ext3_write_super() recently started a commit, then we
538 * have to wait for completion of that transaction
539 */
8fe4cd0d
JK
540 if (ptid)
541 *ptid = journal->j_committing_transaction->t_tid;
1da177e4
LT
542 ret = 1;
543 }
544 spin_unlock(&journal->j_state_lock);
545 return ret;
546}
547
548/*
549 * Wait for a specified commit to complete.
550 * The caller may not hold the journal lock.
551 */
552int log_wait_commit(journal_t *journal, tid_t tid)
553{
554 int err = 0;
555
556#ifdef CONFIG_JBD_DEBUG
557 spin_lock(&journal->j_state_lock);
558 if (!tid_geq(journal->j_commit_request, tid)) {
559 printk(KERN_EMERG
560 "%s: error: j_commit_request=%d, tid=%d\n",
08fc99bf 561 __func__, journal->j_commit_request, tid);
1da177e4
LT
562 }
563 spin_unlock(&journal->j_state_lock);
564#endif
565 spin_lock(&journal->j_state_lock);
2db938be
JK
566 if (!tid_geq(journal->j_commit_waited, tid))
567 journal->j_commit_waited = tid;
1da177e4
LT
568 while (tid_gt(tid, journal->j_commit_sequence)) {
569 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
570 tid, journal->j_commit_sequence);
571 wake_up(&journal->j_wait_commit);
572 spin_unlock(&journal->j_state_lock);
573 wait_event(journal->j_wait_done_commit,
574 !tid_gt(tid, journal->j_commit_sequence));
575 spin_lock(&journal->j_state_lock);
576 }
577 spin_unlock(&journal->j_state_lock);
578
579 if (unlikely(is_journal_aborted(journal))) {
580 printk(KERN_EMERG "journal commit I/O error\n");
581 err = -EIO;
582 }
583 return err;
584}
585
03f4d804
JK
586/*
587 * Return 1 if a given transaction has not yet sent barrier request
588 * connected with a transaction commit. If 0 is returned, transaction
589 * may or may not have sent the barrier. Used to avoid sending barrier
590 * twice in common cases.
591 */
592int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
593{
594 int ret = 0;
595 transaction_t *commit_trans;
596
597 if (!(journal->j_flags & JFS_BARRIER))
598 return 0;
599 spin_lock(&journal->j_state_lock);
600 /* Transaction already committed? */
601 if (tid_geq(journal->j_commit_sequence, tid))
602 goto out;
603 /*
604 * Transaction is being committed and we already proceeded to
605 * writing commit record?
606 */
607 commit_trans = journal->j_committing_transaction;
608 if (commit_trans && commit_trans->t_tid == tid &&
609 commit_trans->t_state >= T_COMMIT_RECORD)
610 goto out;
611 ret = 1;
612out:
613 spin_unlock(&journal->j_state_lock);
614 return ret;
615}
52779708 616EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
03f4d804 617
1da177e4
LT
618/*
619 * Log buffer allocation routines:
620 */
621
9c28cbcc 622int journal_next_log_block(journal_t *journal, unsigned int *retp)
1da177e4 623{
9c28cbcc 624 unsigned int blocknr;
1da177e4
LT
625
626 spin_lock(&journal->j_state_lock);
627 J_ASSERT(journal->j_free > 1);
628
629 blocknr = journal->j_head;
630 journal->j_head++;
631 journal->j_free--;
632 if (journal->j_head == journal->j_last)
633 journal->j_head = journal->j_first;
634 spin_unlock(&journal->j_state_lock);
635 return journal_bmap(journal, blocknr, retp);
636}
637
638/*
639 * Conversion of logical to physical block numbers for the journal
640 *
641 * On external journals the journal blocks are identity-mapped, so
642 * this is a no-op. If needed, we can use j_blk_offset - everything is
643 * ready.
644 */
9c28cbcc
JK
645int journal_bmap(journal_t *journal, unsigned int blocknr,
646 unsigned int *retp)
1da177e4
LT
647{
648 int err = 0;
9c28cbcc 649 unsigned int ret;
1da177e4
LT
650
651 if (journal->j_inode) {
652 ret = bmap(journal->j_inode, blocknr);
653 if (ret)
654 *retp = ret;
655 else {
656 char b[BDEVNAME_SIZE];
657
658 printk(KERN_ALERT "%s: journal block not found "
9c28cbcc 659 "at offset %u on %s\n",
08fc99bf 660 __func__,
1da177e4
LT
661 blocknr,
662 bdevname(journal->j_dev, b));
663 err = -EIO;
664 __journal_abort_soft(journal, err);
665 }
666 } else {
667 *retp = blocknr; /* +journal->j_blk_offset */
668 }
669 return err;
670}
671
672/*
673 * We play buffer_head aliasing tricks to write data/metadata blocks to
674 * the journal without copying their contents, but for journal
675 * descriptor blocks we do need to generate bona fide buffers.
676 *
677 * After the caller of journal_get_descriptor_buffer() has finished modifying
678 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
679 * But we don't bother doing that, so there will be coherency problems with
680 * mmaps of blockdevs which hold live JBD-controlled filesystems.
681 */
682struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
683{
684 struct buffer_head *bh;
9c28cbcc 685 unsigned int blocknr;
1da177e4
LT
686 int err;
687
688 err = journal_next_log_block(journal, &blocknr);
689
690 if (err)
691 return NULL;
692
693 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
ecca9af0
JK
694 if (!bh)
695 return NULL;
1da177e4
LT
696 lock_buffer(bh);
697 memset(bh->b_data, 0, journal->j_blocksize);
698 set_buffer_uptodate(bh);
699 unlock_buffer(bh);
700 BUFFER_TRACE(bh, "return this buffer");
701 return journal_add_journal_head(bh);
702}
703
704/*
705 * Management for journal control blocks: functions to create and
706 * destroy journal_t structures, and to initialise and read existing
707 * journal blocks from disk. */
708
709/* First: create and setup a journal_t object in memory. We initialise
710 * very few fields yet: that has to wait until we have created the
711 * journal structures from from scratch, or loaded them from disk. */
712
713static journal_t * journal_init_common (void)
714{
715 journal_t *journal;
716 int err;
717
8c3478a5 718 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
1da177e4
LT
719 if (!journal)
720 goto fail;
1da177e4
LT
721
722 init_waitqueue_head(&journal->j_wait_transaction_locked);
723 init_waitqueue_head(&journal->j_wait_logspace);
724 init_waitqueue_head(&journal->j_wait_done_commit);
725 init_waitqueue_head(&journal->j_wait_checkpoint);
726 init_waitqueue_head(&journal->j_wait_commit);
727 init_waitqueue_head(&journal->j_wait_updates);
2c68ee75 728 mutex_init(&journal->j_checkpoint_mutex);
1da177e4
LT
729 spin_lock_init(&journal->j_revoke_lock);
730 spin_lock_init(&journal->j_list_lock);
731 spin_lock_init(&journal->j_state_lock);
732
733 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
734
735 /* The journal is marked for error until we succeed with recovery! */
736 journal->j_flags = JFS_ABORT;
737
738 /* Set up a default-sized revoke table for the new mount. */
739 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
740 if (err) {
741 kfree(journal);
742 goto fail;
743 }
744 return journal;
745fail:
746 return NULL;
747}
748
749/* journal_init_dev and journal_init_inode:
750 *
751 * Create a journal structure assigned some fixed set of disk blocks to
752 * the journal. We don't actually touch those disk blocks yet, but we
753 * need to set up all of the mapping information to tell the journaling
754 * system where the journal blocks are.
755 *
756 */
757
758/**
0cf01f66 759 * journal_t * journal_init_dev() - creates and initialises a journal structure
1da177e4
LT
760 * @bdev: Block device on which to create the journal
761 * @fs_dev: Device which hold journalled filesystem for this journal.
762 * @start: Block nr Start of journal.
37ed3222 763 * @len: Length of the journal in blocks.
1da177e4 764 * @blocksize: blocksize of journalling device
0cf01f66
RD
765 *
766 * Returns: a newly created journal_t *
ae6ddcc5 767 *
1da177e4
LT
768 * journal_init_dev creates a journal which maps a fixed contiguous
769 * range of blocks on an arbitrary block device.
ae6ddcc5 770 *
1da177e4
LT
771 */
772journal_t * journal_init_dev(struct block_device *bdev,
773 struct block_device *fs_dev,
774 int start, int len, int blocksize)
775{
776 journal_t *journal = journal_init_common();
777 struct buffer_head *bh;
778 int n;
779
780 if (!journal)
781 return NULL;
782
1da177e4 783 /* journal descriptor can store up to n blocks -bzzz */
d1807793 784 journal->j_blocksize = blocksize;
1da177e4
LT
785 n = journal->j_blocksize / sizeof(journal_block_tag_t);
786 journal->j_wbufsize = n;
787 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
788 if (!journal->j_wbuf) {
25985edc 789 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
08fc99bf 790 __func__);
ecca9af0 791 goto out_err;
1da177e4 792 }
d1807793
ZM
793 journal->j_dev = bdev;
794 journal->j_fs_dev = fs_dev;
795 journal->j_blk_offset = start;
796 journal->j_maxlen = len;
797
798 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
ecca9af0
JK
799 if (!bh) {
800 printk(KERN_ERR
801 "%s: Cannot get buffer for journal superblock\n",
802 __func__);
803 goto out_err;
804 }
d1807793
ZM
805 journal->j_sb_buffer = bh;
806 journal->j_superblock = (journal_superblock_t *)bh->b_data;
ecca9af0 807
1da177e4 808 return journal;
ecca9af0 809out_err:
7b02bec0 810 kfree(journal->j_wbuf);
ecca9af0
JK
811 kfree(journal);
812 return NULL;
1da177e4 813}
ae6ddcc5
MC
814
815/**
1da177e4
LT
816 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
817 * @inode: An inode to create the journal in
ae6ddcc5 818 *
1da177e4
LT
819 * journal_init_inode creates a journal which maps an on-disk inode as
820 * the journal. The inode must exist already, must support bmap() and
821 * must have all data blocks preallocated.
822 */
823journal_t * journal_init_inode (struct inode *inode)
824{
825 struct buffer_head *bh;
826 journal_t *journal = journal_init_common();
827 int err;
828 int n;
9c28cbcc 829 unsigned int blocknr;
1da177e4
LT
830
831 if (!journal)
832 return NULL;
833
834 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
835 journal->j_inode = inode;
836 jbd_debug(1,
837 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
ae6ddcc5 838 journal, inode->i_sb->s_id, inode->i_ino,
1da177e4
LT
839 (long long) inode->i_size,
840 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
841
842 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
843 journal->j_blocksize = inode->i_sb->s_blocksize;
844
845 /* journal descriptor can store up to n blocks -bzzz */
846 n = journal->j_blocksize / sizeof(journal_block_tag_t);
847 journal->j_wbufsize = n;
848 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
849 if (!journal->j_wbuf) {
25985edc 850 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
08fc99bf 851 __func__);
ecca9af0 852 goto out_err;
1da177e4
LT
853 }
854
855 err = journal_bmap(journal, 0, &blocknr);
856 /* If that failed, give up */
857 if (err) {
3c26bdb4 858 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
08fc99bf 859 __func__);
ecca9af0 860 goto out_err;
1da177e4
LT
861 }
862
863 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
ecca9af0
JK
864 if (!bh) {
865 printk(KERN_ERR
866 "%s: Cannot get buffer for journal superblock\n",
867 __func__);
868 goto out_err;
869 }
1da177e4
LT
870 journal->j_sb_buffer = bh;
871 journal->j_superblock = (journal_superblock_t *)bh->b_data;
872
873 return journal;
ecca9af0 874out_err:
7b02bec0 875 kfree(journal->j_wbuf);
ecca9af0
JK
876 kfree(journal);
877 return NULL;
1da177e4
LT
878}
879
ae6ddcc5 880/*
1da177e4
LT
881 * If the journal init or create aborts, we need to mark the journal
882 * superblock as being NULL to prevent the journal destroy from writing
ae6ddcc5 883 * back a bogus superblock.
1da177e4
LT
884 */
885static void journal_fail_superblock (journal_t *journal)
886{
887 struct buffer_head *bh = journal->j_sb_buffer;
888 brelse(bh);
889 journal->j_sb_buffer = NULL;
890}
891
892/*
893 * Given a journal_t structure, initialise the various fields for
894 * startup of a new journaling session. We use this both when creating
895 * a journal, and after recovering an old journal to reset it for
896 * subsequent use.
897 */
898
899static int journal_reset(journal_t *journal)
900{
901 journal_superblock_t *sb = journal->j_superblock;
9c28cbcc 902 unsigned int first, last;
1da177e4
LT
903
904 first = be32_to_cpu(sb->s_first);
905 last = be32_to_cpu(sb->s_maxlen);
7447a668 906 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
9c28cbcc 907 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
7447a668
JK
908 first, last);
909 journal_fail_superblock(journal);
910 return -EINVAL;
911 }
1da177e4
LT
912
913 journal->j_first = first;
914 journal->j_last = last;
915
916 journal->j_head = first;
917 journal->j_tail = first;
918 journal->j_free = last - first;
919
920 journal->j_tail_sequence = journal->j_transaction_sequence;
921 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
922 journal->j_commit_request = journal->j_commit_sequence;
923
924 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
925
9754e39c
JK
926 /*
927 * As a special case, if the on-disk copy is already marked as needing
928 * no recovery (s_start == 0), then we can safely defer the superblock
929 * update until the next commit by setting JFS_FLUSHED. This avoids
930 * attempting a write to a potential-readonly device.
931 */
932 if (sb->s_start == 0) {
933 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
934 "(start %u, seq %d, errno %d)\n",
935 journal->j_tail, journal->j_tail_sequence,
936 journal->j_errno);
937 journal->j_flags |= JFS_FLUSHED;
938 } else {
1ce8486d
JK
939 /* Lock here to make assertions happy... */
940 mutex_lock(&journal->j_checkpoint_mutex);
9754e39c
JK
941 /* Add the dynamic fields and write it to disk. */
942 journal_update_sb_log_tail(journal);
1ce8486d 943 mutex_unlock(&journal->j_checkpoint_mutex);
9754e39c 944 }
97f06784 945 return journal_start_thread(journal);
1da177e4
LT
946}
947
ae6ddcc5 948/**
1da177e4
LT
949 * int journal_create() - Initialise the new journal file
950 * @journal: Journal to create. This structure must have been initialised
ae6ddcc5 951 *
1da177e4
LT
952 * Given a journal_t structure which tells us which disk blocks we can
953 * use, create a new journal superblock and initialise all of the
ae6ddcc5 954 * journal fields from scratch.
1da177e4
LT
955 **/
956int journal_create(journal_t *journal)
957{
9c28cbcc 958 unsigned int blocknr;
1da177e4
LT
959 struct buffer_head *bh;
960 journal_superblock_t *sb;
961 int i, err;
962
963 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
964 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
965 journal->j_maxlen);
966 journal_fail_superblock(journal);
967 return -EINVAL;
968 }
969
970 if (journal->j_inode == NULL) {
971 /*
972 * We don't know what block to start at!
973 */
974 printk(KERN_EMERG
975 "%s: creation of journal on external device!\n",
08fc99bf 976 __func__);
1da177e4
LT
977 BUG();
978 }
979
980 /* Zero out the entire journal on disk. We cannot afford to
981 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
982 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
983 for (i = 0; i < journal->j_maxlen; i++) {
984 err = journal_bmap(journal, i, &blocknr);
985 if (err)
986 return err;
987 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
2a0e3388
NK
988 if (unlikely(!bh))
989 return -ENOMEM;
1da177e4
LT
990 lock_buffer(bh);
991 memset (bh->b_data, 0, journal->j_blocksize);
992 BUFFER_TRACE(bh, "marking dirty");
993 mark_buffer_dirty(bh);
994 BUFFER_TRACE(bh, "marking uptodate");
995 set_buffer_uptodate(bh);
996 unlock_buffer(bh);
997 __brelse(bh);
998 }
999
1000 sync_blockdev(journal->j_dev);
1001 jbd_debug(1, "JBD: journal cleared.\n");
1002
1003 /* OK, fill in the initial static fields in the new superblock */
1004 sb = journal->j_superblock;
1005
1006 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
1007 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1008
1009 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
1010 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
1011 sb->s_first = cpu_to_be32(1);
1012
1013 journal->j_transaction_sequence = 1;
1014
1015 journal->j_flags &= ~JFS_ABORT;
1016 journal->j_format_version = 2;
1017
1018 return journal_reset(journal);
1019}
1020
9754e39c 1021static void journal_write_superblock(journal_t *journal)
1da177e4 1022{
1da177e4
LT
1023 struct buffer_head *bh = journal->j_sb_buffer;
1024
9754e39c 1025 trace_journal_write_superblock(journal);
dff6825e
DW
1026 if (buffer_write_io_error(bh)) {
1027 char b[BDEVNAME_SIZE];
1028 /*
1029 * Oh, dear. A previous attempt to write the journal
1030 * superblock failed. This could happen because the
1031 * USB device was yanked out. Or it could happen to
1032 * be a transient write error and maybe the block will
1033 * be remapped. Nothing we can do but to retry the
1034 * write and hope for the best.
1035 */
1036 printk(KERN_ERR "JBD: previous I/O error detected "
1037 "for journal superblock update for %s.\n",
1038 journal_dev_name(journal, b));
1039 clear_buffer_write_io_error(bh);
1040 set_buffer_uptodate(bh);
1041 }
1042
9754e39c
JK
1043 BUFFER_TRACE(bh, "marking dirty");
1044 mark_buffer_dirty(bh);
1045 sync_dirty_buffer(bh);
1046 if (buffer_write_io_error(bh)) {
1047 char b[BDEVNAME_SIZE];
1048 printk(KERN_ERR "JBD: I/O error detected "
1049 "when updating journal superblock for %s.\n",
1050 journal_dev_name(journal, b));
1051 clear_buffer_write_io_error(bh);
1052 set_buffer_uptodate(bh);
1053 }
1054}
1055
1056/**
1057 * journal_update_sb_log_tail() - Update log tail in journal sb on disk.
1058 * @journal: The journal to update.
1059 *
1060 * Update a journal's superblock information about log tail and write it to
1061 * disk, waiting for the IO to complete.
1062 */
1063void journal_update_sb_log_tail(journal_t *journal)
1064{
1065 journal_superblock_t *sb = journal->j_superblock;
1066
1ce8486d 1067 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1da177e4 1068 spin_lock(&journal->j_state_lock);
9c28cbcc 1069 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
1da177e4
LT
1070 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1071
1072 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1073 sb->s_start = cpu_to_be32(journal->j_tail);
1da177e4
LT
1074 spin_unlock(&journal->j_state_lock);
1075
9754e39c 1076 journal_write_superblock(journal);
1da177e4 1077
9754e39c
JK
1078 /* Log is no longer empty */
1079 spin_lock(&journal->j_state_lock);
1080 WARN_ON(!sb->s_sequence);
1081 journal->j_flags &= ~JFS_FLUSHED;
1082 spin_unlock(&journal->j_state_lock);
1083}
1084
1085/**
1086 * mark_journal_empty() - Mark on disk journal as empty.
1087 * @journal: The journal to update.
1088 *
1089 * Update a journal's dynamic superblock fields to show that journal is empty.
1090 * Write updated superblock to disk waiting for IO to complete.
1091 */
1092static void mark_journal_empty(journal_t *journal)
1093{
1094 journal_superblock_t *sb = journal->j_superblock;
1da177e4 1095
1ce8486d 1096 BUG_ON(!mutex_is_locked(&journal->j_checkpoint_mutex));
1da177e4 1097 spin_lock(&journal->j_state_lock);
9754e39c
JK
1098 jbd_debug(1, "JBD: Marking journal as empty (seq %d)\n",
1099 journal->j_tail_sequence);
1100
1101 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1102 sb->s_start = cpu_to_be32(0);
1103 spin_unlock(&journal->j_state_lock);
1104
1105 journal_write_superblock(journal);
1106
1107 spin_lock(&journal->j_state_lock);
1108 /* Log is empty */
1109 journal->j_flags |= JFS_FLUSHED;
1da177e4
LT
1110 spin_unlock(&journal->j_state_lock);
1111}
1112
9754e39c
JK
1113/**
1114 * journal_update_sb_errno() - Update error in the journal.
1115 * @journal: The journal to update.
1116 *
1117 * Update a journal's errno. Write updated superblock to disk waiting for IO
1118 * to complete.
1119 */
1120static void journal_update_sb_errno(journal_t *journal)
1121{
1122 journal_superblock_t *sb = journal->j_superblock;
1123
1124 spin_lock(&journal->j_state_lock);
1125 jbd_debug(1, "JBD: updating superblock error (errno %d)\n",
1126 journal->j_errno);
1127 sb->s_errno = cpu_to_be32(journal->j_errno);
1128 spin_unlock(&journal->j_state_lock);
1129
1130 journal_write_superblock(journal);
1131}
1132
1da177e4
LT
1133/*
1134 * Read the superblock for a given journal, performing initial
1135 * validation of the format.
1136 */
1137
1138static int journal_get_superblock(journal_t *journal)
1139{
1140 struct buffer_head *bh;
1141 journal_superblock_t *sb;
1142 int err = -EIO;
1143
1144 bh = journal->j_sb_buffer;
1145
1146 J_ASSERT(bh != NULL);
1147 if (!buffer_uptodate(bh)) {
1148 ll_rw_block(READ, 1, &bh);
1149 wait_on_buffer(bh);
1150 if (!buffer_uptodate(bh)) {
1151 printk (KERN_ERR
1152 "JBD: IO error reading journal superblock\n");
1153 goto out;
1154 }
1155 }
1156
1157 sb = journal->j_superblock;
1158
1159 err = -EINVAL;
1160
1161 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1162 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1163 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1164 goto out;
1165 }
1166
1167 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1168 case JFS_SUPERBLOCK_V1:
1169 journal->j_format_version = 1;
1170 break;
1171 case JFS_SUPERBLOCK_V2:
1172 journal->j_format_version = 2;
1173 break;
1174 default:
1175 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1176 goto out;
1177 }
1178
1179 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1180 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1181 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1182 printk (KERN_WARNING "JBD: journal file too short\n");
1183 goto out;
1184 }
1185
8762202d
EG
1186 if (be32_to_cpu(sb->s_first) == 0 ||
1187 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1188 printk(KERN_WARNING
1189 "JBD: Invalid start block of journal: %u\n",
1190 be32_to_cpu(sb->s_first));
1191 goto out;
1192 }
1193
1da177e4
LT
1194 return 0;
1195
1196out:
1197 journal_fail_superblock(journal);
1198 return err;
1199}
1200
1201/*
1202 * Load the on-disk journal superblock and read the key fields into the
1203 * journal_t.
1204 */
1205
1206static int load_superblock(journal_t *journal)
1207{
1208 int err;
1209 journal_superblock_t *sb;
1210
1211 err = journal_get_superblock(journal);
1212 if (err)
1213 return err;
1214
1215 sb = journal->j_superblock;
1216
1217 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1218 journal->j_tail = be32_to_cpu(sb->s_start);
1219 journal->j_first = be32_to_cpu(sb->s_first);
1220 journal->j_last = be32_to_cpu(sb->s_maxlen);
1221 journal->j_errno = be32_to_cpu(sb->s_errno);
1222
1223 return 0;
1224}
1225
1226
1227/**
1228 * int journal_load() - Read journal from disk.
1229 * @journal: Journal to act on.
ae6ddcc5 1230 *
1da177e4
LT
1231 * Given a journal_t structure which tells us which disk blocks contain
1232 * a journal, read the journal from disk to initialise the in-memory
1233 * structures.
1234 */
1235int journal_load(journal_t *journal)
1236{
1237 int err;
ea817398 1238 journal_superblock_t *sb;
1da177e4
LT
1239
1240 err = load_superblock(journal);
1241 if (err)
1242 return err;
1243
ea817398 1244 sb = journal->j_superblock;
1da177e4
LT
1245 /* If this is a V2 superblock, then we have to check the
1246 * features flags on it. */
1247
1248 if (journal->j_format_version >= 2) {
1da177e4
LT
1249 if ((sb->s_feature_ro_compat &
1250 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1251 (sb->s_feature_incompat &
1252 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1253 printk (KERN_WARNING
1254 "JBD: Unrecognised features on journal\n");
1255 return -EINVAL;
1256 }
1257 }
1258
1259 /* Let the recovery code check whether it needs to recover any
1260 * data from the journal. */
1261 if (journal_recover(journal))
1262 goto recovery_error;
1263
1264 /* OK, we've finished with the dynamic journal bits:
1265 * reinitialise the dynamic contents of the superblock in memory
1266 * and reset them on disk. */
1267 if (journal_reset(journal))
1268 goto recovery_error;
1269
1270 journal->j_flags &= ~JFS_ABORT;
1271 journal->j_flags |= JFS_LOADED;
1272 return 0;
1273
1274recovery_error:
1275 printk (KERN_WARNING "JBD: recovery failed\n");
1276 return -EIO;
1277}
1278
1279/**
1280 * void journal_destroy() - Release a journal_t structure.
1281 * @journal: Journal to act on.
1282 *
1283 * Release a journal_t structure once it is no longer in use by the
1284 * journaled object.
4afe9785 1285 * Return <0 if we couldn't clean up the journal.
1da177e4 1286 */
4afe9785 1287int journal_destroy(journal_t *journal)
1da177e4 1288{
4afe9785
HK
1289 int err = 0;
1290
03f4d804 1291
1da177e4
LT
1292 /* Wait for the commit thread to wake up and die. */
1293 journal_kill_thread(journal);
1294
1295 /* Force a final log commit */
1296 if (journal->j_running_transaction)
1297 journal_commit_transaction(journal);
1298
1299 /* Force any old transactions to disk */
1300
1ce8486d
JK
1301 /* We cannot race with anybody but must keep assertions happy */
1302 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4
LT
1303 /* Totally anal locking here... */
1304 spin_lock(&journal->j_list_lock);
1305 while (journal->j_checkpoint_transactions != NULL) {
1306 spin_unlock(&journal->j_list_lock);
1307 log_do_checkpoint(journal);
1308 spin_lock(&journal->j_list_lock);
1309 }
1310
1311 J_ASSERT(journal->j_running_transaction == NULL);
1312 J_ASSERT(journal->j_committing_transaction == NULL);
1313 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1314 spin_unlock(&journal->j_list_lock);
1315
1da177e4 1316 if (journal->j_sb_buffer) {
4afe9785 1317 if (!is_journal_aborted(journal)) {
4afe9785
HK
1318 journal->j_tail_sequence =
1319 ++journal->j_transaction_sequence;
9754e39c
JK
1320 mark_journal_empty(journal);
1321 } else
4afe9785 1322 err = -EIO;
1da177e4
LT
1323 brelse(journal->j_sb_buffer);
1324 }
1ce8486d 1325 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4
LT
1326
1327 if (journal->j_inode)
1328 iput(journal->j_inode);
1329 if (journal->j_revoke)
1330 journal_destroy_revoke(journal);
1331 kfree(journal->j_wbuf);
1332 kfree(journal);
4afe9785
HK
1333
1334 return err;
1da177e4
LT
1335}
1336
1337
1338/**
1339 *int journal_check_used_features () - Check if features specified are used.
1340 * @journal: Journal to check.
1341 * @compat: bitmask of compatible features
1342 * @ro: bitmask of features that force read-only mount
1343 * @incompat: bitmask of incompatible features
ae6ddcc5 1344 *
1da177e4 1345 * Check whether the journal uses all of a given set of
ae6ddcc5 1346 * features. Return true (non-zero) if it does.
1da177e4
LT
1347 **/
1348
1349int journal_check_used_features (journal_t *journal, unsigned long compat,
1350 unsigned long ro, unsigned long incompat)
1351{
1352 journal_superblock_t *sb;
1353
1354 if (!compat && !ro && !incompat)
1355 return 1;
1356 if (journal->j_format_version == 1)
1357 return 0;
1358
1359 sb = journal->j_superblock;
1360
1361 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1362 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1363 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1364 return 1;
1365
1366 return 0;
1367}
1368
1369/**
1370 * int journal_check_available_features() - Check feature set in journalling layer
1371 * @journal: Journal to check.
1372 * @compat: bitmask of compatible features
1373 * @ro: bitmask of features that force read-only mount
1374 * @incompat: bitmask of incompatible features
ae6ddcc5 1375 *
1da177e4
LT
1376 * Check whether the journaling code supports the use of
1377 * all of a given set of features on this journal. Return true
1378 * (non-zero) if it can. */
1379
1380int journal_check_available_features (journal_t *journal, unsigned long compat,
1381 unsigned long ro, unsigned long incompat)
1382{
1da177e4
LT
1383 if (!compat && !ro && !incompat)
1384 return 1;
1385
1da177e4
LT
1386 /* We can support any known requested features iff the
1387 * superblock is in version 2. Otherwise we fail to support any
1388 * extended sb features. */
1389
1390 if (journal->j_format_version != 2)
1391 return 0;
1392
1393 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1394 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1395 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1396 return 1;
1397
1398 return 0;
1399}
1400
1401/**
1402 * int journal_set_features () - Mark a given journal feature in the superblock
1403 * @journal: Journal to act on.
1404 * @compat: bitmask of compatible features
1405 * @ro: bitmask of features that force read-only mount
1406 * @incompat: bitmask of incompatible features
1407 *
1408 * Mark a given journal feature as present on the
ae6ddcc5 1409 * superblock. Returns true if the requested features could be set.
1da177e4
LT
1410 *
1411 */
1412
1413int journal_set_features (journal_t *journal, unsigned long compat,
1414 unsigned long ro, unsigned long incompat)
1415{
1416 journal_superblock_t *sb;
1417
1418 if (journal_check_used_features(journal, compat, ro, incompat))
1419 return 1;
1420
1421 if (!journal_check_available_features(journal, compat, ro, incompat))
1422 return 0;
1423
1424 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1425 compat, ro, incompat);
1426
1427 sb = journal->j_superblock;
1428
1429 sb->s_feature_compat |= cpu_to_be32(compat);
1430 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1431 sb->s_feature_incompat |= cpu_to_be32(incompat);
1432
1433 return 1;
1434}
1435
1436
1437/**
1438 * int journal_update_format () - Update on-disk journal structure.
1439 * @journal: Journal to act on.
1440 *
1441 * Given an initialised but unloaded journal struct, poke about in the
1442 * on-disk structure to update it to the most recent supported version.
1443 */
1444int journal_update_format (journal_t *journal)
1445{
1446 journal_superblock_t *sb;
1447 int err;
1448
1449 err = journal_get_superblock(journal);
1450 if (err)
1451 return err;
1452
1453 sb = journal->j_superblock;
1454
1455 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1456 case JFS_SUPERBLOCK_V2:
1457 return 0;
1458 case JFS_SUPERBLOCK_V1:
1459 return journal_convert_superblock_v1(journal, sb);
1460 default:
1461 break;
1462 }
1463 return -EINVAL;
1464}
1465
1466static int journal_convert_superblock_v1(journal_t *journal,
1467 journal_superblock_t *sb)
1468{
1469 int offset, blocksize;
1470 struct buffer_head *bh;
1471
1472 printk(KERN_WARNING
1473 "JBD: Converting superblock from version 1 to 2.\n");
1474
1475 /* Pre-initialise new fields to zero */
1476 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1477 blocksize = be32_to_cpu(sb->s_blocksize);
1478 memset(&sb->s_feature_compat, 0, blocksize-offset);
1479
1480 sb->s_nr_users = cpu_to_be32(1);
1481 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1482 journal->j_format_version = 2;
1483
1484 bh = journal->j_sb_buffer;
1485 BUFFER_TRACE(bh, "marking dirty");
1486 mark_buffer_dirty(bh);
1487 sync_dirty_buffer(bh);
1488 return 0;
1489}
1490
1491
1492/**
1493 * int journal_flush () - Flush journal
1494 * @journal: Journal to act on.
ae6ddcc5 1495 *
1da177e4
LT
1496 * Flush all data for a given journal to disk and empty the journal.
1497 * Filesystems can use this when remounting readonly to ensure that
1498 * recovery does not need to happen on remount.
1499 */
1500
1501int journal_flush(journal_t *journal)
1502{
1503 int err = 0;
1504 transaction_t *transaction = NULL;
1da177e4
LT
1505
1506 spin_lock(&journal->j_state_lock);
1507
1508 /* Force everything buffered to the log... */
1509 if (journal->j_running_transaction) {
1510 transaction = journal->j_running_transaction;
1511 __log_start_commit(journal, transaction->t_tid);
1512 } else if (journal->j_committing_transaction)
1513 transaction = journal->j_committing_transaction;
1514
1515 /* Wait for the log commit to complete... */
1516 if (transaction) {
1517 tid_t tid = transaction->t_tid;
1518
1519 spin_unlock(&journal->j_state_lock);
1520 log_wait_commit(journal, tid);
1521 } else {
1522 spin_unlock(&journal->j_state_lock);
1523 }
1524
1525 /* ...and flush everything in the log out to disk. */
1526 spin_lock(&journal->j_list_lock);
1527 while (!err && journal->j_checkpoint_transactions != NULL) {
1528 spin_unlock(&journal->j_list_lock);
4afe9785 1529 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4 1530 err = log_do_checkpoint(journal);
4afe9785 1531 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4
LT
1532 spin_lock(&journal->j_list_lock);
1533 }
1534 spin_unlock(&journal->j_list_lock);
4afe9785
HK
1535
1536 if (is_journal_aborted(journal))
1537 return -EIO;
1538
1ce8486d 1539 mutex_lock(&journal->j_checkpoint_mutex);
1da177e4
LT
1540 cleanup_journal_tail(journal);
1541
1542 /* Finally, mark the journal as really needing no recovery.
1543 * This sets s_start==0 in the underlying superblock, which is
1544 * the magic code for a fully-recovered superblock. Any future
1545 * commits of data to the journal will restore the current
1546 * s_start value. */
9754e39c 1547 mark_journal_empty(journal);
1ce8486d 1548 mutex_unlock(&journal->j_checkpoint_mutex);
1da177e4 1549 spin_lock(&journal->j_state_lock);
1da177e4
LT
1550 J_ASSERT(!journal->j_running_transaction);
1551 J_ASSERT(!journal->j_committing_transaction);
1552 J_ASSERT(!journal->j_checkpoint_transactions);
1553 J_ASSERT(journal->j_head == journal->j_tail);
1554 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1555 spin_unlock(&journal->j_state_lock);
4afe9785 1556 return 0;
1da177e4
LT
1557}
1558
1559/**
1560 * int journal_wipe() - Wipe journal contents
1561 * @journal: Journal to act on.
1562 * @write: flag (see below)
ae6ddcc5 1563 *
1da177e4
LT
1564 * Wipe out all of the contents of a journal, safely. This will produce
1565 * a warning if the journal contains any valid recovery information.
1566 * Must be called between journal_init_*() and journal_load().
1567 *
1568 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1569 * we merely suppress recovery.
1570 */
1571
1572int journal_wipe(journal_t *journal, int write)
1573{
1da177e4
LT
1574 int err = 0;
1575
1576 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1577
1578 err = load_superblock(journal);
1579 if (err)
1580 return err;
1581
1da177e4
LT
1582 if (!journal->j_tail)
1583 goto no_recovery;
1584
1585 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1586 write ? "Clearing" : "Ignoring");
1587
1588 err = journal_skip_recovery(journal);
1ce8486d
JK
1589 if (write) {
1590 /* Lock to make assertions happy... */
1591 mutex_lock(&journal->j_checkpoint_mutex);
9754e39c 1592 mark_journal_empty(journal);
1ce8486d
JK
1593 mutex_unlock(&journal->j_checkpoint_mutex);
1594 }
1da177e4
LT
1595
1596 no_recovery:
1597 return err;
1598}
1599
1600/*
1601 * journal_dev_name: format a character string to describe on what
1602 * device this journal is present.
1603 */
1604
022a4a7b 1605static const char *journal_dev_name(journal_t *journal, char *buffer)
1da177e4
LT
1606{
1607 struct block_device *bdev;
1608
1609 if (journal->j_inode)
1610 bdev = journal->j_inode->i_sb->s_bdev;
1611 else
1612 bdev = journal->j_dev;
1613
1614 return bdevname(bdev, buffer);
1615}
1616
1617/*
1618 * Journal abort has very specific semantics, which we describe
ae6ddcc5 1619 * for journal abort.
1da177e4
LT
1620 *
1621 * Two internal function, which provide abort to te jbd layer
1622 * itself are here.
1623 */
1624
1625/*
1626 * Quick version for internal journal use (doesn't lock the journal).
1627 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1628 * and don't attempt to make any other journal updates.
1629 */
53308383 1630static void __journal_abort_hard(journal_t *journal)
1da177e4
LT
1631{
1632 transaction_t *transaction;
1633 char b[BDEVNAME_SIZE];
1634
1635 if (journal->j_flags & JFS_ABORT)
1636 return;
1637
1638 printk(KERN_ERR "Aborting journal on device %s.\n",
1639 journal_dev_name(journal, b));
1640
1641 spin_lock(&journal->j_state_lock);
1642 journal->j_flags |= JFS_ABORT;
1643 transaction = journal->j_running_transaction;
1644 if (transaction)
1645 __log_start_commit(journal, transaction->t_tid);
1646 spin_unlock(&journal->j_state_lock);
1647}
1648
1649/* Soft abort: record the abort error status in the journal superblock,
1650 * but don't do any other IO. */
022a4a7b 1651static void __journal_abort_soft (journal_t *journal, int errno)
1da177e4
LT
1652{
1653 if (journal->j_flags & JFS_ABORT)
1654 return;
1655
1656 if (!journal->j_errno)
1657 journal->j_errno = errno;
1658
1659 __journal_abort_hard(journal);
1660
1661 if (errno)
9754e39c 1662 journal_update_sb_errno(journal);
1da177e4
LT
1663}
1664
1665/**
1666 * void journal_abort () - Shutdown the journal immediately.
1667 * @journal: the journal to shutdown.
1668 * @errno: an error number to record in the journal indicating
1669 * the reason for the shutdown.
1670 *
1671 * Perform a complete, immediate shutdown of the ENTIRE
1672 * journal (not of a single transaction). This operation cannot be
1673 * undone without closing and reopening the journal.
ae6ddcc5 1674 *
1da177e4
LT
1675 * The journal_abort function is intended to support higher level error
1676 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1677 * mode.
1678 *
1679 * Journal abort has very specific semantics. Any existing dirty,
1680 * unjournaled buffers in the main filesystem will still be written to
1681 * disk by bdflush, but the journaling mechanism will be suspended
1682 * immediately and no further transaction commits will be honoured.
1683 *
1684 * Any dirty, journaled buffers will be written back to disk without
1685 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1686 * filesystem, but we _do_ attempt to leave as much data as possible
1687 * behind for fsck to use for cleanup.
1688 *
1689 * Any attempt to get a new transaction handle on a journal which is in
1690 * ABORT state will just result in an -EROFS error return. A
1691 * journal_stop on an existing handle will return -EIO if we have
1692 * entered abort state during the update.
1693 *
1694 * Recursive transactions are not disturbed by journal abort until the
1695 * final journal_stop, which will receive the -EIO error.
1696 *
1697 * Finally, the journal_abort call allows the caller to supply an errno
1698 * which will be recorded (if possible) in the journal superblock. This
1699 * allows a client to record failure conditions in the middle of a
1700 * transaction without having to complete the transaction to record the
1701 * failure to disk. ext3_error, for example, now uses this
1702 * functionality.
1703 *
1704 * Errors which originate from within the journaling layer will NOT
1705 * supply an errno; a null errno implies that absolutely no further
1706 * writes are done to the journal (unless there are any already in
1707 * progress).
ae6ddcc5 1708 *
1da177e4
LT
1709 */
1710
1711void journal_abort(journal_t *journal, int errno)
1712{
1713 __journal_abort_soft(journal, errno);
1714}
1715
ae6ddcc5 1716/**
1da177e4
LT
1717 * int journal_errno () - returns the journal's error state.
1718 * @journal: journal to examine.
1719 *
1720 * This is the errno numbet set with journal_abort(), the last
1721 * time the journal was mounted - if the journal was stopped
1722 * without calling abort this will be 0.
1723 *
1724 * If the journal has been aborted on this mount time -EROFS will
1725 * be returned.
1726 */
1727int journal_errno(journal_t *journal)
1728{
1729 int err;
1730
1731 spin_lock(&journal->j_state_lock);
1732 if (journal->j_flags & JFS_ABORT)
1733 err = -EROFS;
1734 else
1735 err = journal->j_errno;
1736 spin_unlock(&journal->j_state_lock);
1737 return err;
1738}
1739
ae6ddcc5 1740/**
1da177e4
LT
1741 * int journal_clear_err () - clears the journal's error state
1742 * @journal: journal to act on.
1743 *
1744 * An error must be cleared or Acked to take a FS out of readonly
1745 * mode.
1746 */
1747int journal_clear_err(journal_t *journal)
1748{
1749 int err = 0;
1750
1751 spin_lock(&journal->j_state_lock);
1752 if (journal->j_flags & JFS_ABORT)
1753 err = -EROFS;
1754 else
1755 journal->j_errno = 0;
1756 spin_unlock(&journal->j_state_lock);
1757 return err;
1758}
1759
ae6ddcc5 1760/**
1da177e4
LT
1761 * void journal_ack_err() - Ack journal err.
1762 * @journal: journal to act on.
1763 *
1764 * An error must be cleared or Acked to take a FS out of readonly
1765 * mode.
1766 */
1767void journal_ack_err(journal_t *journal)
1768{
1769 spin_lock(&journal->j_state_lock);
1770 if (journal->j_errno)
1771 journal->j_flags |= JFS_ACK_ERR;
1772 spin_unlock(&journal->j_state_lock);
1773}
1774
1775int journal_blocks_per_page(struct inode *inode)
1776{
1777 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1778}
1779
1da177e4
LT
1780/*
1781 * Journal_head storage management
1782 */
e18b890b 1783static struct kmem_cache *journal_head_cache;
1da177e4
LT
1784#ifdef CONFIG_JBD_DEBUG
1785static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1786#endif
1787
1788static int journal_init_journal_head_cache(void)
1789{
1790 int retval;
1791
1076d17a 1792 J_ASSERT(journal_head_cache == NULL);
1da177e4
LT
1793 journal_head_cache = kmem_cache_create("journal_head",
1794 sizeof(struct journal_head),
1795 0, /* offset */
e12ba74d 1796 SLAB_TEMPORARY, /* flags */
20c2df83 1797 NULL); /* ctor */
1da177e4 1798 retval = 0;
1076d17a 1799 if (!journal_head_cache) {
1da177e4
LT
1800 retval = -ENOMEM;
1801 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1802 }
1803 return retval;
1804}
1805
1806static void journal_destroy_journal_head_cache(void)
1807{
3850f7a5
DG
1808 if (journal_head_cache) {
1809 kmem_cache_destroy(journal_head_cache);
1810 journal_head_cache = NULL;
1811 }
1da177e4
LT
1812}
1813
1814/*
1815 * journal_head splicing and dicing
1816 */
1817static struct journal_head *journal_alloc_journal_head(void)
1818{
1819 struct journal_head *ret;
1da177e4
LT
1820
1821#ifdef CONFIG_JBD_DEBUG
1822 atomic_inc(&nr_journal_heads);
1823#endif
1824 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
c80544dc 1825 if (ret == NULL) {
1da177e4 1826 jbd_debug(1, "out of memory for journal_head\n");
f81e3d45
NK
1827 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1828 __func__);
1829
c80544dc 1830 while (ret == NULL) {
1da177e4
LT
1831 yield();
1832 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1833 }
1834 }
1835 return ret;
1836}
1837
1838static void journal_free_journal_head(struct journal_head *jh)
1839{
1840#ifdef CONFIG_JBD_DEBUG
1841 atomic_dec(&nr_journal_heads);
c9cf5528 1842 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1da177e4
LT
1843#endif
1844 kmem_cache_free(journal_head_cache, jh);
1845}
1846
1847/*
1848 * A journal_head is attached to a buffer_head whenever JBD has an
1849 * interest in the buffer.
1850 *
1851 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1852 * is set. This bit is tested in core kernel code where we need to take
1853 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1854 * there.
1855 *
1856 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1857 *
1858 * When a buffer has its BH_JBD bit set it is immune from being released by
1859 * core kernel code, mainly via ->b_count.
1860 *
bb189247
JK
1861 * A journal_head is detached from its buffer_head when the journal_head's
1862 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1863 * transaction (b_cp_transaction) hold their references to b_jcount.
1da177e4
LT
1864 *
1865 * Various places in the kernel want to attach a journal_head to a buffer_head
1866 * _before_ attaching the journal_head to a transaction. To protect the
1867 * journal_head in this situation, journal_add_journal_head elevates the
1868 * journal_head's b_jcount refcount by one. The caller must call
1869 * journal_put_journal_head() to undo this.
1870 *
1871 * So the typical usage would be:
1872 *
1873 * (Attach a journal_head if needed. Increments b_jcount)
1874 * struct journal_head *jh = journal_add_journal_head(bh);
1875 * ...
bb189247
JK
1876 * (Get another reference for transaction)
1877 * journal_grab_journal_head(bh);
1878 * jh->b_transaction = xxx;
1879 * (Put original reference)
1880 * journal_put_journal_head(jh);
1da177e4
LT
1881 */
1882
1883/*
1884 * Give a buffer_head a journal_head.
1885 *
1da177e4
LT
1886 * May sleep.
1887 */
1888struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1889{
1890 struct journal_head *jh;
1891 struct journal_head *new_jh = NULL;
1892
1893repeat:
1894 if (!buffer_jbd(bh)) {
1895 new_jh = journal_alloc_journal_head();
1896 memset(new_jh, 0, sizeof(*new_jh));
1897 }
1898
1899 jbd_lock_bh_journal_head(bh);
1900 if (buffer_jbd(bh)) {
1901 jh = bh2jh(bh);
1902 } else {
1903 J_ASSERT_BH(bh,
1904 (atomic_read(&bh->b_count) > 0) ||
1905 (bh->b_page && bh->b_page->mapping));
1906
1907 if (!new_jh) {
1908 jbd_unlock_bh_journal_head(bh);
1909 goto repeat;
1910 }
1911
1912 jh = new_jh;
1913 new_jh = NULL; /* We consumed it */
1914 set_buffer_jbd(bh);
1915 bh->b_private = jh;
1916 jh->b_bh = bh;
1917 get_bh(bh);
1918 BUFFER_TRACE(bh, "added journal_head");
1919 }
1920 jh->b_jcount++;
1921 jbd_unlock_bh_journal_head(bh);
1922 if (new_jh)
1923 journal_free_journal_head(new_jh);
1924 return bh->b_private;
1925}
1926
1927/*
1928 * Grab a ref against this buffer_head's journal_head. If it ended up not
1929 * having a journal_head, return NULL
1930 */
1931struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1932{
1933 struct journal_head *jh = NULL;
1934
1935 jbd_lock_bh_journal_head(bh);
1936 if (buffer_jbd(bh)) {
1937 jh = bh2jh(bh);
1938 jh->b_jcount++;
1939 }
1940 jbd_unlock_bh_journal_head(bh);
1941 return jh;
1942}
1943
1944static void __journal_remove_journal_head(struct buffer_head *bh)
1945{
1946 struct journal_head *jh = bh2jh(bh);
1947
1948 J_ASSERT_JH(jh, jh->b_jcount >= 0);
bb189247
JK
1949 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1950 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1951 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1952 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1953 J_ASSERT_BH(bh, buffer_jbd(bh));
1954 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1955 BUFFER_TRACE(bh, "remove journal_head");
1956 if (jh->b_frozen_data) {
1957 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1958 jbd_free(jh->b_frozen_data, bh->b_size);
1da177e4 1959 }
bb189247
JK
1960 if (jh->b_committed_data) {
1961 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1962 jbd_free(jh->b_committed_data, bh->b_size);
1963 }
1964 bh->b_private = NULL;
1965 jh->b_bh = NULL; /* debug, really */
1966 clear_buffer_jbd(bh);
1967 journal_free_journal_head(jh);
1da177e4
LT
1968}
1969
1970/*
bb189247 1971 * Drop a reference on the passed journal_head. If it fell to zero then
1da177e4
LT
1972 * release the journal_head from the buffer_head.
1973 */
1974void journal_put_journal_head(struct journal_head *jh)
1975{
1976 struct buffer_head *bh = jh2bh(jh);
1977
1978 jbd_lock_bh_journal_head(bh);
1979 J_ASSERT_JH(jh, jh->b_jcount > 0);
1980 --jh->b_jcount;
bb189247 1981 if (!jh->b_jcount) {
1da177e4 1982 __journal_remove_journal_head(bh);
bb189247 1983 jbd_unlock_bh_journal_head(bh);
1da177e4 1984 __brelse(bh);
bb189247
JK
1985 } else
1986 jbd_unlock_bh_journal_head(bh);
1da177e4
LT
1987}
1988
1989/*
c2a9159c 1990 * debugfs tunables
1da177e4 1991 */
c2a9159c 1992#ifdef CONFIG_JBD_DEBUG
1da177e4 1993
c2a9159c
JS
1994u8 journal_enable_debug __read_mostly;
1995EXPORT_SYMBOL(journal_enable_debug);
1da177e4 1996
c2a9159c
JS
1997static struct dentry *jbd_debugfs_dir;
1998static struct dentry *jbd_debug;
1da177e4 1999
c2a9159c 2000static void __init jbd_create_debugfs_entry(void)
1da177e4 2001{
c2a9159c
JS
2002 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
2003 if (jbd_debugfs_dir)
765f8361 2004 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
c2a9159c
JS
2005 jbd_debugfs_dir,
2006 &journal_enable_debug);
1da177e4
LT
2007}
2008
c2a9159c 2009static void __exit jbd_remove_debugfs_entry(void)
1da177e4 2010{
c2a9159c
JS
2011 debugfs_remove(jbd_debug);
2012 debugfs_remove(jbd_debugfs_dir);
1da177e4
LT
2013}
2014
c2a9159c 2015#else
1da177e4 2016
c2a9159c 2017static inline void jbd_create_debugfs_entry(void)
1da177e4 2018{
1da177e4
LT
2019}
2020
c2a9159c 2021static inline void jbd_remove_debugfs_entry(void)
1da177e4 2022{
1da177e4
LT
2023}
2024
1da177e4
LT
2025#endif
2026
e18b890b 2027struct kmem_cache *jbd_handle_cache;
1da177e4
LT
2028
2029static int __init journal_init_handle_cache(void)
2030{
2031 jbd_handle_cache = kmem_cache_create("journal_handle",
2032 sizeof(handle_t),
2033 0, /* offset */
e12ba74d 2034 SLAB_TEMPORARY, /* flags */
20c2df83 2035 NULL); /* ctor */
1da177e4
LT
2036 if (jbd_handle_cache == NULL) {
2037 printk(KERN_EMERG "JBD: failed to create handle cache\n");
2038 return -ENOMEM;
2039 }
2040 return 0;
2041}
2042
2043static void journal_destroy_handle_cache(void)
2044{
2045 if (jbd_handle_cache)
2046 kmem_cache_destroy(jbd_handle_cache);
2047}
2048
2049/*
2050 * Module startup and shutdown
2051 */
2052
2053static int __init journal_init_caches(void)
2054{
2055 int ret;
2056
2057 ret = journal_init_revoke_caches();
2058 if (ret == 0)
2059 ret = journal_init_journal_head_cache();
2060 if (ret == 0)
2061 ret = journal_init_handle_cache();
2062 return ret;
2063}
2064
2065static void journal_destroy_caches(void)
2066{
2067 journal_destroy_revoke_caches();
2068 journal_destroy_journal_head_cache();
2069 journal_destroy_handle_cache();
2070}
2071
2072static int __init journal_init(void)
2073{
2074 int ret;
2075
2aed3484 2076 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
022a4a7b 2077
1da177e4
LT
2078 ret = journal_init_caches();
2079 if (ret != 0)
2080 journal_destroy_caches();
c2a9159c 2081 jbd_create_debugfs_entry();
1da177e4
LT
2082 return ret;
2083}
2084
2085static void __exit journal_exit(void)
2086{
2087#ifdef CONFIG_JBD_DEBUG
2088 int n = atomic_read(&nr_journal_heads);
2089 if (n)
2090 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2091#endif
c2a9159c 2092 jbd_remove_debugfs_entry();
1da177e4
LT
2093 journal_destroy_caches();
2094}
2095
2096MODULE_LICENSE("GPL");
2097module_init(journal_init);
2098module_exit(journal_exit);
2099