staging:iio:Documentation gyro -> anglvel updates in attribute names
[zen-stable.git] / fs / jbd / journal.c
blob9fe061fb8779be389155a05672b267c8071623e7
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>
39 #include <linux/ratelimit.h>
41 #define CREATE_TRACE_POINTS
42 #include <trace/events/jbd.h>
44 #include <asm/uaccess.h>
45 #include <asm/page.h>
47 EXPORT_SYMBOL(journal_start);
48 EXPORT_SYMBOL(journal_restart);
49 EXPORT_SYMBOL(journal_extend);
50 EXPORT_SYMBOL(journal_stop);
51 EXPORT_SYMBOL(journal_lock_updates);
52 EXPORT_SYMBOL(journal_unlock_updates);
53 EXPORT_SYMBOL(journal_get_write_access);
54 EXPORT_SYMBOL(journal_get_create_access);
55 EXPORT_SYMBOL(journal_get_undo_access);
56 EXPORT_SYMBOL(journal_dirty_data);
57 EXPORT_SYMBOL(journal_dirty_metadata);
58 EXPORT_SYMBOL(journal_release_buffer);
59 EXPORT_SYMBOL(journal_forget);
60 #if 0
61 EXPORT_SYMBOL(journal_sync_buffer);
62 #endif
63 EXPORT_SYMBOL(journal_flush);
64 EXPORT_SYMBOL(journal_revoke);
66 EXPORT_SYMBOL(journal_init_dev);
67 EXPORT_SYMBOL(journal_init_inode);
68 EXPORT_SYMBOL(journal_update_format);
69 EXPORT_SYMBOL(journal_check_used_features);
70 EXPORT_SYMBOL(journal_check_available_features);
71 EXPORT_SYMBOL(journal_set_features);
72 EXPORT_SYMBOL(journal_create);
73 EXPORT_SYMBOL(journal_load);
74 EXPORT_SYMBOL(journal_destroy);
75 EXPORT_SYMBOL(journal_abort);
76 EXPORT_SYMBOL(journal_errno);
77 EXPORT_SYMBOL(journal_ack_err);
78 EXPORT_SYMBOL(journal_clear_err);
79 EXPORT_SYMBOL(log_wait_commit);
80 EXPORT_SYMBOL(log_start_commit);
81 EXPORT_SYMBOL(journal_start_commit);
82 EXPORT_SYMBOL(journal_force_commit_nested);
83 EXPORT_SYMBOL(journal_wipe);
84 EXPORT_SYMBOL(journal_blocks_per_page);
85 EXPORT_SYMBOL(journal_invalidatepage);
86 EXPORT_SYMBOL(journal_try_to_free_buffers);
87 EXPORT_SYMBOL(journal_force_commit);
89 static int journal_convert_superblock_v1(journal_t *, journal_superblock_t *);
90 static void __journal_abort_soft (journal_t *journal, int errno);
91 static const char *journal_dev_name(journal_t *journal, char *buffer);
94 * Helper function used to manage commit timeouts
97 static void commit_timeout(unsigned long __data)
99 struct task_struct * p = (struct task_struct *) __data;
101 wake_up_process(p);
105 * kjournald: The main thread function used to manage a logging device
106 * journal.
108 * This kernel thread is responsible for two things:
110 * 1) COMMIT: Every so often we need to commit the current state of the
111 * filesystem to disk. The journal thread is responsible for writing
112 * all of the metadata buffers to disk.
114 * 2) CHECKPOINT: We cannot reuse a used section of the log file until all
115 * of the data in that part of the log has been rewritten elsewhere on
116 * the disk. Flushing these old buffers to reclaim space in the log is
117 * known as checkpointing, and this thread is responsible for that job.
120 static int kjournald(void *arg)
122 journal_t *journal = arg;
123 transaction_t *transaction;
126 * Set up an interval timer which can be used to trigger a commit wakeup
127 * after the commit interval expires
129 setup_timer(&journal->j_commit_timer, commit_timeout,
130 (unsigned long)current);
132 /* Record that the journal thread is running */
133 journal->j_task = current;
134 wake_up(&journal->j_wait_done_commit);
136 printk(KERN_INFO "kjournald starting. Commit interval %ld seconds\n",
137 journal->j_commit_interval / HZ);
140 * And now, wait forever for commit wakeup events.
142 spin_lock(&journal->j_state_lock);
144 loop:
145 if (journal->j_flags & JFS_UNMOUNT)
146 goto end_loop;
148 jbd_debug(1, "commit_sequence=%d, commit_request=%d\n",
149 journal->j_commit_sequence, journal->j_commit_request);
151 if (journal->j_commit_sequence != journal->j_commit_request) {
152 jbd_debug(1, "OK, requests differ\n");
153 spin_unlock(&journal->j_state_lock);
154 del_timer_sync(&journal->j_commit_timer);
155 journal_commit_transaction(journal);
156 spin_lock(&journal->j_state_lock);
157 goto loop;
160 wake_up(&journal->j_wait_done_commit);
161 if (freezing(current)) {
163 * The simpler the better. Flushing journal isn't a
164 * good idea, because that depends on threads that may
165 * be already stopped.
167 jbd_debug(1, "Now suspending kjournald\n");
168 spin_unlock(&journal->j_state_lock);
169 refrigerator();
170 spin_lock(&journal->j_state_lock);
171 } else {
173 * We assume on resume that commits are already there,
174 * so we don't sleep
176 DEFINE_WAIT(wait);
177 int should_sleep = 1;
179 prepare_to_wait(&journal->j_wait_commit, &wait,
180 TASK_INTERRUPTIBLE);
181 if (journal->j_commit_sequence != journal->j_commit_request)
182 should_sleep = 0;
183 transaction = journal->j_running_transaction;
184 if (transaction && time_after_eq(jiffies,
185 transaction->t_expires))
186 should_sleep = 0;
187 if (journal->j_flags & JFS_UNMOUNT)
188 should_sleep = 0;
189 if (should_sleep) {
190 spin_unlock(&journal->j_state_lock);
191 schedule();
192 spin_lock(&journal->j_state_lock);
194 finish_wait(&journal->j_wait_commit, &wait);
197 jbd_debug(1, "kjournald wakes\n");
200 * Were we woken up by a commit wakeup event?
202 transaction = journal->j_running_transaction;
203 if (transaction && time_after_eq(jiffies, transaction->t_expires)) {
204 journal->j_commit_request = transaction->t_tid;
205 jbd_debug(1, "woke because of timeout\n");
207 goto loop;
209 end_loop:
210 spin_unlock(&journal->j_state_lock);
211 del_timer_sync(&journal->j_commit_timer);
212 journal->j_task = NULL;
213 wake_up(&journal->j_wait_done_commit);
214 jbd_debug(1, "Journal thread exiting.\n");
215 return 0;
218 static int journal_start_thread(journal_t *journal)
220 struct task_struct *t;
222 t = kthread_run(kjournald, journal, "kjournald");
223 if (IS_ERR(t))
224 return PTR_ERR(t);
226 wait_event(journal->j_wait_done_commit, journal->j_task != NULL);
227 return 0;
230 static void journal_kill_thread(journal_t *journal)
232 spin_lock(&journal->j_state_lock);
233 journal->j_flags |= JFS_UNMOUNT;
235 while (journal->j_task) {
236 wake_up(&journal->j_wait_commit);
237 spin_unlock(&journal->j_state_lock);
238 wait_event(journal->j_wait_done_commit,
239 journal->j_task == NULL);
240 spin_lock(&journal->j_state_lock);
242 spin_unlock(&journal->j_state_lock);
246 * journal_write_metadata_buffer: write a metadata buffer to the journal.
248 * Writes a metadata buffer to a given disk block. The actual IO is not
249 * performed but a new buffer_head is constructed which labels the data
250 * to be written with the correct destination disk block.
252 * Any magic-number escaping which needs to be done will cause a
253 * copy-out here. If the buffer happens to start with the
254 * JFS_MAGIC_NUMBER, then we can't write it to the log directly: the
255 * magic number is only written to the log for descripter blocks. In
256 * this case, we copy the data and replace the first word with 0, and we
257 * return a result code which indicates that this buffer needs to be
258 * marked as an escaped buffer in the corresponding log descriptor
259 * block. The missing word can then be restored when the block is read
260 * during recovery.
262 * If the source buffer has already been modified by a new transaction
263 * since we took the last commit snapshot, we use the frozen copy of
264 * that data for IO. If we end up using the existing buffer_head's data
265 * for the write, then we *have* to lock the buffer to prevent anyone
266 * else from using and possibly modifying it while the IO is in
267 * progress.
269 * The function returns a pointer to the buffer_heads to be used for IO.
271 * We assume that the journal has already been locked in this function.
273 * Return value:
274 * <0: Error
275 * >=0: Finished OK
277 * On success:
278 * Bit 0 set == escape performed on the data
279 * Bit 1 set == buffer copy-out performed (kfree the data after IO)
282 int journal_write_metadata_buffer(transaction_t *transaction,
283 struct journal_head *jh_in,
284 struct journal_head **jh_out,
285 unsigned int blocknr)
287 int need_copy_out = 0;
288 int done_copy_out = 0;
289 int do_escape = 0;
290 char *mapped_data;
291 struct buffer_head *new_bh;
292 struct journal_head *new_jh;
293 struct page *new_page;
294 unsigned int new_offset;
295 struct buffer_head *bh_in = jh2bh(jh_in);
296 journal_t *journal = transaction->t_journal;
299 * The buffer really shouldn't be locked: only the current committing
300 * transaction is allowed to write it, so nobody else is allowed
301 * to do any IO.
303 * akpm: except if we're journalling data, and write() output is
304 * also part of a shared mapping, and another thread has
305 * decided to launch a writepage() against this buffer.
307 J_ASSERT_BH(bh_in, buffer_jbddirty(bh_in));
309 new_bh = alloc_buffer_head(GFP_NOFS|__GFP_NOFAIL);
310 /* keep subsequent assertions sane */
311 new_bh->b_state = 0;
312 init_buffer(new_bh, NULL, NULL);
313 atomic_set(&new_bh->b_count, 1);
314 new_jh = journal_add_journal_head(new_bh); /* This sleeps */
317 * If a new transaction has already done a buffer copy-out, then
318 * we use that version of the data for the commit.
320 jbd_lock_bh_state(bh_in);
321 repeat:
322 if (jh_in->b_frozen_data) {
323 done_copy_out = 1;
324 new_page = virt_to_page(jh_in->b_frozen_data);
325 new_offset = offset_in_page(jh_in->b_frozen_data);
326 } else {
327 new_page = jh2bh(jh_in)->b_page;
328 new_offset = offset_in_page(jh2bh(jh_in)->b_data);
331 mapped_data = kmap_atomic(new_page, KM_USER0);
333 * Check for escaping
335 if (*((__be32 *)(mapped_data + new_offset)) ==
336 cpu_to_be32(JFS_MAGIC_NUMBER)) {
337 need_copy_out = 1;
338 do_escape = 1;
340 kunmap_atomic(mapped_data, KM_USER0);
343 * Do we need to do a data copy?
345 if (need_copy_out && !done_copy_out) {
346 char *tmp;
348 jbd_unlock_bh_state(bh_in);
349 tmp = jbd_alloc(bh_in->b_size, GFP_NOFS);
350 jbd_lock_bh_state(bh_in);
351 if (jh_in->b_frozen_data) {
352 jbd_free(tmp, bh_in->b_size);
353 goto repeat;
356 jh_in->b_frozen_data = tmp;
357 mapped_data = kmap_atomic(new_page, KM_USER0);
358 memcpy(tmp, mapped_data + new_offset, jh2bh(jh_in)->b_size);
359 kunmap_atomic(mapped_data, KM_USER0);
361 new_page = virt_to_page(tmp);
362 new_offset = offset_in_page(tmp);
363 done_copy_out = 1;
367 * Did we need to do an escaping? Now we've done all the
368 * copying, we can finally do so.
370 if (do_escape) {
371 mapped_data = kmap_atomic(new_page, KM_USER0);
372 *((unsigned int *)(mapped_data + new_offset)) = 0;
373 kunmap_atomic(mapped_data, KM_USER0);
376 set_bh_page(new_bh, new_page, new_offset);
377 new_jh->b_transaction = NULL;
378 new_bh->b_size = jh2bh(jh_in)->b_size;
379 new_bh->b_bdev = transaction->t_journal->j_dev;
380 new_bh->b_blocknr = blocknr;
381 set_buffer_mapped(new_bh);
382 set_buffer_dirty(new_bh);
384 *jh_out = new_jh;
387 * The to-be-written buffer needs to get moved to the io queue,
388 * and the original buffer whose contents we are shadowing or
389 * copying is moved to the transaction's shadow queue.
391 JBUFFER_TRACE(jh_in, "file as BJ_Shadow");
392 spin_lock(&journal->j_list_lock);
393 __journal_file_buffer(jh_in, transaction, BJ_Shadow);
394 spin_unlock(&journal->j_list_lock);
395 jbd_unlock_bh_state(bh_in);
397 JBUFFER_TRACE(new_jh, "file as BJ_IO");
398 journal_file_buffer(new_jh, transaction, BJ_IO);
400 return do_escape | (done_copy_out << 1);
404 * Allocation code for the journal file. Manage the space left in the
405 * journal, so that we can begin checkpointing when appropriate.
409 * __log_space_left: Return the number of free blocks left in the journal.
411 * Called with the journal already locked.
413 * Called under j_state_lock
416 int __log_space_left(journal_t *journal)
418 int left = journal->j_free;
420 assert_spin_locked(&journal->j_state_lock);
423 * Be pessimistic here about the number of those free blocks which
424 * might be required for log descriptor control blocks.
427 #define MIN_LOG_RESERVED_BLOCKS 32 /* Allow for rounding errors */
429 left -= MIN_LOG_RESERVED_BLOCKS;
431 if (left <= 0)
432 return 0;
433 left -= (left >> 3);
434 return left;
438 * Called under j_state_lock. Returns true if a transaction commit was started.
440 int __log_start_commit(journal_t *journal, tid_t target)
443 * The only transaction we can possibly wait upon is the
444 * currently running transaction (if it exists). Otherwise,
445 * the target tid must be an old one.
447 if (journal->j_running_transaction &&
448 journal->j_running_transaction->t_tid == target) {
450 * We want a new commit: OK, mark the request and wakeup the
451 * commit thread. We do _not_ do the commit ourselves.
454 journal->j_commit_request = target;
455 jbd_debug(1, "JBD: requesting commit %d/%d\n",
456 journal->j_commit_request,
457 journal->j_commit_sequence);
458 wake_up(&journal->j_wait_commit);
459 return 1;
460 } else if (!tid_geq(journal->j_commit_request, target))
461 /* This should never happen, but if it does, preserve
462 the evidence before kjournald goes into a loop and
463 increments j_commit_sequence beyond all recognition. */
464 WARN_ONCE(1, "jbd: bad log_start_commit: %u %u %u %u\n",
465 journal->j_commit_request, journal->j_commit_sequence,
466 target, journal->j_running_transaction ?
467 journal->j_running_transaction->t_tid : 0);
468 return 0;
471 int log_start_commit(journal_t *journal, tid_t tid)
473 int ret;
475 spin_lock(&journal->j_state_lock);
476 ret = __log_start_commit(journal, tid);
477 spin_unlock(&journal->j_state_lock);
478 return ret;
482 * Force and wait upon a commit if the calling process is not within
483 * transaction. This is used for forcing out undo-protected data which contains
484 * bitmaps, when the fs is running out of space.
486 * We can only force the running transaction if we don't have an active handle;
487 * otherwise, we will deadlock.
489 * Returns true if a transaction was started.
491 int journal_force_commit_nested(journal_t *journal)
493 transaction_t *transaction = NULL;
494 tid_t tid;
496 spin_lock(&journal->j_state_lock);
497 if (journal->j_running_transaction && !current->journal_info) {
498 transaction = journal->j_running_transaction;
499 __log_start_commit(journal, transaction->t_tid);
500 } else if (journal->j_committing_transaction)
501 transaction = journal->j_committing_transaction;
503 if (!transaction) {
504 spin_unlock(&journal->j_state_lock);
505 return 0; /* Nothing to retry */
508 tid = transaction->t_tid;
509 spin_unlock(&journal->j_state_lock);
510 log_wait_commit(journal, tid);
511 return 1;
515 * Start a commit of the current running transaction (if any). Returns true
516 * if a transaction is going to be committed (or is currently already
517 * committing), and fills its tid in at *ptid
519 int journal_start_commit(journal_t *journal, tid_t *ptid)
521 int ret = 0;
523 spin_lock(&journal->j_state_lock);
524 if (journal->j_running_transaction) {
525 tid_t tid = journal->j_running_transaction->t_tid;
527 __log_start_commit(journal, tid);
528 /* There's a running transaction and we've just made sure
529 * it's commit has been scheduled. */
530 if (ptid)
531 *ptid = tid;
532 ret = 1;
533 } else if (journal->j_committing_transaction) {
535 * If ext3_write_super() recently started a commit, then we
536 * have to wait for completion of that transaction
538 if (ptid)
539 *ptid = journal->j_committing_transaction->t_tid;
540 ret = 1;
542 spin_unlock(&journal->j_state_lock);
543 return ret;
547 * Wait for a specified commit to complete.
548 * The caller may not hold the journal lock.
550 int log_wait_commit(journal_t *journal, tid_t tid)
552 int err = 0;
554 #ifdef CONFIG_JBD_DEBUG
555 spin_lock(&journal->j_state_lock);
556 if (!tid_geq(journal->j_commit_request, tid)) {
557 printk(KERN_EMERG
558 "%s: error: j_commit_request=%d, tid=%d\n",
559 __func__, journal->j_commit_request, tid);
561 spin_unlock(&journal->j_state_lock);
562 #endif
563 spin_lock(&journal->j_state_lock);
564 while (tid_gt(tid, journal->j_commit_sequence)) {
565 jbd_debug(1, "JBD: want %d, j_commit_sequence=%d\n",
566 tid, journal->j_commit_sequence);
567 wake_up(&journal->j_wait_commit);
568 spin_unlock(&journal->j_state_lock);
569 wait_event(journal->j_wait_done_commit,
570 !tid_gt(tid, journal->j_commit_sequence));
571 spin_lock(&journal->j_state_lock);
573 spin_unlock(&journal->j_state_lock);
575 if (unlikely(is_journal_aborted(journal))) {
576 printk(KERN_EMERG "journal commit I/O error\n");
577 err = -EIO;
579 return err;
583 * Return 1 if a given transaction has not yet sent barrier request
584 * connected with a transaction commit. If 0 is returned, transaction
585 * may or may not have sent the barrier. Used to avoid sending barrier
586 * twice in common cases.
588 int journal_trans_will_send_data_barrier(journal_t *journal, tid_t tid)
590 int ret = 0;
591 transaction_t *commit_trans;
593 if (!(journal->j_flags & JFS_BARRIER))
594 return 0;
595 spin_lock(&journal->j_state_lock);
596 /* Transaction already committed? */
597 if (tid_geq(journal->j_commit_sequence, tid))
598 goto out;
600 * Transaction is being committed and we already proceeded to
601 * writing commit record?
603 commit_trans = journal->j_committing_transaction;
604 if (commit_trans && commit_trans->t_tid == tid &&
605 commit_trans->t_state >= T_COMMIT_RECORD)
606 goto out;
607 ret = 1;
608 out:
609 spin_unlock(&journal->j_state_lock);
610 return ret;
612 EXPORT_SYMBOL(journal_trans_will_send_data_barrier);
615 * Log buffer allocation routines:
618 int journal_next_log_block(journal_t *journal, unsigned int *retp)
620 unsigned int blocknr;
622 spin_lock(&journal->j_state_lock);
623 J_ASSERT(journal->j_free > 1);
625 blocknr = journal->j_head;
626 journal->j_head++;
627 journal->j_free--;
628 if (journal->j_head == journal->j_last)
629 journal->j_head = journal->j_first;
630 spin_unlock(&journal->j_state_lock);
631 return journal_bmap(journal, blocknr, retp);
635 * Conversion of logical to physical block numbers for the journal
637 * On external journals the journal blocks are identity-mapped, so
638 * this is a no-op. If needed, we can use j_blk_offset - everything is
639 * ready.
641 int journal_bmap(journal_t *journal, unsigned int blocknr,
642 unsigned int *retp)
644 int err = 0;
645 unsigned int ret;
647 if (journal->j_inode) {
648 ret = bmap(journal->j_inode, blocknr);
649 if (ret)
650 *retp = ret;
651 else {
652 char b[BDEVNAME_SIZE];
654 printk(KERN_ALERT "%s: journal block not found "
655 "at offset %u on %s\n",
656 __func__,
657 blocknr,
658 bdevname(journal->j_dev, b));
659 err = -EIO;
660 __journal_abort_soft(journal, err);
662 } else {
663 *retp = blocknr; /* +journal->j_blk_offset */
665 return err;
669 * We play buffer_head aliasing tricks to write data/metadata blocks to
670 * the journal without copying their contents, but for journal
671 * descriptor blocks we do need to generate bona fide buffers.
673 * After the caller of journal_get_descriptor_buffer() has finished modifying
674 * the buffer's contents they really should run flush_dcache_page(bh->b_page).
675 * But we don't bother doing that, so there will be coherency problems with
676 * mmaps of blockdevs which hold live JBD-controlled filesystems.
678 struct journal_head *journal_get_descriptor_buffer(journal_t *journal)
680 struct buffer_head *bh;
681 unsigned int blocknr;
682 int err;
684 err = journal_next_log_block(journal, &blocknr);
686 if (err)
687 return NULL;
689 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
690 if (!bh)
691 return NULL;
692 lock_buffer(bh);
693 memset(bh->b_data, 0, journal->j_blocksize);
694 set_buffer_uptodate(bh);
695 unlock_buffer(bh);
696 BUFFER_TRACE(bh, "return this buffer");
697 return journal_add_journal_head(bh);
701 * Management for journal control blocks: functions to create and
702 * destroy journal_t structures, and to initialise and read existing
703 * journal blocks from disk. */
705 /* First: create and setup a journal_t object in memory. We initialise
706 * very few fields yet: that has to wait until we have created the
707 * journal structures from from scratch, or loaded them from disk. */
709 static journal_t * journal_init_common (void)
711 journal_t *journal;
712 int err;
714 journal = kzalloc(sizeof(*journal), GFP_KERNEL);
715 if (!journal)
716 goto fail;
718 init_waitqueue_head(&journal->j_wait_transaction_locked);
719 init_waitqueue_head(&journal->j_wait_logspace);
720 init_waitqueue_head(&journal->j_wait_done_commit);
721 init_waitqueue_head(&journal->j_wait_checkpoint);
722 init_waitqueue_head(&journal->j_wait_commit);
723 init_waitqueue_head(&journal->j_wait_updates);
724 mutex_init(&journal->j_barrier);
725 mutex_init(&journal->j_checkpoint_mutex);
726 spin_lock_init(&journal->j_revoke_lock);
727 spin_lock_init(&journal->j_list_lock);
728 spin_lock_init(&journal->j_state_lock);
730 journal->j_commit_interval = (HZ * JBD_DEFAULT_MAX_COMMIT_AGE);
732 /* The journal is marked for error until we succeed with recovery! */
733 journal->j_flags = JFS_ABORT;
735 /* Set up a default-sized revoke table for the new mount. */
736 err = journal_init_revoke(journal, JOURNAL_REVOKE_DEFAULT_HASH);
737 if (err) {
738 kfree(journal);
739 goto fail;
741 return journal;
742 fail:
743 return NULL;
746 /* journal_init_dev and journal_init_inode:
748 * Create a journal structure assigned some fixed set of disk blocks to
749 * the journal. We don't actually touch those disk blocks yet, but we
750 * need to set up all of the mapping information to tell the journaling
751 * system where the journal blocks are.
756 * journal_t * journal_init_dev() - creates and initialises a journal structure
757 * @bdev: Block device on which to create the journal
758 * @fs_dev: Device which hold journalled filesystem for this journal.
759 * @start: Block nr Start of journal.
760 * @len: Length of the journal in blocks.
761 * @blocksize: blocksize of journalling device
763 * Returns: a newly created journal_t *
765 * journal_init_dev creates a journal which maps a fixed contiguous
766 * range of blocks on an arbitrary block device.
769 journal_t * journal_init_dev(struct block_device *bdev,
770 struct block_device *fs_dev,
771 int start, int len, int blocksize)
773 journal_t *journal = journal_init_common();
774 struct buffer_head *bh;
775 int n;
777 if (!journal)
778 return NULL;
780 /* journal descriptor can store up to n blocks -bzzz */
781 journal->j_blocksize = blocksize;
782 n = journal->j_blocksize / sizeof(journal_block_tag_t);
783 journal->j_wbufsize = n;
784 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
785 if (!journal->j_wbuf) {
786 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
787 __func__);
788 goto out_err;
790 journal->j_dev = bdev;
791 journal->j_fs_dev = fs_dev;
792 journal->j_blk_offset = start;
793 journal->j_maxlen = len;
795 bh = __getblk(journal->j_dev, start, journal->j_blocksize);
796 if (!bh) {
797 printk(KERN_ERR
798 "%s: Cannot get buffer for journal superblock\n",
799 __func__);
800 goto out_err;
802 journal->j_sb_buffer = bh;
803 journal->j_superblock = (journal_superblock_t *)bh->b_data;
805 return journal;
806 out_err:
807 kfree(journal->j_wbuf);
808 kfree(journal);
809 return NULL;
813 * journal_t * journal_init_inode () - creates a journal which maps to a inode.
814 * @inode: An inode to create the journal in
816 * journal_init_inode creates a journal which maps an on-disk inode as
817 * the journal. The inode must exist already, must support bmap() and
818 * must have all data blocks preallocated.
820 journal_t * journal_init_inode (struct inode *inode)
822 struct buffer_head *bh;
823 journal_t *journal = journal_init_common();
824 int err;
825 int n;
826 unsigned int blocknr;
828 if (!journal)
829 return NULL;
831 journal->j_dev = journal->j_fs_dev = inode->i_sb->s_bdev;
832 journal->j_inode = inode;
833 jbd_debug(1,
834 "journal %p: inode %s/%ld, size %Ld, bits %d, blksize %ld\n",
835 journal, inode->i_sb->s_id, inode->i_ino,
836 (long long) inode->i_size,
837 inode->i_sb->s_blocksize_bits, inode->i_sb->s_blocksize);
839 journal->j_maxlen = inode->i_size >> inode->i_sb->s_blocksize_bits;
840 journal->j_blocksize = inode->i_sb->s_blocksize;
842 /* journal descriptor can store up to n blocks -bzzz */
843 n = journal->j_blocksize / sizeof(journal_block_tag_t);
844 journal->j_wbufsize = n;
845 journal->j_wbuf = kmalloc(n * sizeof(struct buffer_head*), GFP_KERNEL);
846 if (!journal->j_wbuf) {
847 printk(KERN_ERR "%s: Can't allocate bhs for commit thread\n",
848 __func__);
849 goto out_err;
852 err = journal_bmap(journal, 0, &blocknr);
853 /* If that failed, give up */
854 if (err) {
855 printk(KERN_ERR "%s: Cannot locate journal superblock\n",
856 __func__);
857 goto out_err;
860 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
861 if (!bh) {
862 printk(KERN_ERR
863 "%s: Cannot get buffer for journal superblock\n",
864 __func__);
865 goto out_err;
867 journal->j_sb_buffer = bh;
868 journal->j_superblock = (journal_superblock_t *)bh->b_data;
870 return journal;
871 out_err:
872 kfree(journal->j_wbuf);
873 kfree(journal);
874 return NULL;
878 * If the journal init or create aborts, we need to mark the journal
879 * superblock as being NULL to prevent the journal destroy from writing
880 * back a bogus superblock.
882 static void journal_fail_superblock (journal_t *journal)
884 struct buffer_head *bh = journal->j_sb_buffer;
885 brelse(bh);
886 journal->j_sb_buffer = NULL;
890 * Given a journal_t structure, initialise the various fields for
891 * startup of a new journaling session. We use this both when creating
892 * a journal, and after recovering an old journal to reset it for
893 * subsequent use.
896 static int journal_reset(journal_t *journal)
898 journal_superblock_t *sb = journal->j_superblock;
899 unsigned int first, last;
901 first = be32_to_cpu(sb->s_first);
902 last = be32_to_cpu(sb->s_maxlen);
903 if (first + JFS_MIN_JOURNAL_BLOCKS > last + 1) {
904 printk(KERN_ERR "JBD: Journal too short (blocks %u-%u).\n",
905 first, last);
906 journal_fail_superblock(journal);
907 return -EINVAL;
910 journal->j_first = first;
911 journal->j_last = last;
913 journal->j_head = first;
914 journal->j_tail = first;
915 journal->j_free = last - first;
917 journal->j_tail_sequence = journal->j_transaction_sequence;
918 journal->j_commit_sequence = journal->j_transaction_sequence - 1;
919 journal->j_commit_request = journal->j_commit_sequence;
921 journal->j_max_transaction_buffers = journal->j_maxlen / 4;
923 /* Add the dynamic fields and write it to disk. */
924 journal_update_superblock(journal, 1);
925 return journal_start_thread(journal);
929 * int journal_create() - Initialise the new journal file
930 * @journal: Journal to create. This structure must have been initialised
932 * Given a journal_t structure which tells us which disk blocks we can
933 * use, create a new journal superblock and initialise all of the
934 * journal fields from scratch.
936 int journal_create(journal_t *journal)
938 unsigned int blocknr;
939 struct buffer_head *bh;
940 journal_superblock_t *sb;
941 int i, err;
943 if (journal->j_maxlen < JFS_MIN_JOURNAL_BLOCKS) {
944 printk (KERN_ERR "Journal length (%d blocks) too short.\n",
945 journal->j_maxlen);
946 journal_fail_superblock(journal);
947 return -EINVAL;
950 if (journal->j_inode == NULL) {
952 * We don't know what block to start at!
954 printk(KERN_EMERG
955 "%s: creation of journal on external device!\n",
956 __func__);
957 BUG();
960 /* Zero out the entire journal on disk. We cannot afford to
961 have any blocks on disk beginning with JFS_MAGIC_NUMBER. */
962 jbd_debug(1, "JBD: Zeroing out journal blocks...\n");
963 for (i = 0; i < journal->j_maxlen; i++) {
964 err = journal_bmap(journal, i, &blocknr);
965 if (err)
966 return err;
967 bh = __getblk(journal->j_dev, blocknr, journal->j_blocksize);
968 if (unlikely(!bh))
969 return -ENOMEM;
970 lock_buffer(bh);
971 memset (bh->b_data, 0, journal->j_blocksize);
972 BUFFER_TRACE(bh, "marking dirty");
973 mark_buffer_dirty(bh);
974 BUFFER_TRACE(bh, "marking uptodate");
975 set_buffer_uptodate(bh);
976 unlock_buffer(bh);
977 __brelse(bh);
980 sync_blockdev(journal->j_dev);
981 jbd_debug(1, "JBD: journal cleared.\n");
983 /* OK, fill in the initial static fields in the new superblock */
984 sb = journal->j_superblock;
986 sb->s_header.h_magic = cpu_to_be32(JFS_MAGIC_NUMBER);
987 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
989 sb->s_blocksize = cpu_to_be32(journal->j_blocksize);
990 sb->s_maxlen = cpu_to_be32(journal->j_maxlen);
991 sb->s_first = cpu_to_be32(1);
993 journal->j_transaction_sequence = 1;
995 journal->j_flags &= ~JFS_ABORT;
996 journal->j_format_version = 2;
998 return journal_reset(journal);
1002 * void journal_update_superblock() - Update journal sb on disk.
1003 * @journal: The journal to update.
1004 * @wait: Set to '0' if you don't want to wait for IO completion.
1006 * Update a journal's dynamic superblock fields and write it to disk,
1007 * optionally waiting for the IO to complete.
1009 void journal_update_superblock(journal_t *journal, int wait)
1011 journal_superblock_t *sb = journal->j_superblock;
1012 struct buffer_head *bh = journal->j_sb_buffer;
1015 * As a special case, if the on-disk copy is already marked as needing
1016 * no recovery (s_start == 0) and there are no outstanding transactions
1017 * in the filesystem, then we can safely defer the superblock update
1018 * until the next commit by setting JFS_FLUSHED. This avoids
1019 * attempting a write to a potential-readonly device.
1021 if (sb->s_start == 0 && journal->j_tail_sequence ==
1022 journal->j_transaction_sequence) {
1023 jbd_debug(1,"JBD: Skipping superblock update on recovered sb "
1024 "(start %u, seq %d, errno %d)\n",
1025 journal->j_tail, journal->j_tail_sequence,
1026 journal->j_errno);
1027 goto out;
1030 if (buffer_write_io_error(bh)) {
1031 char b[BDEVNAME_SIZE];
1033 * Oh, dear. A previous attempt to write the journal
1034 * superblock failed. This could happen because the
1035 * USB device was yanked out. Or it could happen to
1036 * be a transient write error and maybe the block will
1037 * be remapped. Nothing we can do but to retry the
1038 * write and hope for the best.
1040 printk(KERN_ERR "JBD: previous I/O error detected "
1041 "for journal superblock update for %s.\n",
1042 journal_dev_name(journal, b));
1043 clear_buffer_write_io_error(bh);
1044 set_buffer_uptodate(bh);
1047 spin_lock(&journal->j_state_lock);
1048 jbd_debug(1,"JBD: updating superblock (start %u, seq %d, errno %d)\n",
1049 journal->j_tail, journal->j_tail_sequence, journal->j_errno);
1051 sb->s_sequence = cpu_to_be32(journal->j_tail_sequence);
1052 sb->s_start = cpu_to_be32(journal->j_tail);
1053 sb->s_errno = cpu_to_be32(journal->j_errno);
1054 spin_unlock(&journal->j_state_lock);
1056 BUFFER_TRACE(bh, "marking dirty");
1057 mark_buffer_dirty(bh);
1058 if (wait) {
1059 sync_dirty_buffer(bh);
1060 if (buffer_write_io_error(bh)) {
1061 char b[BDEVNAME_SIZE];
1062 printk(KERN_ERR "JBD: I/O error detected "
1063 "when updating journal superblock for %s.\n",
1064 journal_dev_name(journal, b));
1065 clear_buffer_write_io_error(bh);
1066 set_buffer_uptodate(bh);
1068 } else
1069 write_dirty_buffer(bh, WRITE);
1071 trace_jbd_update_superblock_end(journal, wait);
1072 out:
1073 /* If we have just flushed the log (by marking s_start==0), then
1074 * any future commit will have to be careful to update the
1075 * superblock again to re-record the true start of the log. */
1077 spin_lock(&journal->j_state_lock);
1078 if (sb->s_start)
1079 journal->j_flags &= ~JFS_FLUSHED;
1080 else
1081 journal->j_flags |= JFS_FLUSHED;
1082 spin_unlock(&journal->j_state_lock);
1086 * Read the superblock for a given journal, performing initial
1087 * validation of the format.
1090 static int journal_get_superblock(journal_t *journal)
1092 struct buffer_head *bh;
1093 journal_superblock_t *sb;
1094 int err = -EIO;
1096 bh = journal->j_sb_buffer;
1098 J_ASSERT(bh != NULL);
1099 if (!buffer_uptodate(bh)) {
1100 ll_rw_block(READ, 1, &bh);
1101 wait_on_buffer(bh);
1102 if (!buffer_uptodate(bh)) {
1103 printk (KERN_ERR
1104 "JBD: IO error reading journal superblock\n");
1105 goto out;
1109 sb = journal->j_superblock;
1111 err = -EINVAL;
1113 if (sb->s_header.h_magic != cpu_to_be32(JFS_MAGIC_NUMBER) ||
1114 sb->s_blocksize != cpu_to_be32(journal->j_blocksize)) {
1115 printk(KERN_WARNING "JBD: no valid journal superblock found\n");
1116 goto out;
1119 switch(be32_to_cpu(sb->s_header.h_blocktype)) {
1120 case JFS_SUPERBLOCK_V1:
1121 journal->j_format_version = 1;
1122 break;
1123 case JFS_SUPERBLOCK_V2:
1124 journal->j_format_version = 2;
1125 break;
1126 default:
1127 printk(KERN_WARNING "JBD: unrecognised superblock format ID\n");
1128 goto out;
1131 if (be32_to_cpu(sb->s_maxlen) < journal->j_maxlen)
1132 journal->j_maxlen = be32_to_cpu(sb->s_maxlen);
1133 else if (be32_to_cpu(sb->s_maxlen) > journal->j_maxlen) {
1134 printk (KERN_WARNING "JBD: journal file too short\n");
1135 goto out;
1138 return 0;
1140 out:
1141 journal_fail_superblock(journal);
1142 return err;
1146 * Load the on-disk journal superblock and read the key fields into the
1147 * journal_t.
1150 static int load_superblock(journal_t *journal)
1152 int err;
1153 journal_superblock_t *sb;
1155 err = journal_get_superblock(journal);
1156 if (err)
1157 return err;
1159 sb = journal->j_superblock;
1161 journal->j_tail_sequence = be32_to_cpu(sb->s_sequence);
1162 journal->j_tail = be32_to_cpu(sb->s_start);
1163 journal->j_first = be32_to_cpu(sb->s_first);
1164 journal->j_last = be32_to_cpu(sb->s_maxlen);
1165 journal->j_errno = be32_to_cpu(sb->s_errno);
1167 return 0;
1172 * int journal_load() - Read journal from disk.
1173 * @journal: Journal to act on.
1175 * Given a journal_t structure which tells us which disk blocks contain
1176 * a journal, read the journal from disk to initialise the in-memory
1177 * structures.
1179 int journal_load(journal_t *journal)
1181 int err;
1182 journal_superblock_t *sb;
1184 err = load_superblock(journal);
1185 if (err)
1186 return err;
1188 sb = journal->j_superblock;
1189 /* If this is a V2 superblock, then we have to check the
1190 * features flags on it. */
1192 if (journal->j_format_version >= 2) {
1193 if ((sb->s_feature_ro_compat &
1194 ~cpu_to_be32(JFS_KNOWN_ROCOMPAT_FEATURES)) ||
1195 (sb->s_feature_incompat &
1196 ~cpu_to_be32(JFS_KNOWN_INCOMPAT_FEATURES))) {
1197 printk (KERN_WARNING
1198 "JBD: Unrecognised features on journal\n");
1199 return -EINVAL;
1203 /* Let the recovery code check whether it needs to recover any
1204 * data from the journal. */
1205 if (journal_recover(journal))
1206 goto recovery_error;
1208 /* OK, we've finished with the dynamic journal bits:
1209 * reinitialise the dynamic contents of the superblock in memory
1210 * and reset them on disk. */
1211 if (journal_reset(journal))
1212 goto recovery_error;
1214 journal->j_flags &= ~JFS_ABORT;
1215 journal->j_flags |= JFS_LOADED;
1216 return 0;
1218 recovery_error:
1219 printk (KERN_WARNING "JBD: recovery failed\n");
1220 return -EIO;
1224 * void journal_destroy() - Release a journal_t structure.
1225 * @journal: Journal to act on.
1227 * Release a journal_t structure once it is no longer in use by the
1228 * journaled object.
1229 * Return <0 if we couldn't clean up the journal.
1231 int journal_destroy(journal_t *journal)
1233 int err = 0;
1236 /* Wait for the commit thread to wake up and die. */
1237 journal_kill_thread(journal);
1239 /* Force a final log commit */
1240 if (journal->j_running_transaction)
1241 journal_commit_transaction(journal);
1243 /* Force any old transactions to disk */
1245 /* Totally anal locking here... */
1246 spin_lock(&journal->j_list_lock);
1247 while (journal->j_checkpoint_transactions != NULL) {
1248 spin_unlock(&journal->j_list_lock);
1249 log_do_checkpoint(journal);
1250 spin_lock(&journal->j_list_lock);
1253 J_ASSERT(journal->j_running_transaction == NULL);
1254 J_ASSERT(journal->j_committing_transaction == NULL);
1255 J_ASSERT(journal->j_checkpoint_transactions == NULL);
1256 spin_unlock(&journal->j_list_lock);
1258 if (journal->j_sb_buffer) {
1259 if (!is_journal_aborted(journal)) {
1260 /* We can now mark the journal as empty. */
1261 journal->j_tail = 0;
1262 journal->j_tail_sequence =
1263 ++journal->j_transaction_sequence;
1264 journal_update_superblock(journal, 1);
1265 } else {
1266 err = -EIO;
1268 brelse(journal->j_sb_buffer);
1271 if (journal->j_inode)
1272 iput(journal->j_inode);
1273 if (journal->j_revoke)
1274 journal_destroy_revoke(journal);
1275 kfree(journal->j_wbuf);
1276 kfree(journal);
1278 return err;
1283 *int journal_check_used_features () - Check if features specified are used.
1284 * @journal: Journal to check.
1285 * @compat: bitmask of compatible features
1286 * @ro: bitmask of features that force read-only mount
1287 * @incompat: bitmask of incompatible features
1289 * Check whether the journal uses all of a given set of
1290 * features. Return true (non-zero) if it does.
1293 int journal_check_used_features (journal_t *journal, unsigned long compat,
1294 unsigned long ro, unsigned long incompat)
1296 journal_superblock_t *sb;
1298 if (!compat && !ro && !incompat)
1299 return 1;
1300 if (journal->j_format_version == 1)
1301 return 0;
1303 sb = journal->j_superblock;
1305 if (((be32_to_cpu(sb->s_feature_compat) & compat) == compat) &&
1306 ((be32_to_cpu(sb->s_feature_ro_compat) & ro) == ro) &&
1307 ((be32_to_cpu(sb->s_feature_incompat) & incompat) == incompat))
1308 return 1;
1310 return 0;
1314 * int journal_check_available_features() - Check feature set in journalling layer
1315 * @journal: Journal to check.
1316 * @compat: bitmask of compatible features
1317 * @ro: bitmask of features that force read-only mount
1318 * @incompat: bitmask of incompatible features
1320 * Check whether the journaling code supports the use of
1321 * all of a given set of features on this journal. Return true
1322 * (non-zero) if it can. */
1324 int journal_check_available_features (journal_t *journal, unsigned long compat,
1325 unsigned long ro, unsigned long incompat)
1327 if (!compat && !ro && !incompat)
1328 return 1;
1330 /* We can support any known requested features iff the
1331 * superblock is in version 2. Otherwise we fail to support any
1332 * extended sb features. */
1334 if (journal->j_format_version != 2)
1335 return 0;
1337 if ((compat & JFS_KNOWN_COMPAT_FEATURES) == compat &&
1338 (ro & JFS_KNOWN_ROCOMPAT_FEATURES) == ro &&
1339 (incompat & JFS_KNOWN_INCOMPAT_FEATURES) == incompat)
1340 return 1;
1342 return 0;
1346 * int journal_set_features () - Mark a given journal feature in the superblock
1347 * @journal: Journal to act on.
1348 * @compat: bitmask of compatible features
1349 * @ro: bitmask of features that force read-only mount
1350 * @incompat: bitmask of incompatible features
1352 * Mark a given journal feature as present on the
1353 * superblock. Returns true if the requested features could be set.
1357 int journal_set_features (journal_t *journal, unsigned long compat,
1358 unsigned long ro, unsigned long incompat)
1360 journal_superblock_t *sb;
1362 if (journal_check_used_features(journal, compat, ro, incompat))
1363 return 1;
1365 if (!journal_check_available_features(journal, compat, ro, incompat))
1366 return 0;
1368 jbd_debug(1, "Setting new features 0x%lx/0x%lx/0x%lx\n",
1369 compat, ro, incompat);
1371 sb = journal->j_superblock;
1373 sb->s_feature_compat |= cpu_to_be32(compat);
1374 sb->s_feature_ro_compat |= cpu_to_be32(ro);
1375 sb->s_feature_incompat |= cpu_to_be32(incompat);
1377 return 1;
1382 * int journal_update_format () - Update on-disk journal structure.
1383 * @journal: Journal to act on.
1385 * Given an initialised but unloaded journal struct, poke about in the
1386 * on-disk structure to update it to the most recent supported version.
1388 int journal_update_format (journal_t *journal)
1390 journal_superblock_t *sb;
1391 int err;
1393 err = journal_get_superblock(journal);
1394 if (err)
1395 return err;
1397 sb = journal->j_superblock;
1399 switch (be32_to_cpu(sb->s_header.h_blocktype)) {
1400 case JFS_SUPERBLOCK_V2:
1401 return 0;
1402 case JFS_SUPERBLOCK_V1:
1403 return journal_convert_superblock_v1(journal, sb);
1404 default:
1405 break;
1407 return -EINVAL;
1410 static int journal_convert_superblock_v1(journal_t *journal,
1411 journal_superblock_t *sb)
1413 int offset, blocksize;
1414 struct buffer_head *bh;
1416 printk(KERN_WARNING
1417 "JBD: Converting superblock from version 1 to 2.\n");
1419 /* Pre-initialise new fields to zero */
1420 offset = ((char *) &(sb->s_feature_compat)) - ((char *) sb);
1421 blocksize = be32_to_cpu(sb->s_blocksize);
1422 memset(&sb->s_feature_compat, 0, blocksize-offset);
1424 sb->s_nr_users = cpu_to_be32(1);
1425 sb->s_header.h_blocktype = cpu_to_be32(JFS_SUPERBLOCK_V2);
1426 journal->j_format_version = 2;
1428 bh = journal->j_sb_buffer;
1429 BUFFER_TRACE(bh, "marking dirty");
1430 mark_buffer_dirty(bh);
1431 sync_dirty_buffer(bh);
1432 return 0;
1437 * int journal_flush () - Flush journal
1438 * @journal: Journal to act on.
1440 * Flush all data for a given journal to disk and empty the journal.
1441 * Filesystems can use this when remounting readonly to ensure that
1442 * recovery does not need to happen on remount.
1445 int journal_flush(journal_t *journal)
1447 int err = 0;
1448 transaction_t *transaction = NULL;
1449 unsigned int old_tail;
1451 spin_lock(&journal->j_state_lock);
1453 /* Force everything buffered to the log... */
1454 if (journal->j_running_transaction) {
1455 transaction = journal->j_running_transaction;
1456 __log_start_commit(journal, transaction->t_tid);
1457 } else if (journal->j_committing_transaction)
1458 transaction = journal->j_committing_transaction;
1460 /* Wait for the log commit to complete... */
1461 if (transaction) {
1462 tid_t tid = transaction->t_tid;
1464 spin_unlock(&journal->j_state_lock);
1465 log_wait_commit(journal, tid);
1466 } else {
1467 spin_unlock(&journal->j_state_lock);
1470 /* ...and flush everything in the log out to disk. */
1471 spin_lock(&journal->j_list_lock);
1472 while (!err && journal->j_checkpoint_transactions != NULL) {
1473 spin_unlock(&journal->j_list_lock);
1474 mutex_lock(&journal->j_checkpoint_mutex);
1475 err = log_do_checkpoint(journal);
1476 mutex_unlock(&journal->j_checkpoint_mutex);
1477 spin_lock(&journal->j_list_lock);
1479 spin_unlock(&journal->j_list_lock);
1481 if (is_journal_aborted(journal))
1482 return -EIO;
1484 cleanup_journal_tail(journal);
1486 /* Finally, mark the journal as really needing no recovery.
1487 * This sets s_start==0 in the underlying superblock, which is
1488 * the magic code for a fully-recovered superblock. Any future
1489 * commits of data to the journal will restore the current
1490 * s_start value. */
1491 spin_lock(&journal->j_state_lock);
1492 old_tail = journal->j_tail;
1493 journal->j_tail = 0;
1494 spin_unlock(&journal->j_state_lock);
1495 journal_update_superblock(journal, 1);
1496 spin_lock(&journal->j_state_lock);
1497 journal->j_tail = old_tail;
1499 J_ASSERT(!journal->j_running_transaction);
1500 J_ASSERT(!journal->j_committing_transaction);
1501 J_ASSERT(!journal->j_checkpoint_transactions);
1502 J_ASSERT(journal->j_head == journal->j_tail);
1503 J_ASSERT(journal->j_tail_sequence == journal->j_transaction_sequence);
1504 spin_unlock(&journal->j_state_lock);
1505 return 0;
1509 * int journal_wipe() - Wipe journal contents
1510 * @journal: Journal to act on.
1511 * @write: flag (see below)
1513 * Wipe out all of the contents of a journal, safely. This will produce
1514 * a warning if the journal contains any valid recovery information.
1515 * Must be called between journal_init_*() and journal_load().
1517 * If 'write' is non-zero, then we wipe out the journal on disk; otherwise
1518 * we merely suppress recovery.
1521 int journal_wipe(journal_t *journal, int write)
1523 int err = 0;
1525 J_ASSERT (!(journal->j_flags & JFS_LOADED));
1527 err = load_superblock(journal);
1528 if (err)
1529 return err;
1531 if (!journal->j_tail)
1532 goto no_recovery;
1534 printk (KERN_WARNING "JBD: %s recovery information on journal\n",
1535 write ? "Clearing" : "Ignoring");
1537 err = journal_skip_recovery(journal);
1538 if (write)
1539 journal_update_superblock(journal, 1);
1541 no_recovery:
1542 return err;
1546 * journal_dev_name: format a character string to describe on what
1547 * device this journal is present.
1550 static const char *journal_dev_name(journal_t *journal, char *buffer)
1552 struct block_device *bdev;
1554 if (journal->j_inode)
1555 bdev = journal->j_inode->i_sb->s_bdev;
1556 else
1557 bdev = journal->j_dev;
1559 return bdevname(bdev, buffer);
1563 * Journal abort has very specific semantics, which we describe
1564 * for journal abort.
1566 * Two internal function, which provide abort to te jbd layer
1567 * itself are here.
1571 * Quick version for internal journal use (doesn't lock the journal).
1572 * Aborts hard --- we mark the abort as occurred, but do _nothing_ else,
1573 * and don't attempt to make any other journal updates.
1575 static void __journal_abort_hard(journal_t *journal)
1577 transaction_t *transaction;
1578 char b[BDEVNAME_SIZE];
1580 if (journal->j_flags & JFS_ABORT)
1581 return;
1583 printk(KERN_ERR "Aborting journal on device %s.\n",
1584 journal_dev_name(journal, b));
1586 spin_lock(&journal->j_state_lock);
1587 journal->j_flags |= JFS_ABORT;
1588 transaction = journal->j_running_transaction;
1589 if (transaction)
1590 __log_start_commit(journal, transaction->t_tid);
1591 spin_unlock(&journal->j_state_lock);
1594 /* Soft abort: record the abort error status in the journal superblock,
1595 * but don't do any other IO. */
1596 static void __journal_abort_soft (journal_t *journal, int errno)
1598 if (journal->j_flags & JFS_ABORT)
1599 return;
1601 if (!journal->j_errno)
1602 journal->j_errno = errno;
1604 __journal_abort_hard(journal);
1606 if (errno)
1607 journal_update_superblock(journal, 1);
1611 * void journal_abort () - Shutdown the journal immediately.
1612 * @journal: the journal to shutdown.
1613 * @errno: an error number to record in the journal indicating
1614 * the reason for the shutdown.
1616 * Perform a complete, immediate shutdown of the ENTIRE
1617 * journal (not of a single transaction). This operation cannot be
1618 * undone without closing and reopening the journal.
1620 * The journal_abort function is intended to support higher level error
1621 * recovery mechanisms such as the ext2/ext3 remount-readonly error
1622 * mode.
1624 * Journal abort has very specific semantics. Any existing dirty,
1625 * unjournaled buffers in the main filesystem will still be written to
1626 * disk by bdflush, but the journaling mechanism will be suspended
1627 * immediately and no further transaction commits will be honoured.
1629 * Any dirty, journaled buffers will be written back to disk without
1630 * hitting the journal. Atomicity cannot be guaranteed on an aborted
1631 * filesystem, but we _do_ attempt to leave as much data as possible
1632 * behind for fsck to use for cleanup.
1634 * Any attempt to get a new transaction handle on a journal which is in
1635 * ABORT state will just result in an -EROFS error return. A
1636 * journal_stop on an existing handle will return -EIO if we have
1637 * entered abort state during the update.
1639 * Recursive transactions are not disturbed by journal abort until the
1640 * final journal_stop, which will receive the -EIO error.
1642 * Finally, the journal_abort call allows the caller to supply an errno
1643 * which will be recorded (if possible) in the journal superblock. This
1644 * allows a client to record failure conditions in the middle of a
1645 * transaction without having to complete the transaction to record the
1646 * failure to disk. ext3_error, for example, now uses this
1647 * functionality.
1649 * Errors which originate from within the journaling layer will NOT
1650 * supply an errno; a null errno implies that absolutely no further
1651 * writes are done to the journal (unless there are any already in
1652 * progress).
1656 void journal_abort(journal_t *journal, int errno)
1658 __journal_abort_soft(journal, errno);
1662 * int journal_errno () - returns the journal's error state.
1663 * @journal: journal to examine.
1665 * This is the errno numbet set with journal_abort(), the last
1666 * time the journal was mounted - if the journal was stopped
1667 * without calling abort this will be 0.
1669 * If the journal has been aborted on this mount time -EROFS will
1670 * be returned.
1672 int journal_errno(journal_t *journal)
1674 int err;
1676 spin_lock(&journal->j_state_lock);
1677 if (journal->j_flags & JFS_ABORT)
1678 err = -EROFS;
1679 else
1680 err = journal->j_errno;
1681 spin_unlock(&journal->j_state_lock);
1682 return err;
1686 * int journal_clear_err () - clears the journal's error state
1687 * @journal: journal to act on.
1689 * An error must be cleared or Acked to take a FS out of readonly
1690 * mode.
1692 int journal_clear_err(journal_t *journal)
1694 int err = 0;
1696 spin_lock(&journal->j_state_lock);
1697 if (journal->j_flags & JFS_ABORT)
1698 err = -EROFS;
1699 else
1700 journal->j_errno = 0;
1701 spin_unlock(&journal->j_state_lock);
1702 return err;
1706 * void journal_ack_err() - Ack journal err.
1707 * @journal: journal to act on.
1709 * An error must be cleared or Acked to take a FS out of readonly
1710 * mode.
1712 void journal_ack_err(journal_t *journal)
1714 spin_lock(&journal->j_state_lock);
1715 if (journal->j_errno)
1716 journal->j_flags |= JFS_ACK_ERR;
1717 spin_unlock(&journal->j_state_lock);
1720 int journal_blocks_per_page(struct inode *inode)
1722 return 1 << (PAGE_CACHE_SHIFT - inode->i_sb->s_blocksize_bits);
1726 * Journal_head storage management
1728 static struct kmem_cache *journal_head_cache;
1729 #ifdef CONFIG_JBD_DEBUG
1730 static atomic_t nr_journal_heads = ATOMIC_INIT(0);
1731 #endif
1733 static int journal_init_journal_head_cache(void)
1735 int retval;
1737 J_ASSERT(journal_head_cache == NULL);
1738 journal_head_cache = kmem_cache_create("journal_head",
1739 sizeof(struct journal_head),
1740 0, /* offset */
1741 SLAB_TEMPORARY, /* flags */
1742 NULL); /* ctor */
1743 retval = 0;
1744 if (!journal_head_cache) {
1745 retval = -ENOMEM;
1746 printk(KERN_EMERG "JBD: no memory for journal_head cache\n");
1748 return retval;
1751 static void journal_destroy_journal_head_cache(void)
1753 if (journal_head_cache) {
1754 kmem_cache_destroy(journal_head_cache);
1755 journal_head_cache = NULL;
1760 * journal_head splicing and dicing
1762 static struct journal_head *journal_alloc_journal_head(void)
1764 struct journal_head *ret;
1766 #ifdef CONFIG_JBD_DEBUG
1767 atomic_inc(&nr_journal_heads);
1768 #endif
1769 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1770 if (ret == NULL) {
1771 jbd_debug(1, "out of memory for journal_head\n");
1772 printk_ratelimited(KERN_NOTICE "ENOMEM in %s, retrying.\n",
1773 __func__);
1775 while (ret == NULL) {
1776 yield();
1777 ret = kmem_cache_alloc(journal_head_cache, GFP_NOFS);
1780 return ret;
1783 static void journal_free_journal_head(struct journal_head *jh)
1785 #ifdef CONFIG_JBD_DEBUG
1786 atomic_dec(&nr_journal_heads);
1787 memset(jh, JBD_POISON_FREE, sizeof(*jh));
1788 #endif
1789 kmem_cache_free(journal_head_cache, jh);
1793 * A journal_head is attached to a buffer_head whenever JBD has an
1794 * interest in the buffer.
1796 * Whenever a buffer has an attached journal_head, its ->b_state:BH_JBD bit
1797 * is set. This bit is tested in core kernel code where we need to take
1798 * JBD-specific actions. Testing the zeroness of ->b_private is not reliable
1799 * there.
1801 * When a buffer has its BH_JBD bit set, its ->b_count is elevated by one.
1803 * When a buffer has its BH_JBD bit set it is immune from being released by
1804 * core kernel code, mainly via ->b_count.
1806 * A journal_head is detached from its buffer_head when the journal_head's
1807 * b_jcount reaches zero. Running transaction (b_transaction) and checkpoint
1808 * transaction (b_cp_transaction) hold their references to b_jcount.
1810 * Various places in the kernel want to attach a journal_head to a buffer_head
1811 * _before_ attaching the journal_head to a transaction. To protect the
1812 * journal_head in this situation, journal_add_journal_head elevates the
1813 * journal_head's b_jcount refcount by one. The caller must call
1814 * journal_put_journal_head() to undo this.
1816 * So the typical usage would be:
1818 * (Attach a journal_head if needed. Increments b_jcount)
1819 * struct journal_head *jh = journal_add_journal_head(bh);
1820 * ...
1821 * (Get another reference for transaction)
1822 * journal_grab_journal_head(bh);
1823 * jh->b_transaction = xxx;
1824 * (Put original reference)
1825 * journal_put_journal_head(jh);
1829 * Give a buffer_head a journal_head.
1831 * May sleep.
1833 struct journal_head *journal_add_journal_head(struct buffer_head *bh)
1835 struct journal_head *jh;
1836 struct journal_head *new_jh = NULL;
1838 repeat:
1839 if (!buffer_jbd(bh)) {
1840 new_jh = journal_alloc_journal_head();
1841 memset(new_jh, 0, sizeof(*new_jh));
1844 jbd_lock_bh_journal_head(bh);
1845 if (buffer_jbd(bh)) {
1846 jh = bh2jh(bh);
1847 } else {
1848 J_ASSERT_BH(bh,
1849 (atomic_read(&bh->b_count) > 0) ||
1850 (bh->b_page && bh->b_page->mapping));
1852 if (!new_jh) {
1853 jbd_unlock_bh_journal_head(bh);
1854 goto repeat;
1857 jh = new_jh;
1858 new_jh = NULL; /* We consumed it */
1859 set_buffer_jbd(bh);
1860 bh->b_private = jh;
1861 jh->b_bh = bh;
1862 get_bh(bh);
1863 BUFFER_TRACE(bh, "added journal_head");
1865 jh->b_jcount++;
1866 jbd_unlock_bh_journal_head(bh);
1867 if (new_jh)
1868 journal_free_journal_head(new_jh);
1869 return bh->b_private;
1873 * Grab a ref against this buffer_head's journal_head. If it ended up not
1874 * having a journal_head, return NULL
1876 struct journal_head *journal_grab_journal_head(struct buffer_head *bh)
1878 struct journal_head *jh = NULL;
1880 jbd_lock_bh_journal_head(bh);
1881 if (buffer_jbd(bh)) {
1882 jh = bh2jh(bh);
1883 jh->b_jcount++;
1885 jbd_unlock_bh_journal_head(bh);
1886 return jh;
1889 static void __journal_remove_journal_head(struct buffer_head *bh)
1891 struct journal_head *jh = bh2jh(bh);
1893 J_ASSERT_JH(jh, jh->b_jcount >= 0);
1894 J_ASSERT_JH(jh, jh->b_transaction == NULL);
1895 J_ASSERT_JH(jh, jh->b_next_transaction == NULL);
1896 J_ASSERT_JH(jh, jh->b_cp_transaction == NULL);
1897 J_ASSERT_JH(jh, jh->b_jlist == BJ_None);
1898 J_ASSERT_BH(bh, buffer_jbd(bh));
1899 J_ASSERT_BH(bh, jh2bh(jh) == bh);
1900 BUFFER_TRACE(bh, "remove journal_head");
1901 if (jh->b_frozen_data) {
1902 printk(KERN_WARNING "%s: freeing b_frozen_data\n", __func__);
1903 jbd_free(jh->b_frozen_data, bh->b_size);
1905 if (jh->b_committed_data) {
1906 printk(KERN_WARNING "%s: freeing b_committed_data\n", __func__);
1907 jbd_free(jh->b_committed_data, bh->b_size);
1909 bh->b_private = NULL;
1910 jh->b_bh = NULL; /* debug, really */
1911 clear_buffer_jbd(bh);
1912 journal_free_journal_head(jh);
1916 * Drop a reference on the passed journal_head. If it fell to zero then
1917 * release the journal_head from the buffer_head.
1919 void journal_put_journal_head(struct journal_head *jh)
1921 struct buffer_head *bh = jh2bh(jh);
1923 jbd_lock_bh_journal_head(bh);
1924 J_ASSERT_JH(jh, jh->b_jcount > 0);
1925 --jh->b_jcount;
1926 if (!jh->b_jcount) {
1927 __journal_remove_journal_head(bh);
1928 jbd_unlock_bh_journal_head(bh);
1929 __brelse(bh);
1930 } else
1931 jbd_unlock_bh_journal_head(bh);
1935 * debugfs tunables
1937 #ifdef CONFIG_JBD_DEBUG
1939 u8 journal_enable_debug __read_mostly;
1940 EXPORT_SYMBOL(journal_enable_debug);
1942 static struct dentry *jbd_debugfs_dir;
1943 static struct dentry *jbd_debug;
1945 static void __init jbd_create_debugfs_entry(void)
1947 jbd_debugfs_dir = debugfs_create_dir("jbd", NULL);
1948 if (jbd_debugfs_dir)
1949 jbd_debug = debugfs_create_u8("jbd-debug", S_IRUGO | S_IWUSR,
1950 jbd_debugfs_dir,
1951 &journal_enable_debug);
1954 static void __exit jbd_remove_debugfs_entry(void)
1956 debugfs_remove(jbd_debug);
1957 debugfs_remove(jbd_debugfs_dir);
1960 #else
1962 static inline void jbd_create_debugfs_entry(void)
1966 static inline void jbd_remove_debugfs_entry(void)
1970 #endif
1972 struct kmem_cache *jbd_handle_cache;
1974 static int __init journal_init_handle_cache(void)
1976 jbd_handle_cache = kmem_cache_create("journal_handle",
1977 sizeof(handle_t),
1978 0, /* offset */
1979 SLAB_TEMPORARY, /* flags */
1980 NULL); /* ctor */
1981 if (jbd_handle_cache == NULL) {
1982 printk(KERN_EMERG "JBD: failed to create handle cache\n");
1983 return -ENOMEM;
1985 return 0;
1988 static void journal_destroy_handle_cache(void)
1990 if (jbd_handle_cache)
1991 kmem_cache_destroy(jbd_handle_cache);
1995 * Module startup and shutdown
1998 static int __init journal_init_caches(void)
2000 int ret;
2002 ret = journal_init_revoke_caches();
2003 if (ret == 0)
2004 ret = journal_init_journal_head_cache();
2005 if (ret == 0)
2006 ret = journal_init_handle_cache();
2007 return ret;
2010 static void journal_destroy_caches(void)
2012 journal_destroy_revoke_caches();
2013 journal_destroy_journal_head_cache();
2014 journal_destroy_handle_cache();
2017 static int __init journal_init(void)
2019 int ret;
2021 BUILD_BUG_ON(sizeof(struct journal_superblock_s) != 1024);
2023 ret = journal_init_caches();
2024 if (ret != 0)
2025 journal_destroy_caches();
2026 jbd_create_debugfs_entry();
2027 return ret;
2030 static void __exit journal_exit(void)
2032 #ifdef CONFIG_JBD_DEBUG
2033 int n = atomic_read(&nr_journal_heads);
2034 if (n)
2035 printk(KERN_EMERG "JBD: leaked %d journal_heads!\n", n);
2036 #endif
2037 jbd_remove_debugfs_entry();
2038 journal_destroy_caches();
2041 MODULE_LICENSE("GPL");
2042 module_init(journal_init);
2043 module_exit(journal_exit);