jbd/jbd2: validate sb->s_first in journal_get_superblock()
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd / journal.c
blob70713d508a0f88ebe1d3ac586ea188c3be4dc200
1 /*
2 * linux/fs/jbd/journal.c
4 * Written by Stephen C. Tweedie <sct@redhat.com>, 1998
6 * Copyright 1998 Red Hat corp --- All Rights Reserved
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.
12 * Generic filesystem journal-writing code; part of the ext2fs
13 * journaling system.
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.
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).
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>
31 #include <linux/init.h>
32 #include <linux/mm.h>
33 #include <linux/freezer.h>
34 #include <linux/pagemap.h>
35 #include <linux/kthread.h>
36 #include <linux/poison.h>
37 #include <linux/proc_fs.h>
38 #include <linux/debugfs.h>
40 #include <asm/uaccess.h>
41 #include <asm/page.h>
43 EXPORT_SYMBOL(journal_start);
44 EXPORT_SYMBOL(journal_restart);
45 EXPORT_SYMBOL(journal_extend);
46 EXPORT_SYMBOL(journal_stop);
47 EXPORT_SYMBOL(journal_lock_updates);
48 EXPORT_SYMBOL(journal_unlock_updates);
49 EXPORT_SYMBOL(journal_get_write_access);
50 EXPORT_SYMBOL(journal_get_create_access);
51 EXPORT_SYMBOL(journal_get_undo_access);
52 EXPORT_SYMBOL(journal_dirty_data);
53 EXPORT_SYMBOL(journal_dirty_metadata);
54 EXPORT_SYMBOL(journal_release_buffer);
55 EXPORT_SYMBOL(journal_forget);
56 #if 0
57 EXPORT_SYMBOL(journal_sync_buffer);
58 #endif
59 EXPORT_SYMBOL(journal_flush);
60 EXPORT_SYMBOL(journal_revoke);
62 EXPORT_SYMBOL(journal_init_dev);
63 EXPORT_SYMBOL(journal_init_inode);
64 EXPORT_SYMBOL(journal_update_format);
65 EXPORT_SYMBOL(journal_check_used_features);
66 EXPORT_SYMBOL(journal_check_available_features);
67 EXPORT_SYMBOL(journal_set_features);
68 EXPORT_SYMBOL(journal_create);
69 EXPORT_SYMBOL(journal_load);
70 EXPORT_SYMBOL(journal_destroy);
71 EXPORT_SYMBOL(journal_abort);
72 EXPORT_SYMBOL(journal_errno);
73 EXPORT_SYMBOL(journal_ack_err);
74 EXPORT_SYMBOL(journal_clear_err);
75 EXPORT_SYMBOL(log_wait_commit);
76 EXPORT_SYMBOL(log_start_commit);
77 EXPORT_SYMBOL(journal_start_commit);
78 EXPORT_SYMBOL(journal_force_commit_nested);
79 EXPORT_SYMBOL(journal_wipe);
80 EXPORT_SYMBOL(journal_blocks_per_page);
81 EXPORT_SYMBOL(journal_invalidatepage);
82 EXPORT_SYMBOL(journal_try_to_free_buffers);
83 EXPORT_SYMBOL(journal_force_commit);
85 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
86 static void __journal_abort_soft (journal_t *journal, int errno);
89 * Helper function used to manage commit timeouts
92 static void commit_timeout(unsigned long __data)
94 struct task_struct * p = (struct task_struct *) __data;
96 wake_up_process(p);
100 * kjournald: The main thread function used to manage a logging device
101 * journal.
103 * This kernel thread is responsible for two things:
105 * 1) COMMIT: Every so often we need to commit the current state of the
106 * filesystem to disk. The journal thread is responsible for writing
107 * all of the metadata buffers to disk.
109 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
110 * of the data in that part of the log has been rewritten elsewhere on
111 * the disk. Flushing these old buffers to reclaim space in the log is
112 * known as checkpointing, and this thread is responsible for that job.
115 static int kjournald(void *arg)
117 journal_t *journal = arg;
118 transaction_t *transaction;
121 * Set up an interval timer which can be used to trigger a commit wakeup
122 * after the commit interval expires
124 setup_timer(&journal->j_commit_timer, commit_timeout,
125 (unsigned long)current);
127 /* Record that the journal thread is running */
128 journal->j_task = current;
129 wake_up(&journal->j_wait_done_commit);
131 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
132 journal->j_commit_interval / HZ);
135 * And now, wait forever for commit wakeup events.
137 spin_lock(&journal->j_state_lock);
139 loop:
140 if (journal->j_flags & JFS_UNMOUNT)
141 goto end_loop;
143 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
144 journal->j_commit_sequence, journal->j_commit_request);
146 if (journal->j_commit_sequence != journal->j_commit_request) {
147 jbd_debug(1, "OK, requests differ\n");
148 spin_unlock(&journal->j_state_lock);
149 del_timer_sync(&journal->j_commit_timer);
150 journal_commit_transaction(journal);
151 spin_lock(&journal->j_state_lock);
152 goto loop;
155 wake_up(&journal->j_wait_done_commit);
156 if (freezing(current)) {
158 * The simpler the better. Flushing journal isn't a
159 * good idea, because that depends on threads that may
160 * be already stopped.
162 jbd_debug(1, "Now suspending kjournald\n");
163 spin_unlock(&journal->j_state_lock);
164 refrigerator();
165 spin_lock(&journal->j_state_lock);
166 } else {
168 * We assume on resume that commits are already there,
169 * so we don't sleep
171 DEFINE_WAIT(wait);
172 int should_sleep = 1;
174 prepare_to_wait(&journal->j_wait_commit, &wait,
175 TASK_INTERRUPTIBLE);
176 if (journal->j_commit_sequence != journal->j_commit_request)
177 should_sleep = 0;
178 transaction = journal->j_running_transaction;
179 if (transaction && time_after_eq(jiffies,
180 transaction->t_expires))
181 should_sleep = 0;
182 if (journal->j_flags & JFS_UNMOUNT)
183 should_sleep = 0;
184 if (should_sleep) {
185 spin_unlock(&journal->j_state_lock);
186 schedule();
187 spin_lock(&journal->j_state_lock);
189 finish_wait(&journal->j_wait_commit, &wait);
192 jbd_debug(1, "kjournald wakes\n");
195 * Were we woken up by a commit wakeup event?
197 transaction = journal->j_running_transaction;
198 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
199 journal->j_commit_request = transaction->t_tid;
200 jbd_debug(1, "woke because of timeout\n");
202 goto loop;
204 end_loop:
205 spin_unlock(&journal->j_state_lock);
206 del_timer_sync(&journal->j_commit_timer);
207 journal->j_task = NULL;
208 wake_up(&journal->j_wait_done_commit);
209 jbd_debug(1, "Journal thread exiting.\n");
210 return 0;
213 static int journal_start_thread(journal_t *journal)
215 struct task_struct *t;
217 t = kthread_run(kjournald, journal, "kjournald");
218 if (IS_ERR(t))
219 return PTR_ERR(t);
221 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
222 return 0;
225 static void journal_kill_thread(journal_t *journal)
227 spin_lock(&journal->j_state_lock);
228 journal->j_flags |= JFS_UNMOUNT;
230 while (journal->j_task) {
231 wake_up(&journal->j_wait_commit);
232 spin_unlock(&journal->j_state_lock);
233 wait_event(journal->j_wait_done_commit,
234 journal->j_task == NULL);
235 spin_lock(&journal->j_state_lock);
237 spin_unlock(&journal->j_state_lock);
241 * journal_write_metadata_buffer: write a metadata buffer to the journal.
243 * Writes a metadata buffer to a given disk block. The actual IO is not
244 * performed but a new buffer_head is constructed which labels the data
245 * to be written with the correct destination disk block.
247 * Any magic-number escaping which needs to be done will cause a
248 * copy-out here. If the buffer happens to start with the
249 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
250 * magic number is only written to the log for descripter blocks. In
251 * this case, we copy the data and replace the first word with 0, and we
252 * return a result code which indicates that this buffer needs to be
253 * marked as an escaped buffer in the corresponding log descriptor
254 * block. The missing word can then be restored when the block is read
255 * during recovery.
257 * If the source buffer has already been modified by a new transaction
258 * since we took the last commit snapshot, we use the frozen copy of
259 * that data for IO. If we end up using the existing buffer_head's data
260 * for the write, then we *have* to lock the buffer to prevent anyone
261 * else from using and possibly modifying it while the IO is in
262 * progress.
264 * The function returns a pointer to the buffer_heads to be used for IO.
266 * We assume that the journal has already been locked in this function.
268 * Return value:
269 * <0: Error
270 * >=0: Finished OK
272 * On success:
273 * Bit 0 set == escape performed on the data
274 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
277 int journal_write_metadata_buffer(transaction_t *transaction,
278 struct journal_head *jh_in,
279 struct journal_head **jh_out,
280 unsigned int blocknr)
282 int need_copy_out = 0;
283 int done_copy_out = 0;
284 int do_escape = 0;
285 char *mapped_data;
286 struct buffer_head *new_bh;
287 struct journal_head *new_jh;
288 struct page *new_page;
289 unsigned int new_offset;
290 struct buffer_head *bh_in = jh2bh(jh_in);
291 journal_t *journal = transaction->t_journal;
294 * The buffer really shouldn't be locked: only the current committing
295 * transaction is allowed to write it, so nobody else is allowed
296 * to do any IO.
298 * akpm: except if we're journalling data, and write() output is
299 * also part of a shared mapping, and another thread has
300 * decided to launch a writepage() against this buffer.
302 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
304 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
305 /* keep subsequent assertions sane */
306 new_bh->b_state = 0;
307 init_buffer(new_bh, NULL, NULL);
308 atomic_set(&new_bh->b_count, 1);
309 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
312 * If a new transaction has already done a buffer copy-out, then
313 * we use that version of the data for the commit.
315 jbd_lock_bh_state(bh_in);
316 repeat:
317 if (jh_in->b_frozen_data) {
318 done_copy_out = 1;
319 new_page = virt_to_page(jh_in->b_frozen_data);
320 new_offset = offset_in_page(jh_in->b_frozen_data);
321 } else {
322 new_page = jh2bh(jh_in)->b_page;
323 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
326 mapped_data = kmap_atomic(new_page, KM_USER0);
328 * Check for escaping
330 if (*((__be32 *)(mapped_data + new_offset)) ==
331 cpu_to_be32(JFS_MAGIC_NUMBER)) {
332 need_copy_out = 1;
333 do_escape = 1;
335 kunmap_atomic(mapped_data, KM_USER0);
338 * Do we need to do a data copy?
340 if (need_copy_out && !done_copy_out) {
341 char *tmp;
343 jbd_unlock_bh_state(bh_in);
344 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
345 jbd_lock_bh_state(bh_in);
346 if (jh_in->b_frozen_data) {
347 jbd_free(tmp, bh_in->b_size);
348 goto repeat;
351 jh_in->b_frozen_data = tmp;
352 mapped_data = kmap_atomic(new_page, KM_USER0);
353 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
354 kunmap_atomic(mapped_data, KM_USER0);
356 new_page = virt_to_page(tmp);
357 new_offset = offset_in_page(tmp);
358 done_copy_out = 1;
362 * Did we need to do an escaping? Now we've done all the
363 * copying, we can finally do so.
365 if (do_escape) {
366 mapped_data = kmap_atomic(new_page, KM_USER0);
367 *((unsigned int *)(mapped_data + new_offset)) = 0;
368 kunmap_atomic(mapped_data, KM_USER0);
371 set_bh_page(new_bh, new_page, new_offset);
372 new_jh->b_transaction = NULL;
373 new_bh->b_size = jh2bh(jh_in)->b_size;
374 new_bh->b_bdev = transaction->t_journal->j_dev;
375 new_bh->b_blocknr = blocknr;
376 set_buffer_mapped(new_bh);
377 set_buffer_dirty(new_bh);
379 *jh_out = new_jh;
382 * The to-be-written buffer needs to get moved to the io queue,
383 * and the original buffer whose contents we are shadowing or
384 * copying is moved to the transaction's shadow queue.
386 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
387 spin_lock(&journal->j_list_lock);
388 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
389 spin_unlock(&journal->j_list_lock);
390 jbd_unlock_bh_state(bh_in);
392 JBUFFER_TRACE(new_jh, "file as BJ_IO");
393 journal_file_buffer(new_jh, transaction, BJ_IO);
395 return do_escape | (done_copy_out << 1);
399 * Allocation code for the journal file. Manage the space left in the
400 * journal, so that we can begin checkpointing when appropriate.
404 * __log_space_left: Return the number of free blocks left in the journal.
406 * Called with the journal already locked.
408 * Called under j_state_lock
411 int __log_space_left(journal_t *journal)
413 int left = journal->j_free;
415 assert_spin_locked(&journal->j_state_lock);
418 * Be pessimistic here about the number of those free blocks which
419 * might be required for log descriptor control blocks.
422 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
424 left -= MIN_LOG_RESERVED_BLOCKS;
426 if (left <= 0)
427 return 0;
428 left -= (left >> 3);
429 return left;
433 * Called under j_state_lock. Returns true if a transaction commit was started.
435 int __log_start_commit(journal_t *journal, tid_t target)
438 * The only transaction we can possibly wait upon is the
439 * currently running transaction (if it exists). Otherwise,
440 * the target tid must be an old one.
442 if (journal->j_running_transaction &&
443 journal->j_running_transaction->t_tid == target) {
445 * We want a new commit: OK, mark the request and wakup the
446 * commit thread. We do _not_ do the commit ourselves.
449 journal->j_commit_request = target;
450 jbd_debug(1, "JBD: requesting commit %d/%d\n",
451 journal->j_commit_request,
452 journal->j_commit_sequence);
453 wake_up(&journal->j_wait_commit);
454 return 1;
455 } else if (!tid_geq(journal->j_commit_request, target))
456 /* This should never happen, but if it does, preserve
457 the evidence before kjournald goes into a loop and
458 increments j_commit_sequence beyond all recognition. */
459 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
460 journal->j_commit_request, journal->j_commit_sequence,
461 target, journal->j_running_transaction ?
462 journal->j_running_transaction->t_tid : 0);
463 return 0;
466 int log_start_commit(journal_t *journal, tid_t tid)
468 int ret;
470 spin_lock(&journal->j_state_lock);
471 ret = __log_start_commit(journal, tid);
472 spin_unlock(&journal->j_state_lock);
473 return ret;
477 * Force and wait upon a commit if the calling process is not within
478 * transaction. This is used for forcing out undo-protected data which contains
479 * bitmaps, when the fs is running out of space.
481 * We can only force the running transaction if we don't have an active handle;
482 * otherwise, we will deadlock.
484 * Returns true if a transaction was started.
486 int journal_force_commit_nested(journal_t *journal)
488 transaction_t *transaction = NULL;
489 tid_t tid;
491 spin_lock(&journal->j_state_lock);
492 if (journal->j_running_transaction && !current->journal_info) {
493 transaction = journal->j_running_transaction;
494 __log_start_commit(journal, transaction->t_tid);
495 } else if (journal->j_committing_transaction)
496 transaction = journal->j_committing_transaction;
498 if (!transaction) {
499 spin_unlock(&journal->j_state_lock);
500 return 0; /* Nothing to retry */
503 tid = transaction->t_tid;
504 spin_unlock(&journal->j_state_lock);
505 log_wait_commit(journal, tid);
506 return 1;
510 * Start a commit of the current running transaction (if any). Returns true
511 * if a transaction is going to be committed (or is currently already
512 * committing), and fills its tid in at *ptid
514 int journal_start_commit(journal_t *journal, tid_t *ptid)
516 int ret = 0;
518 spin_lock(&journal->j_state_lock);
519 if (journal->j_running_transaction) {
520 tid_t tid = journal->j_running_transaction->t_tid;
522 __log_start_commit(journal, tid);
523 /* There's a running transaction and we've just made sure
524 * it's commit has been scheduled. */
525 if (ptid)
526 *ptid = tid;
527 ret = 1;
528 } else if (journal->j_committing_transaction) {
530 * If ext3_write_super() recently started a commit, then we
531 * have to wait for completion of that transaction
533 if (ptid)
534 *ptid = journal->j_committing_transaction->t_tid;
535 ret = 1;
537 spin_unlock(&journal->j_state_lock);
538 return ret;
542 * Wait for a specified commit to complete.
543 * The caller may not hold the journal lock.
545 int log_wait_commit(journal_t *journal, tid_t tid)
547 int err = 0;
549 #ifdef CONFIG_JBD_DEBUG
550 spin_lock(&journal->j_state_lock);
551 if (!tid_geq(journal->j_commit_request, tid)) {
552 printk(KERN_EMERG
553 "%s: error: j_commit_request=%d, tid=%d\n",
554 __func__, journal->j_commit_request, tid);
556 spin_unlock(&journal->j_state_lock);
557 #endif
558 spin_lock(&journal->j_state_lock);
559 while (tid_gt(tid, journal->j_commit_sequence)) {
560 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
561 tid, journal->j_commit_sequence);
562 wake_up(&journal->j_wait_commit);
563 spin_unlock(&journal->j_state_lock);
564 wait_event(journal->j_wait_done_commit,
565 !tid_gt(tid, journal->j_commit_sequence));
566 spin_lock(&journal->j_state_lock);
568 spin_unlock(&journal->j_state_lock);
570 if (unlikely(is_journal_aborted(journal))) {
571 printk(KERN_EMERG "journal commit I/O error\n");
572 err = -EIO;
574 return err;
578 * Log buffer allocation routines:
581 int journal_next_log_block(journal_t *journal, unsigned int *retp)
583 unsigned int blocknr;
585 spin_lock(&journal->j_state_lock);
586 J_ASSERT(journal->j_free > 1);
588 blocknr = journal->j_head;
589 journal->j_head++;
590 journal->j_free--;
591 if (journal->j_head == journal->j_last)
592 journal->j_head = journal->j_first;
593 spin_unlock(&journal->j_state_lock);
594 return journal_bmap(journal, blocknr, retp);
598 * Conversion of logical to physical block numbers for the journal
600 * On external journals the journal blocks are identity-mapped, so
601 * this is a no-op. If needed, we can use j_blk_offset - everything is
602 * ready.
604 int journal_bmap(journal_t *journal, unsigned int blocknr,
605 unsigned int *retp)
607 int err = 0;
608 unsigned int ret;
610 if (journal->j_inode) {
611 ret = bmap(journal->j_inode, blocknr);
612 if (ret)
613 *retp = ret;
614 else {
615 char b[BDEVNAME_SIZE];
617 printk(KERN_ALERT "%s: journal block not found "
618 "at offset %u on %s\n",
619 __func__,
620 blocknr,
621 bdevname(journal->j_dev, b));
622 err = -EIO;
623 __journal_abort_soft(journal, err);
625 } else {
626 *retp = blocknr; /* +journal->j_blk_offset */
628 return err;
632 * We play buffer_head aliasing tricks to write data/metadata blocks to
633 * the journal without copying their contents, but for journal
634 * descriptor blocks we do need to generate bona fide buffers.
636 * After the caller of journal_get_descriptor_buffer() has finished modifying
637 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
638 * But we don't bother doing that, so there will be coherency problems with
639 * mmaps of blockdevs which hold live JBD-controlled filesystems.
641 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
643 struct buffer_head *bh;
644 unsigned int blocknr;
645 int err;
647 err = journal_next_log_block(journal, &blocknr);
649 if (err)
650 return NULL;
652 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
653 if (!bh)
654 return NULL;
655 lock_buffer(bh);
656 memset(bh->b_data, 0, journal->j_blocksize);
657 set_buffer_uptodate(bh);
658 unlock_buffer(bh);
659 BUFFER_TRACE(bh, "return this buffer");
660 return journal_add_journal_head(bh);
664 * Management for journal control blocks: functions to create and
665 * destroy journal_t structures, and to initialise and read existing
666 * journal blocks from disk. */
668 /* First: create and setup a journal_t object in memory. We initialise
669 * very few fields yet: that has to wait until we have created the
670 * journal structures from from scratch, or loaded them from disk. */
672 static journal_t * journal_init_common (void)
674 journal_t *journal;
675 int err;
677 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
678 if (!journal)
679 goto fail;
681 init_waitqueue_head(&journal->j_wait_transaction_locked);
682 init_waitqueue_head(&journal->j_wait_logspace);
683 init_waitqueue_head(&journal->j_wait_done_commit);
684 init_waitqueue_head(&journal->j_wait_checkpoint);
685 init_waitqueue_head(&journal->j_wait_commit);
686 init_waitqueue_head(&journal->j_wait_updates);
687 mutex_init(&journal->j_barrier);
688 mutex_init(&journal->j_checkpoint_mutex);
689 spin_lock_init(&journal->j_revoke_lock);
690 spin_lock_init(&journal->j_list_lock);
691 spin_lock_init(&journal->j_state_lock);
693 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
695 /* The journal is marked for error until we succeed with recovery! */
696 journal->j_flags = JFS_ABORT;
698 /* Set up a default-sized revoke table for the new mount. */
699 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
700 if (err) {
701 kfree(journal);
702 goto fail;
704 return journal;
705 fail:
706 return NULL;
709 /* journal_init_dev and journal_init_inode:
711 * Create a journal structure assigned some fixed set of disk blocks to
712 * the journal. We don't actually touch those disk blocks yet, but we
713 * need to set up all of the mapping information to tell the journaling
714 * system where the journal blocks are.
719 * journal_t * journal_init_dev() - creates and initialises a journal structure
720 * @bdev: Block device on which to create the journal
721 * @fs_dev: Device which hold journalled filesystem for this journal.
722 * @start: Block nr Start of journal.
723 * @len: Length of the journal in blocks.
724 * @blocksize: blocksize of journalling device
726 * Returns: a newly created journal_t *
728 * journal_init_dev creates a journal which maps a fixed contiguous
729 * range of blocks on an arbitrary block device.
732 journal_t * journal_init_dev(struct block_device *bdev,
733 struct block_device *fs_dev,
734 int start, int len, int blocksize)
736 journal_t *journal = journal_init_common();
737 struct buffer_head *bh;
738 int n;
740 if (!journal)
741 return NULL;
743 /* journal descriptor can store up to n blocks -bzzz */
744 journal->j_blocksize = blocksize;
745 n = journal->j_blocksize / sizeof(journal_block_tag_t);
746 journal->j_wbufsize = n;
747 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
748 if (!journal->j_wbuf) {
749 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
750 __func__);
751 goto out_err;
753 journal->j_dev = bdev;
754 journal->j_fs_dev = fs_dev;
755 journal->j_blk_offset = start;
756 journal->j_maxlen = len;
758 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
759 if (!bh) {
760 printk(KERN_ERR
761 "%s: Cannot get buffer for journal superblock\n",
762 __func__);
763 goto out_err;
765 journal->j_sb_buffer = bh;
766 journal->j_superblock = (journal_superblock_t *)bh->b_data;
768 return journal;
769 out_err:
770 kfree(journal->j_wbuf);
771 kfree(journal);
772 return NULL;
776 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
777 * @inode: An inode to create the journal in
779 * journal_init_inode creates a journal which maps an on-disk inode as
780 * the journal. The inode must exist already, must support bmap() and
781 * must have all data blocks preallocated.
783 journal_t * journal_init_inode (struct inode *inode)
785 struct buffer_head *bh;
786 journal_t *journal = journal_init_common();
787 int err;
788 int n;
789 unsigned int blocknr;
791 if (!journal)
792 return NULL;
794 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
795 journal->j_inode = inode;
796 jbd_debug(1,
797 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
798 journal, inode->i_sb->s_id, inode->i_ino,
799 (long long) inode->i_size,
800 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
802 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
803 journal->j_blocksize = inode->i_sb->s_blocksize;
805 /* journal descriptor can store up to n blocks -bzzz */
806 n = journal->j_blocksize / sizeof(journal_block_tag_t);
807 journal->j_wbufsize = n;
808 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
809 if (!journal->j_wbuf) {
810 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
811 __func__);
812 goto out_err;
815 err = journal_bmap(journal, 0, &blocknr);
816 /* If that failed, give up */
817 if (err) {
818 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
819 __func__);
820 goto out_err;
823 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
824 if (!bh) {
825 printk(KERN_ERR
826 "%s: Cannot get buffer for journal superblock\n",
827 __func__);
828 goto out_err;
830 journal->j_sb_buffer = bh;
831 journal->j_superblock = (journal_superblock_t *)bh->b_data;
833 return journal;
834 out_err:
835 kfree(journal->j_wbuf);
836 kfree(journal);
837 return NULL;
841 * If the journal init or create aborts, we need to mark the journal
842 * superblock as being NULL to prevent the journal destroy from writing
843 * back a bogus superblock.
845 static void journal_fail_superblock (journal_t *journal)
847 struct buffer_head *bh = journal->j_sb_buffer;
848 brelse(bh);
849 journal->j_sb_buffer = NULL;
853 * Given a journal_t structure, initialise the various fields for
854 * startup of a new journaling session. We use this both when creating
855 * a journal, and after recovering an old journal to reset it for
856 * subsequent use.
859 static int journal_reset(journal_t *journal)
861 journal_superblock_t *sb = journal->j_superblock;
862 unsigned int first, last;
864 first = be32_to_cpu(sb->s_first);
865 last = be32_to_cpu(sb->s_maxlen);
866 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
867 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
868 first, last);
869 journal_fail_superblock(journal);
870 return -EINVAL;
873 journal->j_first = first;
874 journal->j_last = last;
876 journal->j_head = first;
877 journal->j_tail = first;
878 journal->j_free = last - first;
880 journal->j_tail_sequence = journal->j_transaction_sequence;
881 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
882 journal->j_commit_request = journal->j_commit_sequence;
884 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
886 /* Add the dynamic fields and write it to disk. */
887 journal_update_superblock(journal, 1);
888 return journal_start_thread(journal);
892 * int journal_create() - Initialise the new journal file
893 * @journal: Journal to create. This structure must have been initialised
895 * Given a journal_t structure which tells us which disk blocks we can
896 * use, create a new journal superblock and initialise all of the
897 * journal fields from scratch.
899 int journal_create(journal_t *journal)
901 unsigned int blocknr;
902 struct buffer_head *bh;
903 journal_superblock_t *sb;
904 int i, err;
906 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
907 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
908 journal->j_maxlen);
909 journal_fail_superblock(journal);
910 return -EINVAL;
913 if (journal->j_inode == NULL) {
915 * We don't know what block to start at!
917 printk(KERN_EMERG
918 "%s: creation of journal on external device!\n",
919 __func__);
920 BUG();
923 /* Zero out the entire journal on disk. We cannot afford to
924 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
925 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
926 for (i = 0; i < journal->j_maxlen; i++) {
927 err = journal_bmap(journal, i, &blocknr);
928 if (err)
929 return err;
930 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
931 lock_buffer(bh);
932 memset (bh->b_data, 0, journal->j_blocksize);
933 BUFFER_TRACE(bh, "marking dirty");
934 mark_buffer_dirty(bh);
935 BUFFER_TRACE(bh, "marking uptodate");
936 set_buffer_uptodate(bh);
937 unlock_buffer(bh);
938 __brelse(bh);
941 sync_blockdev(journal->j_dev);
942 jbd_debug(1, "JBD: journal cleared.\n");
944 /* OK, fill in the initial static fields in the new superblock */
945 sb = journal->j_superblock;
947 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
948 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
950 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
951 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
952 sb->s_first = cpu_to_be32(1);
954 journal->j_transaction_sequence = 1;
956 journal->j_flags &= ~JFS_ABORT;
957 journal->j_format_version = 2;
959 return journal_reset(journal);
963 * void journal_update_superblock() - Update journal sb on disk.
964 * @journal: The journal to update.
965 * @wait: Set to '0' if you don't want to wait for IO completion.
967 * Update a journal's dynamic superblock fields and write it to disk,
968 * optionally waiting for the IO to complete.
970 void journal_update_superblock(journal_t *journal, int wait)
972 journal_superblock_t *sb = journal->j_superblock;
973 struct buffer_head *bh = journal->j_sb_buffer;
976 * As a special case, if the on-disk copy is already marked as needing
977 * no recovery (s_start == 0) and there are no outstanding transactions
978 * in the filesystem, then we can safely defer the superblock update
979 * until the next commit by setting JFS_FLUSHED. This avoids
980 * attempting a write to a potential-readonly device.
982 if (sb->s_start == 0 && journal->j_tail_sequence ==
983 journal->j_transaction_sequence) {
984 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
985 "(start %u, seq %d, errno %d)\n",
986 journal->j_tail, journal->j_tail_sequence,
987 journal->j_errno);
988 goto out;
991 spin_lock(&journal->j_state_lock);
992 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
993 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
995 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
996 sb->s_start = cpu_to_be32(journal->j_tail);
997 sb->s_errno = cpu_to_be32(journal->j_errno);
998 spin_unlock(&journal->j_state_lock);
1000 BUFFER_TRACE(bh, "marking dirty");
1001 mark_buffer_dirty(bh);
1002 if (wait)
1003 sync_dirty_buffer(bh);
1004 else
1005 ll_rw_block(SWRITE, 1, &bh);
1007 out:
1008 /* If we have just flushed the log (by marking s_start==0), then
1009 * any future commit will have to be careful to update the
1010 * superblock again to re-record the true start of the log. */
1012 spin_lock(&journal->j_state_lock);
1013 if (sb->s_start)
1014 journal->j_flags &= ~JFS_FLUSHED;
1015 else
1016 journal->j_flags |= JFS_FLUSHED;
1017 spin_unlock(&journal->j_state_lock);
1021 * Read the superblock for a given journal, performing initial
1022 * validation of the format.
1025 static int journal_get_superblock(journal_t *journal)
1027 struct buffer_head *bh;
1028 journal_superblock_t *sb;
1029 int err = -EIO;
1031 bh = journal->j_sb_buffer;
1033 J_ASSERT(bh != NULL);
1034 if (!buffer_uptodate(bh)) {
1035 ll_rw_block(READ, 1, &bh);
1036 wait_on_buffer(bh);
1037 if (!buffer_uptodate(bh)) {
1038 printk (KERN_ERR
1039 "JBD: IO error reading journal superblock\n");
1040 goto out;
1044 sb = journal->j_superblock;
1046 err = -EINVAL;
1048 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1049 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1050 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1051 goto out;
1054 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1055 case JFS_SUPERBLOCK_V1:
1056 journal->j_format_version = 1;
1057 break;
1058 case JFS_SUPERBLOCK_V2:
1059 journal->j_format_version = 2;
1060 break;
1061 default:
1062 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1063 goto out;
1066 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1067 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1068 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1069 printk (KERN_WARNING "JBD: journal file too short\n");
1070 goto out;
1073 if (be32_to_cpu(sb->s_first) == 0 ||
1074 be32_to_cpu(sb->s_first) >= journal->j_maxlen) {
1075 printk(KERN_WARNING
1076 "JBD: Invalid start block of journal: %u\n",
1077 be32_to_cpu(sb->s_first));
1078 goto out;
1081 return 0;
1083 out:
1084 journal_fail_superblock(journal);
1085 return err;
1089 * Load the on-disk journal superblock and read the key fields into the
1090 * journal_t.
1093 static int load_superblock(journal_t *journal)
1095 int err;
1096 journal_superblock_t *sb;
1098 err = journal_get_superblock(journal);
1099 if (err)
1100 return err;
1102 sb = journal->j_superblock;
1104 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1105 journal->j_tail = be32_to_cpu(sb->s_start);
1106 journal->j_first = be32_to_cpu(sb->s_first);
1107 journal->j_last = be32_to_cpu(sb->s_maxlen);
1108 journal->j_errno = be32_to_cpu(sb->s_errno);
1110 return 0;
1115 * int journal_load() - Read journal from disk.
1116 * @journal: Journal to act on.
1118 * Given a journal_t structure which tells us which disk blocks contain
1119 * a journal, read the journal from disk to initialise the in-memory
1120 * structures.
1122 int journal_load(journal_t *journal)
1124 int err;
1125 journal_superblock_t *sb;
1127 err = load_superblock(journal);
1128 if (err)
1129 return err;
1131 sb = journal->j_superblock;
1132 /* If this is a V2 superblock, then we have to check the
1133 * features flags on it. */
1135 if (journal->j_format_version >= 2) {
1136 if ((sb->s_feature_ro_compat &
1137 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1138 (sb->s_feature_incompat &
1139 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1140 printk (KERN_WARNING
1141 "JBD: Unrecognised features on journal\n");
1142 return -EINVAL;
1146 /* Let the recovery code check whether it needs to recover any
1147 * data from the journal. */
1148 if (journal_recover(journal))
1149 goto recovery_error;
1151 /* OK, we've finished with the dynamic journal bits:
1152 * reinitialise the dynamic contents of the superblock in memory
1153 * and reset them on disk. */
1154 if (journal_reset(journal))
1155 goto recovery_error;
1157 journal->j_flags &= ~JFS_ABORT;
1158 journal->j_flags |= JFS_LOADED;
1159 return 0;
1161 recovery_error:
1162 printk (KERN_WARNING "JBD: recovery failed\n");
1163 return -EIO;
1167 * void journal_destroy() - Release a journal_t structure.
1168 * @journal: Journal to act on.
1170 * Release a journal_t structure once it is no longer in use by the
1171 * journaled object.
1172 * Return <0 if we couldn't clean up the journal.
1174 int journal_destroy(journal_t *journal)
1176 int err = 0;
1178 /* Wait for the commit thread to wake up and die. */
1179 journal_kill_thread(journal);
1181 /* Force a final log commit */
1182 if (journal->j_running_transaction)
1183 journal_commit_transaction(journal);
1185 /* Force any old transactions to disk */
1187 /* Totally anal locking here... */
1188 spin_lock(&journal->j_list_lock);
1189 while (journal->j_checkpoint_transactions != NULL) {
1190 spin_unlock(&journal->j_list_lock);
1191 log_do_checkpoint(journal);
1192 spin_lock(&journal->j_list_lock);
1195 J_ASSERT(journal->j_running_transaction == NULL);
1196 J_ASSERT(journal->j_committing_transaction == NULL);
1197 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1198 spin_unlock(&journal->j_list_lock);
1200 if (journal->j_sb_buffer) {
1201 if (!is_journal_aborted(journal)) {
1202 /* We can now mark the journal as empty. */
1203 journal->j_tail = 0;
1204 journal->j_tail_sequence =
1205 ++journal->j_transaction_sequence;
1206 journal_update_superblock(journal, 1);
1207 } else {
1208 err = -EIO;
1210 brelse(journal->j_sb_buffer);
1213 if (journal->j_inode)
1214 iput(journal->j_inode);
1215 if (journal->j_revoke)
1216 journal_destroy_revoke(journal);
1217 kfree(journal->j_wbuf);
1218 kfree(journal);
1220 return err;
1225 *int journal_check_used_features () - Check if features specified are used.
1226 * @journal: Journal to check.
1227 * @compat: bitmask of compatible features
1228 * @ro: bitmask of features that force read-only mount
1229 * @incompat: bitmask of incompatible features
1231 * Check whether the journal uses all of a given set of
1232 * features. Return true (non-zero) if it does.
1235 int journal_check_used_features (journal_t *journal, unsigned long compat,
1236 unsigned long ro, unsigned long incompat)
1238 journal_superblock_t *sb;
1240 if (!compat && !ro && !incompat)
1241 return 1;
1242 if (journal->j_format_version == 1)
1243 return 0;
1245 sb = journal->j_superblock;
1247 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1248 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1249 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1250 return 1;
1252 return 0;
1256 * int journal_check_available_features() - Check feature set in journalling layer
1257 * @journal: Journal to check.
1258 * @compat: bitmask of compatible features
1259 * @ro: bitmask of features that force read-only mount
1260 * @incompat: bitmask of incompatible features
1262 * Check whether the journaling code supports the use of
1263 * all of a given set of features on this journal. Return true
1264 * (non-zero) if it can. */
1266 int journal_check_available_features (journal_t *journal, unsigned long compat,
1267 unsigned long ro, unsigned long incompat)
1269 journal_superblock_t *sb;
1271 if (!compat && !ro && !incompat)
1272 return 1;
1274 sb = journal->j_superblock;
1276 /* We can support any known requested features iff the
1277 * superblock is in version 2. Otherwise we fail to support any
1278 * extended sb features. */
1280 if (journal->j_format_version != 2)
1281 return 0;
1283 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1284 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1285 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1286 return 1;
1288 return 0;
1292 * int journal_set_features () - Mark a given journal feature in the superblock
1293 * @journal: Journal to act on.
1294 * @compat: bitmask of compatible features
1295 * @ro: bitmask of features that force read-only mount
1296 * @incompat: bitmask of incompatible features
1298 * Mark a given journal feature as present on the
1299 * superblock. Returns true if the requested features could be set.
1303 int journal_set_features (journal_t *journal, unsigned long compat,
1304 unsigned long ro, unsigned long incompat)
1306 journal_superblock_t *sb;
1308 if (journal_check_used_features(journal, compat, ro, incompat))
1309 return 1;
1311 if (!journal_check_available_features(journal, compat, ro, incompat))
1312 return 0;
1314 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1315 compat, ro, incompat);
1317 sb = journal->j_superblock;
1319 sb->s_feature_compat |= cpu_to_be32(compat);
1320 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1321 sb->s_feature_incompat |= cpu_to_be32(incompat);
1323 return 1;
1328 * int journal_update_format () - Update on-disk journal structure.
1329 * @journal: Journal to act on.
1331 * Given an initialised but unloaded journal struct, poke about in the
1332 * on-disk structure to update it to the most recent supported version.
1334 int journal_update_format (journal_t *journal)
1336 journal_superblock_t *sb;
1337 int err;
1339 err = journal_get_superblock(journal);
1340 if (err)
1341 return err;
1343 sb = journal->j_superblock;
1345 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1346 case JFS_SUPERBLOCK_V2:
1347 return 0;
1348 case JFS_SUPERBLOCK_V1:
1349 return journal_convert_superblock_v1(journal, sb);
1350 default:
1351 break;
1353 return -EINVAL;
1356 static int journal_convert_superblock_v1(journal_t *journal,
1357 journal_superblock_t *sb)
1359 int offset, blocksize;
1360 struct buffer_head *bh;
1362 printk(KERN_WARNING
1363 "JBD: Converting superblock from version 1 to 2.\n");
1365 /* Pre-initialise new fields to zero */
1366 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1367 blocksize = be32_to_cpu(sb->s_blocksize);
1368 memset(&sb->s_feature_compat, 0, blocksize-offset);
1370 sb->s_nr_users = cpu_to_be32(1);
1371 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1372 journal->j_format_version = 2;
1374 bh = journal->j_sb_buffer;
1375 BUFFER_TRACE(bh, "marking dirty");
1376 mark_buffer_dirty(bh);
1377 sync_dirty_buffer(bh);
1378 return 0;
1383 * int journal_flush () - Flush journal
1384 * @journal: Journal to act on.
1386 * Flush all data for a given journal to disk and empty the journal.
1387 * Filesystems can use this when remounting readonly to ensure that
1388 * recovery does not need to happen on remount.
1391 int journal_flush(journal_t *journal)
1393 int err = 0;
1394 transaction_t *transaction = NULL;
1395 unsigned int old_tail;
1397 spin_lock(&journal->j_state_lock);
1399 /* Force everything buffered to the log... */
1400 if (journal->j_running_transaction) {
1401 transaction = journal->j_running_transaction;
1402 __log_start_commit(journal, transaction->t_tid);
1403 } else if (journal->j_committing_transaction)
1404 transaction = journal->j_committing_transaction;
1406 /* Wait for the log commit to complete... */
1407 if (transaction) {
1408 tid_t tid = transaction->t_tid;
1410 spin_unlock(&journal->j_state_lock);
1411 log_wait_commit(journal, tid);
1412 } else {
1413 spin_unlock(&journal->j_state_lock);
1416 /* ...and flush everything in the log out to disk. */
1417 spin_lock(&journal->j_list_lock);
1418 while (!err && journal->j_checkpoint_transactions != NULL) {
1419 spin_unlock(&journal->j_list_lock);
1420 mutex_lock(&journal->j_checkpoint_mutex);
1421 err = log_do_checkpoint(journal);
1422 mutex_unlock(&journal->j_checkpoint_mutex);
1423 spin_lock(&journal->j_list_lock);
1425 spin_unlock(&journal->j_list_lock);
1427 if (is_journal_aborted(journal))
1428 return -EIO;
1430 cleanup_journal_tail(journal);
1432 /* Finally, mark the journal as really needing no recovery.
1433 * This sets s_start==0 in the underlying superblock, which is
1434 * the magic code for a fully-recovered superblock. Any future
1435 * commits of data to the journal will restore the current
1436 * s_start value. */
1437 spin_lock(&journal->j_state_lock);
1438 old_tail = journal->j_tail;
1439 journal->j_tail = 0;
1440 spin_unlock(&journal->j_state_lock);
1441 journal_update_superblock(journal, 1);
1442 spin_lock(&journal->j_state_lock);
1443 journal->j_tail = old_tail;
1445 J_ASSERT(!journal->j_running_transaction);
1446 J_ASSERT(!journal->j_committing_transaction);
1447 J_ASSERT(!journal->j_checkpoint_transactions);
1448 J_ASSERT(journal->j_head == journal->j_tail);
1449 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1450 spin_unlock(&journal->j_state_lock);
1451 return 0;
1455 * int journal_wipe() - Wipe journal contents
1456 * @journal: Journal to act on.
1457 * @write: flag (see below)
1459 * Wipe out all of the contents of a journal, safely. This will produce
1460 * a warning if the journal contains any valid recovery information.
1461 * Must be called between journal_init_*() and journal_load().
1463 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1464 * we merely suppress recovery.
1467 int journal_wipe(journal_t *journal, int write)
1469 journal_superblock_t *sb;
1470 int err = 0;
1472 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1474 err = load_superblock(journal);
1475 if (err)
1476 return err;
1478 sb = journal->j_superblock;
1480 if (!journal->j_tail)
1481 goto no_recovery;
1483 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1484 write ? "Clearing" : "Ignoring");
1486 err = journal_skip_recovery(journal);
1487 if (write)
1488 journal_update_superblock(journal, 1);
1490 no_recovery:
1491 return err;
1495 * journal_dev_name: format a character string to describe on what
1496 * device this journal is present.
1499 static const char *journal_dev_name(journal_t *journal, char *buffer)
1501 struct block_device *bdev;
1503 if (journal->j_inode)
1504 bdev = journal->j_inode->i_sb->s_bdev;
1505 else
1506 bdev = journal->j_dev;
1508 return bdevname(bdev, buffer);
1512 * Journal abort has very specific semantics, which we describe
1513 * for journal abort.
1515 * Two internal function, which provide abort to te jbd layer
1516 * itself are here.
1520 * Quick version for internal journal use (doesn't lock the journal).
1521 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1522 * and don't attempt to make any other journal updates.
1524 static void __journal_abort_hard(journal_t *journal)
1526 transaction_t *transaction;
1527 char b[BDEVNAME_SIZE];
1529 if (journal->j_flags & JFS_ABORT)
1530 return;
1532 printk(KERN_ERR "Aborting journal on device %s.\n",
1533 journal_dev_name(journal, b));
1535 spin_lock(&journal->j_state_lock);
1536 journal->j_flags |= JFS_ABORT;
1537 transaction = journal->j_running_transaction;
1538 if (transaction)
1539 __log_start_commit(journal, transaction->t_tid);
1540 spin_unlock(&journal->j_state_lock);
1543 /* Soft abort: record the abort error status in the journal superblock,
1544 * but don't do any other IO. */
1545 static void __journal_abort_soft (journal_t *journal, int errno)
1547 if (journal->j_flags & JFS_ABORT)
1548 return;
1550 if (!journal->j_errno)
1551 journal->j_errno = errno;
1553 __journal_abort_hard(journal);
1555 if (errno)
1556 journal_update_superblock(journal, 1);
1560 * void journal_abort () - Shutdown the journal immediately.
1561 * @journal: the journal to shutdown.
1562 * @errno: an error number to record in the journal indicating
1563 * the reason for the shutdown.
1565 * Perform a complete, immediate shutdown of the ENTIRE
1566 * journal (not of a single transaction). This operation cannot be
1567 * undone without closing and reopening the journal.
1569 * The journal_abort function is intended to support higher level error
1570 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1571 * mode.
1573 * Journal abort has very specific semantics. Any existing dirty,
1574 * unjournaled buffers in the main filesystem will still be written to
1575 * disk by bdflush, but the journaling mechanism will be suspended
1576 * immediately and no further transaction commits will be honoured.
1578 * Any dirty, journaled buffers will be written back to disk without
1579 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1580 * filesystem, but we _do_ attempt to leave as much data as possible
1581 * behind for fsck to use for cleanup.
1583 * Any attempt to get a new transaction handle on a journal which is in
1584 * ABORT state will just result in an -EROFS error return. A
1585 * journal_stop on an existing handle will return -EIO if we have
1586 * entered abort state during the update.
1588 * Recursive transactions are not disturbed by journal abort until the
1589 * final journal_stop, which will receive the -EIO error.
1591 * Finally, the journal_abort call allows the caller to supply an errno
1592 * which will be recorded (if possible) in the journal superblock. This
1593 * allows a client to record failure conditions in the middle of a
1594 * transaction without having to complete the transaction to record the
1595 * failure to disk. ext3_error, for example, now uses this
1596 * functionality.
1598 * Errors which originate from within the journaling layer will NOT
1599 * supply an errno; a null errno implies that absolutely no further
1600 * writes are done to the journal (unless there are any already in
1601 * progress).
1605 void journal_abort(journal_t *journal, int errno)
1607 __journal_abort_soft(journal, errno);
1611 * int journal_errno () - returns the journal's error state.
1612 * @journal: journal to examine.
1614 * This is the errno numbet set with journal_abort(), the last
1615 * time the journal was mounted - if the journal was stopped
1616 * without calling abort this will be 0.
1618 * If the journal has been aborted on this mount time -EROFS will
1619 * be returned.
1621 int journal_errno(journal_t *journal)
1623 int err;
1625 spin_lock(&journal->j_state_lock);
1626 if (journal->j_flags & JFS_ABORT)
1627 err = -EROFS;
1628 else
1629 err = journal->j_errno;
1630 spin_unlock(&journal->j_state_lock);
1631 return err;
1635 * int journal_clear_err () - clears the journal's error state
1636 * @journal: journal to act on.
1638 * An error must be cleared or Acked to take a FS out of readonly
1639 * mode.
1641 int journal_clear_err(journal_t *journal)
1643 int err = 0;
1645 spin_lock(&journal->j_state_lock);
1646 if (journal->j_flags & JFS_ABORT)
1647 err = -EROFS;
1648 else
1649 journal->j_errno = 0;
1650 spin_unlock(&journal->j_state_lock);
1651 return err;
1655 * void journal_ack_err() - Ack journal err.
1656 * @journal: journal to act on.
1658 * An error must be cleared or Acked to take a FS out of readonly
1659 * mode.
1661 void journal_ack_err(journal_t *journal)
1663 spin_lock(&journal->j_state_lock);
1664 if (journal->j_errno)
1665 journal->j_flags |= JFS_ACK_ERR;
1666 spin_unlock(&journal->j_state_lock);
1669 int journal_blocks_per_page(struct inode *inode)
1671 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1675 * Journal_head storage management
1677 static struct kmem_cache *journal_head_cache;
1678 #ifdef CONFIG_JBD_DEBUG
1679 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1680 #endif
1682 static int journal_init_journal_head_cache(void)
1684 int retval;
1686 J_ASSERT(journal_head_cache == NULL);
1687 journal_head_cache = kmem_cache_create("journal_head",
1688 sizeof(struct journal_head),
1689 0, /* offset */
1690 SLAB_TEMPORARY, /* flags */
1691 NULL); /* ctor */
1692 retval = 0;
1693 if (!journal_head_cache) {
1694 retval = -ENOMEM;
1695 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1697 return retval;
1700 static void journal_destroy_journal_head_cache(void)
1702 if (journal_head_cache) {
1703 kmem_cache_destroy(journal_head_cache);
1704 journal_head_cache = NULL;
1709 * journal_head splicing and dicing
1711 static struct journal_head *journal_alloc_journal_head(void)
1713 struct journal_head *ret;
1714 static unsigned long last_warning;
1716 #ifdef CONFIG_JBD_DEBUG
1717 atomic_inc(&nr_journal_heads);
1718 #endif
1719 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1720 if (ret == NULL) {
1721 jbd_debug(1, "out of memory for journal_head\n");
1722 if (time_after(jiffies, last_warning + 5*HZ)) {
1723 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1724 __func__);
1725 last_warning = jiffies;
1727 while (ret == NULL) {
1728 yield();
1729 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1732 return ret;
1735 static void journal_free_journal_head(struct journal_head *jh)
1737 #ifdef CONFIG_JBD_DEBUG
1738 atomic_dec(&nr_journal_heads);
1739 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1740 #endif
1741 kmem_cache_free(journal_head_cache, jh);
1745 * A journal_head is attached to a buffer_head whenever JBD has an
1746 * interest in the buffer.
1748 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1749 * is set. This bit is tested in core kernel code where we need to take
1750 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1751 * there.
1753 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1755 * When a buffer has its BH_JBD bit set it is immune from being released by
1756 * core kernel code, mainly via ->b_count.
1758 * A journal_head may be detached from its buffer_head when the journal_head's
1759 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1760 * Various places in JBD call journal_remove_journal_head() to indicate that the
1761 * journal_head can be dropped if needed.
1763 * Various places in the kernel want to attach a journal_head to a buffer_head
1764 * _before_ attaching the journal_head to a transaction. To protect the
1765 * journal_head in this situation, journal_add_journal_head elevates the
1766 * journal_head's b_jcount refcount by one. The caller must call
1767 * journal_put_journal_head() to undo this.
1769 * So the typical usage would be:
1771 * (Attach a journal_head if needed. Increments b_jcount)
1772 * struct journal_head *jh = journal_add_journal_head(bh);
1773 * ...
1774 * jh->b_transaction = xxx;
1775 * journal_put_journal_head(jh);
1777 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1778 * because it has a non-zero b_transaction.
1782 * Give a buffer_head a journal_head.
1784 * Doesn't need the journal lock.
1785 * May sleep.
1787 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1789 struct journal_head *jh;
1790 struct journal_head *new_jh = NULL;
1792 repeat:
1793 if (!buffer_jbd(bh)) {
1794 new_jh = journal_alloc_journal_head();
1795 memset(new_jh, 0, sizeof(*new_jh));
1798 jbd_lock_bh_journal_head(bh);
1799 if (buffer_jbd(bh)) {
1800 jh = bh2jh(bh);
1801 } else {
1802 J_ASSERT_BH(bh,
1803 (atomic_read(&bh->b_count) > 0) ||
1804 (bh->b_page && bh->b_page->mapping));
1806 if (!new_jh) {
1807 jbd_unlock_bh_journal_head(bh);
1808 goto repeat;
1811 jh = new_jh;
1812 new_jh = NULL; /* We consumed it */
1813 set_buffer_jbd(bh);
1814 bh->b_private = jh;
1815 jh->b_bh = bh;
1816 get_bh(bh);
1817 BUFFER_TRACE(bh, "added journal_head");
1819 jh->b_jcount++;
1820 jbd_unlock_bh_journal_head(bh);
1821 if (new_jh)
1822 journal_free_journal_head(new_jh);
1823 return bh->b_private;
1827 * Grab a ref against this buffer_head's journal_head. If it ended up not
1828 * having a journal_head, return NULL
1830 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1832 struct journal_head *jh = NULL;
1834 jbd_lock_bh_journal_head(bh);
1835 if (buffer_jbd(bh)) {
1836 jh = bh2jh(bh);
1837 jh->b_jcount++;
1839 jbd_unlock_bh_journal_head(bh);
1840 return jh;
1843 static void __journal_remove_journal_head(struct buffer_head *bh)
1845 struct journal_head *jh = bh2jh(bh);
1847 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1849 get_bh(bh);
1850 if (jh->b_jcount == 0) {
1851 if (jh->b_transaction == NULL &&
1852 jh->b_next_transaction == NULL &&
1853 jh->b_cp_transaction == NULL) {
1854 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1855 J_ASSERT_BH(bh, buffer_jbd(bh));
1856 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1857 BUFFER_TRACE(bh, "remove journal_head");
1858 if (jh->b_frozen_data) {
1859 printk(KERN_WARNING "%s: freeing "
1860 "b_frozen_data\n",
1861 __func__);
1862 jbd_free(jh->b_frozen_data, bh->b_size);
1864 if (jh->b_committed_data) {
1865 printk(KERN_WARNING "%s: freeing "
1866 "b_committed_data\n",
1867 __func__);
1868 jbd_free(jh->b_committed_data, bh->b_size);
1870 bh->b_private = NULL;
1871 jh->b_bh = NULL; /* debug, really */
1872 clear_buffer_jbd(bh);
1873 __brelse(bh);
1874 journal_free_journal_head(jh);
1875 } else {
1876 BUFFER_TRACE(bh, "journal_head was locked");
1882 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1883 * and has a zero b_jcount then remove and release its journal_head. If we did
1884 * see that the buffer is not used by any transaction we also "logically"
1885 * decrement ->b_count.
1887 * We in fact take an additional increment on ->b_count as a convenience,
1888 * because the caller usually wants to do additional things with the bh
1889 * after calling here.
1890 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1891 * time. Once the caller has run __brelse(), the buffer is eligible for
1892 * reaping by try_to_free_buffers().
1894 void journal_remove_journal_head(struct buffer_head *bh)
1896 jbd_lock_bh_journal_head(bh);
1897 __journal_remove_journal_head(bh);
1898 jbd_unlock_bh_journal_head(bh);
1902 * Drop a reference on the passed journal_head. If it fell to zero then try to
1903 * release the journal_head from the buffer_head.
1905 void journal_put_journal_head(struct journal_head *jh)
1907 struct buffer_head *bh = jh2bh(jh);
1909 jbd_lock_bh_journal_head(bh);
1910 J_ASSERT_JH(jh, jh->b_jcount > 0);
1911 --jh->b_jcount;
1912 if (!jh->b_jcount && !jh->b_transaction) {
1913 __journal_remove_journal_head(bh);
1914 __brelse(bh);
1916 jbd_unlock_bh_journal_head(bh);
1920 * debugfs tunables
1922 #ifdef CONFIG_JBD_DEBUG
1924 u8 journal_enable_debug __read_mostly;
1925 EXPORT_SYMBOL(journal_enable_debug);
1927 static struct dentry *jbd_debugfs_dir;
1928 static struct dentry *jbd_debug;
1930 static void __init jbd_create_debugfs_entry(void)
1932 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1933 if (jbd_debugfs_dir)
1934 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
1935 jbd_debugfs_dir,
1936 &journal_enable_debug);
1939 static void __exit jbd_remove_debugfs_entry(void)
1941 debugfs_remove(jbd_debug);
1942 debugfs_remove(jbd_debugfs_dir);
1945 #else
1947 static inline void jbd_create_debugfs_entry(void)
1951 static inline void jbd_remove_debugfs_entry(void)
1955 #endif
1957 struct kmem_cache *jbd_handle_cache;
1959 static int __init journal_init_handle_cache(void)
1961 jbd_handle_cache = kmem_cache_create("journal_handle",
1962 sizeof(handle_t),
1963 0, /* offset */
1964 SLAB_TEMPORARY, /* flags */
1965 NULL); /* ctor */
1966 if (jbd_handle_cache == NULL) {
1967 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1968 return -ENOMEM;
1970 return 0;
1973 static void journal_destroy_handle_cache(void)
1975 if (jbd_handle_cache)
1976 kmem_cache_destroy(jbd_handle_cache);
1980 * Module startup and shutdown
1983 static int __init journal_init_caches(void)
1985 int ret;
1987 ret = journal_init_revoke_caches();
1988 if (ret == 0)
1989 ret = journal_init_journal_head_cache();
1990 if (ret == 0)
1991 ret = journal_init_handle_cache();
1992 return ret;
1995 static void journal_destroy_caches(void)
1997 journal_destroy_revoke_caches();
1998 journal_destroy_journal_head_cache();
1999 journal_destroy_handle_cache();
2002 static int __init journal_init(void)
2004 int ret;
2006 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2008 ret = journal_init_caches();
2009 if (ret != 0)
2010 journal_destroy_caches();
2011 jbd_create_debugfs_entry();
2012 return ret;
2015 static void __exit journal_exit(void)
2017 #ifdef CONFIG_JBD_DEBUG
2018 int n = atomic_read(&nr_journal_heads);
2019 if (n)
2020 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2021 #endif
2022 jbd_remove_debugfs_entry();
2023 journal_destroy_caches();
2026 MODULE_LICENSE("GPL");
2027 module_init(journal_init);
2028 module_exit(journal_exit);