hwmon: (applesmc) Add iMac9,1 and MacBookPro2,2 support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / jbd / journal.c
blobbd224eec9b072d5ebe0a075c57e203b9fde61c1d
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 * Are we already doing a recent enough commit?
440 if (!tid_geq(journal->j_commit_request, target)) {
442 * We want a new commit: OK, mark the request and wakup the
443 * commit thread. We do _not_ do the commit ourselves.
446 journal->j_commit_request = target;
447 jbd_debug(1, "JBD: requesting commit %d/%d\n",
448 journal->j_commit_request,
449 journal->j_commit_sequence);
450 wake_up(&journal->j_wait_commit);
451 return 1;
453 return 0;
456 int log_start_commit(journal_t *journal, tid_t tid)
458 int ret;
460 spin_lock(&journal->j_state_lock);
461 ret = __log_start_commit(journal, tid);
462 spin_unlock(&journal->j_state_lock);
463 return ret;
467 * Force and wait upon a commit if the calling process is not within
468 * transaction. This is used for forcing out undo-protected data which contains
469 * bitmaps, when the fs is running out of space.
471 * We can only force the running transaction if we don't have an active handle;
472 * otherwise, we will deadlock.
474 * Returns true if a transaction was started.
476 int journal_force_commit_nested(journal_t *journal)
478 transaction_t *transaction = NULL;
479 tid_t tid;
481 spin_lock(&journal->j_state_lock);
482 if (journal->j_running_transaction && !current->journal_info) {
483 transaction = journal->j_running_transaction;
484 __log_start_commit(journal, transaction->t_tid);
485 } else if (journal->j_committing_transaction)
486 transaction = journal->j_committing_transaction;
488 if (!transaction) {
489 spin_unlock(&journal->j_state_lock);
490 return 0; /* Nothing to retry */
493 tid = transaction->t_tid;
494 spin_unlock(&journal->j_state_lock);
495 log_wait_commit(journal, tid);
496 return 1;
500 * Start a commit of the current running transaction (if any). Returns true
501 * if a transaction is going to be committed (or is currently already
502 * committing), and fills its tid in at *ptid
504 int journal_start_commit(journal_t *journal, tid_t *ptid)
506 int ret = 0;
508 spin_lock(&journal->j_state_lock);
509 if (journal->j_running_transaction) {
510 tid_t tid = journal->j_running_transaction->t_tid;
512 __log_start_commit(journal, tid);
513 /* There's a running transaction and we've just made sure
514 * it's commit has been scheduled. */
515 if (ptid)
516 *ptid = tid;
517 ret = 1;
518 } else if (journal->j_committing_transaction) {
520 * If ext3_write_super() recently started a commit, then we
521 * have to wait for completion of that transaction
523 if (ptid)
524 *ptid = journal->j_committing_transaction->t_tid;
525 ret = 1;
527 spin_unlock(&journal->j_state_lock);
528 return ret;
532 * Wait for a specified commit to complete.
533 * The caller may not hold the journal lock.
535 int log_wait_commit(journal_t *journal, tid_t tid)
537 int err = 0;
539 #ifdef CONFIG_JBD_DEBUG
540 spin_lock(&journal->j_state_lock);
541 if (!tid_geq(journal->j_commit_request, tid)) {
542 printk(KERN_EMERG
543 "%s: error: j_commit_request=%d, tid=%d\n",
544 __func__, journal->j_commit_request, tid);
546 spin_unlock(&journal->j_state_lock);
547 #endif
548 spin_lock(&journal->j_state_lock);
549 while (tid_gt(tid, journal->j_commit_sequence)) {
550 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
551 tid, journal->j_commit_sequence);
552 wake_up(&journal->j_wait_commit);
553 spin_unlock(&journal->j_state_lock);
554 wait_event(journal->j_wait_done_commit,
555 !tid_gt(tid, journal->j_commit_sequence));
556 spin_lock(&journal->j_state_lock);
558 spin_unlock(&journal->j_state_lock);
560 if (unlikely(is_journal_aborted(journal))) {
561 printk(KERN_EMERG "journal commit I/O error\n");
562 err = -EIO;
564 return err;
568 * Log buffer allocation routines:
571 int journal_next_log_block(journal_t *journal, unsigned int *retp)
573 unsigned int blocknr;
575 spin_lock(&journal->j_state_lock);
576 J_ASSERT(journal->j_free > 1);
578 blocknr = journal->j_head;
579 journal->j_head++;
580 journal->j_free--;
581 if (journal->j_head == journal->j_last)
582 journal->j_head = journal->j_first;
583 spin_unlock(&journal->j_state_lock);
584 return journal_bmap(journal, blocknr, retp);
588 * Conversion of logical to physical block numbers for the journal
590 * On external journals the journal blocks are identity-mapped, so
591 * this is a no-op. If needed, we can use j_blk_offset - everything is
592 * ready.
594 int journal_bmap(journal_t *journal, unsigned int blocknr,
595 unsigned int *retp)
597 int err = 0;
598 unsigned int ret;
600 if (journal->j_inode) {
601 ret = bmap(journal->j_inode, blocknr);
602 if (ret)
603 *retp = ret;
604 else {
605 char b[BDEVNAME_SIZE];
607 printk(KERN_ALERT "%s: journal block not found "
608 "at offset %u on %s\n",
609 __func__,
610 blocknr,
611 bdevname(journal->j_dev, b));
612 err = -EIO;
613 __journal_abort_soft(journal, err);
615 } else {
616 *retp = blocknr; /* +journal->j_blk_offset */
618 return err;
622 * We play buffer_head aliasing tricks to write data/metadata blocks to
623 * the journal without copying their contents, but for journal
624 * descriptor blocks we do need to generate bona fide buffers.
626 * After the caller of journal_get_descriptor_buffer() has finished modifying
627 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
628 * But we don't bother doing that, so there will be coherency problems with
629 * mmaps of blockdevs which hold live JBD-controlled filesystems.
631 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
633 struct buffer_head *bh;
634 unsigned int blocknr;
635 int err;
637 err = journal_next_log_block(journal, &blocknr);
639 if (err)
640 return NULL;
642 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
643 if (!bh)
644 return NULL;
645 lock_buffer(bh);
646 memset(bh->b_data, 0, journal->j_blocksize);
647 set_buffer_uptodate(bh);
648 unlock_buffer(bh);
649 BUFFER_TRACE(bh, "return this buffer");
650 return journal_add_journal_head(bh);
654 * Management for journal control blocks: functions to create and
655 * destroy journal_t structures, and to initialise and read existing
656 * journal blocks from disk. */
658 /* First: create and setup a journal_t object in memory. We initialise
659 * very few fields yet: that has to wait until we have created the
660 * journal structures from from scratch, or loaded them from disk. */
662 static journal_t * journal_init_common (void)
664 journal_t *journal;
665 int err;
667 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
668 if (!journal)
669 goto fail;
671 init_waitqueue_head(&journal->j_wait_transaction_locked);
672 init_waitqueue_head(&journal->j_wait_logspace);
673 init_waitqueue_head(&journal->j_wait_done_commit);
674 init_waitqueue_head(&journal->j_wait_checkpoint);
675 init_waitqueue_head(&journal->j_wait_commit);
676 init_waitqueue_head(&journal->j_wait_updates);
677 mutex_init(&journal->j_barrier);
678 mutex_init(&journal->j_checkpoint_mutex);
679 spin_lock_init(&journal->j_revoke_lock);
680 spin_lock_init(&journal->j_list_lock);
681 spin_lock_init(&journal->j_state_lock);
683 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
685 /* The journal is marked for error until we succeed with recovery! */
686 journal->j_flags = JFS_ABORT;
688 /* Set up a default-sized revoke table for the new mount. */
689 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
690 if (err) {
691 kfree(journal);
692 goto fail;
694 return journal;
695 fail:
696 return NULL;
699 /* journal_init_dev and journal_init_inode:
701 * Create a journal structure assigned some fixed set of disk blocks to
702 * the journal. We don't actually touch those disk blocks yet, but we
703 * need to set up all of the mapping information to tell the journaling
704 * system where the journal blocks are.
709 * journal_t * journal_init_dev() - creates and initialises a journal structure
710 * @bdev: Block device on which to create the journal
711 * @fs_dev: Device which hold journalled filesystem for this journal.
712 * @start: Block nr Start of journal.
713 * @len: Length of the journal in blocks.
714 * @blocksize: blocksize of journalling device
716 * Returns: a newly created journal_t *
718 * journal_init_dev creates a journal which maps a fixed contiguous
719 * range of blocks on an arbitrary block device.
722 journal_t * journal_init_dev(struct block_device *bdev,
723 struct block_device *fs_dev,
724 int start, int len, int blocksize)
726 journal_t *journal = journal_init_common();
727 struct buffer_head *bh;
728 int n;
730 if (!journal)
731 return NULL;
733 /* journal descriptor can store up to n blocks -bzzz */
734 journal->j_blocksize = blocksize;
735 n = journal->j_blocksize / sizeof(journal_block_tag_t);
736 journal->j_wbufsize = n;
737 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
738 if (!journal->j_wbuf) {
739 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
740 __func__);
741 goto out_err;
743 journal->j_dev = bdev;
744 journal->j_fs_dev = fs_dev;
745 journal->j_blk_offset = start;
746 journal->j_maxlen = len;
748 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
749 if (!bh) {
750 printk(KERN_ERR
751 "%s: Cannot get buffer for journal superblock\n",
752 __func__);
753 goto out_err;
755 journal->j_sb_buffer = bh;
756 journal->j_superblock = (journal_superblock_t *)bh->b_data;
758 return journal;
759 out_err:
760 kfree(journal->j_wbuf);
761 kfree(journal);
762 return NULL;
766 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
767 * @inode: An inode to create the journal in
769 * journal_init_inode creates a journal which maps an on-disk inode as
770 * the journal. The inode must exist already, must support bmap() and
771 * must have all data blocks preallocated.
773 journal_t * journal_init_inode (struct inode *inode)
775 struct buffer_head *bh;
776 journal_t *journal = journal_init_common();
777 int err;
778 int n;
779 unsigned int blocknr;
781 if (!journal)
782 return NULL;
784 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
785 journal->j_inode = inode;
786 jbd_debug(1,
787 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
788 journal, inode->i_sb->s_id, inode->i_ino,
789 (long long) inode->i_size,
790 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
792 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
793 journal->j_blocksize = inode->i_sb->s_blocksize;
795 /* journal descriptor can store up to n blocks -bzzz */
796 n = journal->j_blocksize / sizeof(journal_block_tag_t);
797 journal->j_wbufsize = n;
798 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
799 if (!journal->j_wbuf) {
800 printk(KERN_ERR "%s: Cant allocate bhs for commit thread\n",
801 __func__);
802 goto out_err;
805 err = journal_bmap(journal, 0, &blocknr);
806 /* If that failed, give up */
807 if (err) {
808 printk(KERN_ERR "%s: Cannnot locate journal superblock\n",
809 __func__);
810 goto out_err;
813 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
814 if (!bh) {
815 printk(KERN_ERR
816 "%s: Cannot get buffer for journal superblock\n",
817 __func__);
818 goto out_err;
820 journal->j_sb_buffer = bh;
821 journal->j_superblock = (journal_superblock_t *)bh->b_data;
823 return journal;
824 out_err:
825 kfree(journal->j_wbuf);
826 kfree(journal);
827 return NULL;
831 * If the journal init or create aborts, we need to mark the journal
832 * superblock as being NULL to prevent the journal destroy from writing
833 * back a bogus superblock.
835 static void journal_fail_superblock (journal_t *journal)
837 struct buffer_head *bh = journal->j_sb_buffer;
838 brelse(bh);
839 journal->j_sb_buffer = NULL;
843 * Given a journal_t structure, initialise the various fields for
844 * startup of a new journaling session. We use this both when creating
845 * a journal, and after recovering an old journal to reset it for
846 * subsequent use.
849 static int journal_reset(journal_t *journal)
851 journal_superblock_t *sb = journal->j_superblock;
852 unsigned int first, last;
854 first = be32_to_cpu(sb->s_first);
855 last = be32_to_cpu(sb->s_maxlen);
856 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
857 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
858 first, last);
859 journal_fail_superblock(journal);
860 return -EINVAL;
863 journal->j_first = first;
864 journal->j_last = last;
866 journal->j_head = first;
867 journal->j_tail = first;
868 journal->j_free = last - first;
870 journal->j_tail_sequence = journal->j_transaction_sequence;
871 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
872 journal->j_commit_request = journal->j_commit_sequence;
874 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
876 /* Add the dynamic fields and write it to disk. */
877 journal_update_superblock(journal, 1);
878 return journal_start_thread(journal);
882 * int journal_create() - Initialise the new journal file
883 * @journal: Journal to create. This structure must have been initialised
885 * Given a journal_t structure which tells us which disk blocks we can
886 * use, create a new journal superblock and initialise all of the
887 * journal fields from scratch.
889 int journal_create(journal_t *journal)
891 unsigned int blocknr;
892 struct buffer_head *bh;
893 journal_superblock_t *sb;
894 int i, err;
896 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
897 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
898 journal->j_maxlen);
899 journal_fail_superblock(journal);
900 return -EINVAL;
903 if (journal->j_inode == NULL) {
905 * We don't know what block to start at!
907 printk(KERN_EMERG
908 "%s: creation of journal on external device!\n",
909 __func__);
910 BUG();
913 /* Zero out the entire journal on disk. We cannot afford to
914 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
915 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
916 for (i = 0; i < journal->j_maxlen; i++) {
917 err = journal_bmap(journal, i, &blocknr);
918 if (err)
919 return err;
920 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
921 lock_buffer(bh);
922 memset (bh->b_data, 0, journal->j_blocksize);
923 BUFFER_TRACE(bh, "marking dirty");
924 mark_buffer_dirty(bh);
925 BUFFER_TRACE(bh, "marking uptodate");
926 set_buffer_uptodate(bh);
927 unlock_buffer(bh);
928 __brelse(bh);
931 sync_blockdev(journal->j_dev);
932 jbd_debug(1, "JBD: journal cleared.\n");
934 /* OK, fill in the initial static fields in the new superblock */
935 sb = journal->j_superblock;
937 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
938 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
940 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
941 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
942 sb->s_first = cpu_to_be32(1);
944 journal->j_transaction_sequence = 1;
946 journal->j_flags &= ~JFS_ABORT;
947 journal->j_format_version = 2;
949 return journal_reset(journal);
953 * void journal_update_superblock() - Update journal sb on disk.
954 * @journal: The journal to update.
955 * @wait: Set to '0' if you don't want to wait for IO completion.
957 * Update a journal's dynamic superblock fields and write it to disk,
958 * optionally waiting for the IO to complete.
960 void journal_update_superblock(journal_t *journal, int wait)
962 journal_superblock_t *sb = journal->j_superblock;
963 struct buffer_head *bh = journal->j_sb_buffer;
966 * As a special case, if the on-disk copy is already marked as needing
967 * no recovery (s_start == 0) and there are no outstanding transactions
968 * in the filesystem, then we can safely defer the superblock update
969 * until the next commit by setting JFS_FLUSHED. This avoids
970 * attempting a write to a potential-readonly device.
972 if (sb->s_start == 0 && journal->j_tail_sequence ==
973 journal->j_transaction_sequence) {
974 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
975 "(start %u, seq %d, errno %d)\n",
976 journal->j_tail, journal->j_tail_sequence,
977 journal->j_errno);
978 goto out;
981 spin_lock(&journal->j_state_lock);
982 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
983 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
985 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
986 sb->s_start = cpu_to_be32(journal->j_tail);
987 sb->s_errno = cpu_to_be32(journal->j_errno);
988 spin_unlock(&journal->j_state_lock);
990 BUFFER_TRACE(bh, "marking dirty");
991 mark_buffer_dirty(bh);
992 if (wait)
993 sync_dirty_buffer(bh);
994 else
995 ll_rw_block(SWRITE, 1, &bh);
997 out:
998 /* If we have just flushed the log (by marking s_start==0), then
999 * any future commit will have to be careful to update the
1000 * superblock again to re-record the true start of the log. */
1002 spin_lock(&journal->j_state_lock);
1003 if (sb->s_start)
1004 journal->j_flags &= ~JFS_FLUSHED;
1005 else
1006 journal->j_flags |= JFS_FLUSHED;
1007 spin_unlock(&journal->j_state_lock);
1011 * Read the superblock for a given journal, performing initial
1012 * validation of the format.
1015 static int journal_get_superblock(journal_t *journal)
1017 struct buffer_head *bh;
1018 journal_superblock_t *sb;
1019 int err = -EIO;
1021 bh = journal->j_sb_buffer;
1023 J_ASSERT(bh != NULL);
1024 if (!buffer_uptodate(bh)) {
1025 ll_rw_block(READ, 1, &bh);
1026 wait_on_buffer(bh);
1027 if (!buffer_uptodate(bh)) {
1028 printk (KERN_ERR
1029 "JBD: IO error reading journal superblock\n");
1030 goto out;
1034 sb = journal->j_superblock;
1036 err = -EINVAL;
1038 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1039 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1040 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1041 goto out;
1044 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1045 case JFS_SUPERBLOCK_V1:
1046 journal->j_format_version = 1;
1047 break;
1048 case JFS_SUPERBLOCK_V2:
1049 journal->j_format_version = 2;
1050 break;
1051 default:
1052 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1053 goto out;
1056 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1057 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1058 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1059 printk (KERN_WARNING "JBD: journal file too short\n");
1060 goto out;
1063 return 0;
1065 out:
1066 journal_fail_superblock(journal);
1067 return err;
1071 * Load the on-disk journal superblock and read the key fields into the
1072 * journal_t.
1075 static int load_superblock(journal_t *journal)
1077 int err;
1078 journal_superblock_t *sb;
1080 err = journal_get_superblock(journal);
1081 if (err)
1082 return err;
1084 sb = journal->j_superblock;
1086 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1087 journal->j_tail = be32_to_cpu(sb->s_start);
1088 journal->j_first = be32_to_cpu(sb->s_first);
1089 journal->j_last = be32_to_cpu(sb->s_maxlen);
1090 journal->j_errno = be32_to_cpu(sb->s_errno);
1092 return 0;
1097 * int journal_load() - Read journal from disk.
1098 * @journal: Journal to act on.
1100 * Given a journal_t structure which tells us which disk blocks contain
1101 * a journal, read the journal from disk to initialise the in-memory
1102 * structures.
1104 int journal_load(journal_t *journal)
1106 int err;
1107 journal_superblock_t *sb;
1109 err = load_superblock(journal);
1110 if (err)
1111 return err;
1113 sb = journal->j_superblock;
1114 /* If this is a V2 superblock, then we have to check the
1115 * features flags on it. */
1117 if (journal->j_format_version >= 2) {
1118 if ((sb->s_feature_ro_compat &
1119 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1120 (sb->s_feature_incompat &
1121 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1122 printk (KERN_WARNING
1123 "JBD: Unrecognised features on journal\n");
1124 return -EINVAL;
1128 /* Let the recovery code check whether it needs to recover any
1129 * data from the journal. */
1130 if (journal_recover(journal))
1131 goto recovery_error;
1133 /* OK, we've finished with the dynamic journal bits:
1134 * reinitialise the dynamic contents of the superblock in memory
1135 * and reset them on disk. */
1136 if (journal_reset(journal))
1137 goto recovery_error;
1139 journal->j_flags &= ~JFS_ABORT;
1140 journal->j_flags |= JFS_LOADED;
1141 return 0;
1143 recovery_error:
1144 printk (KERN_WARNING "JBD: recovery failed\n");
1145 return -EIO;
1149 * void journal_destroy() - Release a journal_t structure.
1150 * @journal: Journal to act on.
1152 * Release a journal_t structure once it is no longer in use by the
1153 * journaled object.
1154 * Return <0 if we couldn't clean up the journal.
1156 int journal_destroy(journal_t *journal)
1158 int err = 0;
1160 /* Wait for the commit thread to wake up and die. */
1161 journal_kill_thread(journal);
1163 /* Force a final log commit */
1164 if (journal->j_running_transaction)
1165 journal_commit_transaction(journal);
1167 /* Force any old transactions to disk */
1169 /* Totally anal locking here... */
1170 spin_lock(&journal->j_list_lock);
1171 while (journal->j_checkpoint_transactions != NULL) {
1172 spin_unlock(&journal->j_list_lock);
1173 log_do_checkpoint(journal);
1174 spin_lock(&journal->j_list_lock);
1177 J_ASSERT(journal->j_running_transaction == NULL);
1178 J_ASSERT(journal->j_committing_transaction == NULL);
1179 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1180 spin_unlock(&journal->j_list_lock);
1182 if (journal->j_sb_buffer) {
1183 if (!is_journal_aborted(journal)) {
1184 /* We can now mark the journal as empty. */
1185 journal->j_tail = 0;
1186 journal->j_tail_sequence =
1187 ++journal->j_transaction_sequence;
1188 journal_update_superblock(journal, 1);
1189 } else {
1190 err = -EIO;
1192 brelse(journal->j_sb_buffer);
1195 if (journal->j_inode)
1196 iput(journal->j_inode);
1197 if (journal->j_revoke)
1198 journal_destroy_revoke(journal);
1199 kfree(journal->j_wbuf);
1200 kfree(journal);
1202 return err;
1207 *int journal_check_used_features () - Check if features specified are used.
1208 * @journal: Journal to check.
1209 * @compat: bitmask of compatible features
1210 * @ro: bitmask of features that force read-only mount
1211 * @incompat: bitmask of incompatible features
1213 * Check whether the journal uses all of a given set of
1214 * features. Return true (non-zero) if it does.
1217 int journal_check_used_features (journal_t *journal, unsigned long compat,
1218 unsigned long ro, unsigned long incompat)
1220 journal_superblock_t *sb;
1222 if (!compat && !ro && !incompat)
1223 return 1;
1224 if (journal->j_format_version == 1)
1225 return 0;
1227 sb = journal->j_superblock;
1229 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1230 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1231 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1232 return 1;
1234 return 0;
1238 * int journal_check_available_features() - Check feature set in journalling layer
1239 * @journal: Journal to check.
1240 * @compat: bitmask of compatible features
1241 * @ro: bitmask of features that force read-only mount
1242 * @incompat: bitmask of incompatible features
1244 * Check whether the journaling code supports the use of
1245 * all of a given set of features on this journal. Return true
1246 * (non-zero) if it can. */
1248 int journal_check_available_features (journal_t *journal, unsigned long compat,
1249 unsigned long ro, unsigned long incompat)
1251 journal_superblock_t *sb;
1253 if (!compat && !ro && !incompat)
1254 return 1;
1256 sb = journal->j_superblock;
1258 /* We can support any known requested features iff the
1259 * superblock is in version 2. Otherwise we fail to support any
1260 * extended sb features. */
1262 if (journal->j_format_version != 2)
1263 return 0;
1265 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1266 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1267 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1268 return 1;
1270 return 0;
1274 * int journal_set_features () - Mark a given journal feature in the superblock
1275 * @journal: Journal to act on.
1276 * @compat: bitmask of compatible features
1277 * @ro: bitmask of features that force read-only mount
1278 * @incompat: bitmask of incompatible features
1280 * Mark a given journal feature as present on the
1281 * superblock. Returns true if the requested features could be set.
1285 int journal_set_features (journal_t *journal, unsigned long compat,
1286 unsigned long ro, unsigned long incompat)
1288 journal_superblock_t *sb;
1290 if (journal_check_used_features(journal, compat, ro, incompat))
1291 return 1;
1293 if (!journal_check_available_features(journal, compat, ro, incompat))
1294 return 0;
1296 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1297 compat, ro, incompat);
1299 sb = journal->j_superblock;
1301 sb->s_feature_compat |= cpu_to_be32(compat);
1302 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1303 sb->s_feature_incompat |= cpu_to_be32(incompat);
1305 return 1;
1310 * int journal_update_format () - Update on-disk journal structure.
1311 * @journal: Journal to act on.
1313 * Given an initialised but unloaded journal struct, poke about in the
1314 * on-disk structure to update it to the most recent supported version.
1316 int journal_update_format (journal_t *journal)
1318 journal_superblock_t *sb;
1319 int err;
1321 err = journal_get_superblock(journal);
1322 if (err)
1323 return err;
1325 sb = journal->j_superblock;
1327 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1328 case JFS_SUPERBLOCK_V2:
1329 return 0;
1330 case JFS_SUPERBLOCK_V1:
1331 return journal_convert_superblock_v1(journal, sb);
1332 default:
1333 break;
1335 return -EINVAL;
1338 static int journal_convert_superblock_v1(journal_t *journal,
1339 journal_superblock_t *sb)
1341 int offset, blocksize;
1342 struct buffer_head *bh;
1344 printk(KERN_WARNING
1345 "JBD: Converting superblock from version 1 to 2.\n");
1347 /* Pre-initialise new fields to zero */
1348 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1349 blocksize = be32_to_cpu(sb->s_blocksize);
1350 memset(&sb->s_feature_compat, 0, blocksize-offset);
1352 sb->s_nr_users = cpu_to_be32(1);
1353 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1354 journal->j_format_version = 2;
1356 bh = journal->j_sb_buffer;
1357 BUFFER_TRACE(bh, "marking dirty");
1358 mark_buffer_dirty(bh);
1359 sync_dirty_buffer(bh);
1360 return 0;
1365 * int journal_flush () - Flush journal
1366 * @journal: Journal to act on.
1368 * Flush all data for a given journal to disk and empty the journal.
1369 * Filesystems can use this when remounting readonly to ensure that
1370 * recovery does not need to happen on remount.
1373 int journal_flush(journal_t *journal)
1375 int err = 0;
1376 transaction_t *transaction = NULL;
1377 unsigned int old_tail;
1379 spin_lock(&journal->j_state_lock);
1381 /* Force everything buffered to the log... */
1382 if (journal->j_running_transaction) {
1383 transaction = journal->j_running_transaction;
1384 __log_start_commit(journal, transaction->t_tid);
1385 } else if (journal->j_committing_transaction)
1386 transaction = journal->j_committing_transaction;
1388 /* Wait for the log commit to complete... */
1389 if (transaction) {
1390 tid_t tid = transaction->t_tid;
1392 spin_unlock(&journal->j_state_lock);
1393 log_wait_commit(journal, tid);
1394 } else {
1395 spin_unlock(&journal->j_state_lock);
1398 /* ...and flush everything in the log out to disk. */
1399 spin_lock(&journal->j_list_lock);
1400 while (!err && journal->j_checkpoint_transactions != NULL) {
1401 spin_unlock(&journal->j_list_lock);
1402 mutex_lock(&journal->j_checkpoint_mutex);
1403 err = log_do_checkpoint(journal);
1404 mutex_unlock(&journal->j_checkpoint_mutex);
1405 spin_lock(&journal->j_list_lock);
1407 spin_unlock(&journal->j_list_lock);
1409 if (is_journal_aborted(journal))
1410 return -EIO;
1412 cleanup_journal_tail(journal);
1414 /* Finally, mark the journal as really needing no recovery.
1415 * This sets s_start==0 in the underlying superblock, which is
1416 * the magic code for a fully-recovered superblock. Any future
1417 * commits of data to the journal will restore the current
1418 * s_start value. */
1419 spin_lock(&journal->j_state_lock);
1420 old_tail = journal->j_tail;
1421 journal->j_tail = 0;
1422 spin_unlock(&journal->j_state_lock);
1423 journal_update_superblock(journal, 1);
1424 spin_lock(&journal->j_state_lock);
1425 journal->j_tail = old_tail;
1427 J_ASSERT(!journal->j_running_transaction);
1428 J_ASSERT(!journal->j_committing_transaction);
1429 J_ASSERT(!journal->j_checkpoint_transactions);
1430 J_ASSERT(journal->j_head == journal->j_tail);
1431 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1432 spin_unlock(&journal->j_state_lock);
1433 return 0;
1437 * int journal_wipe() - Wipe journal contents
1438 * @journal: Journal to act on.
1439 * @write: flag (see below)
1441 * Wipe out all of the contents of a journal, safely. This will produce
1442 * a warning if the journal contains any valid recovery information.
1443 * Must be called between journal_init_*() and journal_load().
1445 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1446 * we merely suppress recovery.
1449 int journal_wipe(journal_t *journal, int write)
1451 journal_superblock_t *sb;
1452 int err = 0;
1454 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1456 err = load_superblock(journal);
1457 if (err)
1458 return err;
1460 sb = journal->j_superblock;
1462 if (!journal->j_tail)
1463 goto no_recovery;
1465 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1466 write ? "Clearing" : "Ignoring");
1468 err = journal_skip_recovery(journal);
1469 if (write)
1470 journal_update_superblock(journal, 1);
1472 no_recovery:
1473 return err;
1477 * journal_dev_name: format a character string to describe on what
1478 * device this journal is present.
1481 static const char *journal_dev_name(journal_t *journal, char *buffer)
1483 struct block_device *bdev;
1485 if (journal->j_inode)
1486 bdev = journal->j_inode->i_sb->s_bdev;
1487 else
1488 bdev = journal->j_dev;
1490 return bdevname(bdev, buffer);
1494 * Journal abort has very specific semantics, which we describe
1495 * for journal abort.
1497 * Two internal function, which provide abort to te jbd layer
1498 * itself are here.
1502 * Quick version for internal journal use (doesn't lock the journal).
1503 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1504 * and don't attempt to make any other journal updates.
1506 static void __journal_abort_hard(journal_t *journal)
1508 transaction_t *transaction;
1509 char b[BDEVNAME_SIZE];
1511 if (journal->j_flags & JFS_ABORT)
1512 return;
1514 printk(KERN_ERR "Aborting journal on device %s.\n",
1515 journal_dev_name(journal, b));
1517 spin_lock(&journal->j_state_lock);
1518 journal->j_flags |= JFS_ABORT;
1519 transaction = journal->j_running_transaction;
1520 if (transaction)
1521 __log_start_commit(journal, transaction->t_tid);
1522 spin_unlock(&journal->j_state_lock);
1525 /* Soft abort: record the abort error status in the journal superblock,
1526 * but don't do any other IO. */
1527 static void __journal_abort_soft (journal_t *journal, int errno)
1529 if (journal->j_flags & JFS_ABORT)
1530 return;
1532 if (!journal->j_errno)
1533 journal->j_errno = errno;
1535 __journal_abort_hard(journal);
1537 if (errno)
1538 journal_update_superblock(journal, 1);
1542 * void journal_abort () - Shutdown the journal immediately.
1543 * @journal: the journal to shutdown.
1544 * @errno: an error number to record in the journal indicating
1545 * the reason for the shutdown.
1547 * Perform a complete, immediate shutdown of the ENTIRE
1548 * journal (not of a single transaction). This operation cannot be
1549 * undone without closing and reopening the journal.
1551 * The journal_abort function is intended to support higher level error
1552 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1553 * mode.
1555 * Journal abort has very specific semantics. Any existing dirty,
1556 * unjournaled buffers in the main filesystem will still be written to
1557 * disk by bdflush, but the journaling mechanism will be suspended
1558 * immediately and no further transaction commits will be honoured.
1560 * Any dirty, journaled buffers will be written back to disk without
1561 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1562 * filesystem, but we _do_ attempt to leave as much data as possible
1563 * behind for fsck to use for cleanup.
1565 * Any attempt to get a new transaction handle on a journal which is in
1566 * ABORT state will just result in an -EROFS error return. A
1567 * journal_stop on an existing handle will return -EIO if we have
1568 * entered abort state during the update.
1570 * Recursive transactions are not disturbed by journal abort until the
1571 * final journal_stop, which will receive the -EIO error.
1573 * Finally, the journal_abort call allows the caller to supply an errno
1574 * which will be recorded (if possible) in the journal superblock. This
1575 * allows a client to record failure conditions in the middle of a
1576 * transaction without having to complete the transaction to record the
1577 * failure to disk. ext3_error, for example, now uses this
1578 * functionality.
1580 * Errors which originate from within the journaling layer will NOT
1581 * supply an errno; a null errno implies that absolutely no further
1582 * writes are done to the journal (unless there are any already in
1583 * progress).
1587 void journal_abort(journal_t *journal, int errno)
1589 __journal_abort_soft(journal, errno);
1593 * int journal_errno () - returns the journal's error state.
1594 * @journal: journal to examine.
1596 * This is the errno numbet set with journal_abort(), the last
1597 * time the journal was mounted - if the journal was stopped
1598 * without calling abort this will be 0.
1600 * If the journal has been aborted on this mount time -EROFS will
1601 * be returned.
1603 int journal_errno(journal_t *journal)
1605 int err;
1607 spin_lock(&journal->j_state_lock);
1608 if (journal->j_flags & JFS_ABORT)
1609 err = -EROFS;
1610 else
1611 err = journal->j_errno;
1612 spin_unlock(&journal->j_state_lock);
1613 return err;
1617 * int journal_clear_err () - clears the journal's error state
1618 * @journal: journal to act on.
1620 * An error must be cleared or Acked to take a FS out of readonly
1621 * mode.
1623 int journal_clear_err(journal_t *journal)
1625 int err = 0;
1627 spin_lock(&journal->j_state_lock);
1628 if (journal->j_flags & JFS_ABORT)
1629 err = -EROFS;
1630 else
1631 journal->j_errno = 0;
1632 spin_unlock(&journal->j_state_lock);
1633 return err;
1637 * void journal_ack_err() - Ack journal err.
1638 * @journal: journal to act on.
1640 * An error must be cleared or Acked to take a FS out of readonly
1641 * mode.
1643 void journal_ack_err(journal_t *journal)
1645 spin_lock(&journal->j_state_lock);
1646 if (journal->j_errno)
1647 journal->j_flags |= JFS_ACK_ERR;
1648 spin_unlock(&journal->j_state_lock);
1651 int journal_blocks_per_page(struct inode *inode)
1653 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1657 * Journal_head storage management
1659 static struct kmem_cache *journal_head_cache;
1660 #ifdef CONFIG_JBD_DEBUG
1661 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1662 #endif
1664 static int journal_init_journal_head_cache(void)
1666 int retval;
1668 J_ASSERT(journal_head_cache == NULL);
1669 journal_head_cache = kmem_cache_create("journal_head",
1670 sizeof(struct journal_head),
1671 0, /* offset */
1672 SLAB_TEMPORARY, /* flags */
1673 NULL); /* ctor */
1674 retval = 0;
1675 if (!journal_head_cache) {
1676 retval = -ENOMEM;
1677 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1679 return retval;
1682 static void journal_destroy_journal_head_cache(void)
1684 if (journal_head_cache) {
1685 kmem_cache_destroy(journal_head_cache);
1686 journal_head_cache = NULL;
1691 * journal_head splicing and dicing
1693 static struct journal_head *journal_alloc_journal_head(void)
1695 struct journal_head *ret;
1696 static unsigned long last_warning;
1698 #ifdef CONFIG_JBD_DEBUG
1699 atomic_inc(&nr_journal_heads);
1700 #endif
1701 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1702 if (ret == NULL) {
1703 jbd_debug(1, "out of memory for journal_head\n");
1704 if (time_after(jiffies, last_warning + 5*HZ)) {
1705 printk(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1706 __func__);
1707 last_warning = jiffies;
1709 while (ret == NULL) {
1710 yield();
1711 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1714 return ret;
1717 static void journal_free_journal_head(struct journal_head *jh)
1719 #ifdef CONFIG_JBD_DEBUG
1720 atomic_dec(&nr_journal_heads);
1721 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1722 #endif
1723 kmem_cache_free(journal_head_cache, jh);
1727 * A journal_head is attached to a buffer_head whenever JBD has an
1728 * interest in the buffer.
1730 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1731 * is set. This bit is tested in core kernel code where we need to take
1732 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1733 * there.
1735 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1737 * When a buffer has its BH_JBD bit set it is immune from being released by
1738 * core kernel code, mainly via ->b_count.
1740 * A journal_head may be detached from its buffer_head when the journal_head's
1741 * b_transaction, b_cp_transaction and b_next_transaction pointers are NULL.
1742 * Various places in JBD call journal_remove_journal_head() to indicate that the
1743 * journal_head can be dropped if needed.
1745 * Various places in the kernel want to attach a journal_head to a buffer_head
1746 * _before_ attaching the journal_head to a transaction. To protect the
1747 * journal_head in this situation, journal_add_journal_head elevates the
1748 * journal_head's b_jcount refcount by one. The caller must call
1749 * journal_put_journal_head() to undo this.
1751 * So the typical usage would be:
1753 * (Attach a journal_head if needed. Increments b_jcount)
1754 * struct journal_head *jh = journal_add_journal_head(bh);
1755 * ...
1756 * jh->b_transaction = xxx;
1757 * journal_put_journal_head(jh);
1759 * Now, the journal_head's b_jcount is zero, but it is safe from being released
1760 * because it has a non-zero b_transaction.
1764 * Give a buffer_head a journal_head.
1766 * Doesn't need the journal lock.
1767 * May sleep.
1769 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1771 struct journal_head *jh;
1772 struct journal_head *new_jh = NULL;
1774 repeat:
1775 if (!buffer_jbd(bh)) {
1776 new_jh = journal_alloc_journal_head();
1777 memset(new_jh, 0, sizeof(*new_jh));
1780 jbd_lock_bh_journal_head(bh);
1781 if (buffer_jbd(bh)) {
1782 jh = bh2jh(bh);
1783 } else {
1784 J_ASSERT_BH(bh,
1785 (atomic_read(&bh->b_count) > 0) ||
1786 (bh->b_page && bh->b_page->mapping));
1788 if (!new_jh) {
1789 jbd_unlock_bh_journal_head(bh);
1790 goto repeat;
1793 jh = new_jh;
1794 new_jh = NULL; /* We consumed it */
1795 set_buffer_jbd(bh);
1796 bh->b_private = jh;
1797 jh->b_bh = bh;
1798 get_bh(bh);
1799 BUFFER_TRACE(bh, "added journal_head");
1801 jh->b_jcount++;
1802 jbd_unlock_bh_journal_head(bh);
1803 if (new_jh)
1804 journal_free_journal_head(new_jh);
1805 return bh->b_private;
1809 * Grab a ref against this buffer_head's journal_head. If it ended up not
1810 * having a journal_head, return NULL
1812 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1814 struct journal_head *jh = NULL;
1816 jbd_lock_bh_journal_head(bh);
1817 if (buffer_jbd(bh)) {
1818 jh = bh2jh(bh);
1819 jh->b_jcount++;
1821 jbd_unlock_bh_journal_head(bh);
1822 return jh;
1825 static void __journal_remove_journal_head(struct buffer_head *bh)
1827 struct journal_head *jh = bh2jh(bh);
1829 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1831 get_bh(bh);
1832 if (jh->b_jcount == 0) {
1833 if (jh->b_transaction == NULL &&
1834 jh->b_next_transaction == NULL &&
1835 jh->b_cp_transaction == NULL) {
1836 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1837 J_ASSERT_BH(bh, buffer_jbd(bh));
1838 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1839 BUFFER_TRACE(bh, "remove journal_head");
1840 if (jh->b_frozen_data) {
1841 printk(KERN_WARNING "%s: freeing "
1842 "b_frozen_data\n",
1843 __func__);
1844 jbd_free(jh->b_frozen_data, bh->b_size);
1846 if (jh->b_committed_data) {
1847 printk(KERN_WARNING "%s: freeing "
1848 "b_committed_data\n",
1849 __func__);
1850 jbd_free(jh->b_committed_data, bh->b_size);
1852 bh->b_private = NULL;
1853 jh->b_bh = NULL; /* debug, really */
1854 clear_buffer_jbd(bh);
1855 __brelse(bh);
1856 journal_free_journal_head(jh);
1857 } else {
1858 BUFFER_TRACE(bh, "journal_head was locked");
1864 * journal_remove_journal_head(): if the buffer isn't attached to a transaction
1865 * and has a zero b_jcount then remove and release its journal_head. If we did
1866 * see that the buffer is not used by any transaction we also "logically"
1867 * decrement ->b_count.
1869 * We in fact take an additional increment on ->b_count as a convenience,
1870 * because the caller usually wants to do additional things with the bh
1871 * after calling here.
1872 * The caller of journal_remove_journal_head() *must* run __brelse(bh) at some
1873 * time. Once the caller has run __brelse(), the buffer is eligible for
1874 * reaping by try_to_free_buffers().
1876 void journal_remove_journal_head(struct buffer_head *bh)
1878 jbd_lock_bh_journal_head(bh);
1879 __journal_remove_journal_head(bh);
1880 jbd_unlock_bh_journal_head(bh);
1884 * Drop a reference on the passed journal_head. If it fell to zero then try to
1885 * release the journal_head from the buffer_head.
1887 void journal_put_journal_head(struct journal_head *jh)
1889 struct buffer_head *bh = jh2bh(jh);
1891 jbd_lock_bh_journal_head(bh);
1892 J_ASSERT_JH(jh, jh->b_jcount > 0);
1893 --jh->b_jcount;
1894 if (!jh->b_jcount && !jh->b_transaction) {
1895 __journal_remove_journal_head(bh);
1896 __brelse(bh);
1898 jbd_unlock_bh_journal_head(bh);
1902 * debugfs tunables
1904 #ifdef CONFIG_JBD_DEBUG
1906 u8 journal_enable_debug __read_mostly;
1907 EXPORT_SYMBOL(journal_enable_debug);
1909 static struct dentry *jbd_debugfs_dir;
1910 static struct dentry *jbd_debug;
1912 static void __init jbd_create_debugfs_entry(void)
1914 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1915 if (jbd_debugfs_dir)
1916 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
1917 jbd_debugfs_dir,
1918 &journal_enable_debug);
1921 static void __exit jbd_remove_debugfs_entry(void)
1923 debugfs_remove(jbd_debug);
1924 debugfs_remove(jbd_debugfs_dir);
1927 #else
1929 static inline void jbd_create_debugfs_entry(void)
1933 static inline void jbd_remove_debugfs_entry(void)
1937 #endif
1939 struct kmem_cache *jbd_handle_cache;
1941 static int __init journal_init_handle_cache(void)
1943 jbd_handle_cache = kmem_cache_create("journal_handle",
1944 sizeof(handle_t),
1945 0, /* offset */
1946 SLAB_TEMPORARY, /* flags */
1947 NULL); /* ctor */
1948 if (jbd_handle_cache == NULL) {
1949 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1950 return -ENOMEM;
1952 return 0;
1955 static void journal_destroy_handle_cache(void)
1957 if (jbd_handle_cache)
1958 kmem_cache_destroy(jbd_handle_cache);
1962 * Module startup and shutdown
1965 static int __init journal_init_caches(void)
1967 int ret;
1969 ret = journal_init_revoke_caches();
1970 if (ret == 0)
1971 ret = journal_init_journal_head_cache();
1972 if (ret == 0)
1973 ret = journal_init_handle_cache();
1974 return ret;
1977 static void journal_destroy_caches(void)
1979 journal_destroy_revoke_caches();
1980 journal_destroy_journal_head_cache();
1981 journal_destroy_handle_cache();
1984 static int __init journal_init(void)
1986 int ret;
1988 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
1990 ret = journal_init_caches();
1991 if (ret != 0)
1992 journal_destroy_caches();
1993 jbd_create_debugfs_entry();
1994 return ret;
1997 static void __exit journal_exit(void)
1999 #ifdef CONFIG_JBD_DEBUG
2000 int n = atomic_read(&nr_journal_heads);
2001 if (n)
2002 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2003 #endif
2004 jbd_remove_debugfs_entry();
2005 journal_destroy_caches();
2008 MODULE_LICENSE("GPL");
2009 module_init(journal_init);
2010 module_exit(journal_exit);