ext4_jbd2.c 4.0 KB
Newer Older
1 2 3 4
/*
 * Interface between ext4 and JBD
 */

5
#include "ext4_jbd2.h"
6

7 8
#include <trace/events/ext4.h>

9 10
int __ext4_journal_get_write_access(const char *where, unsigned int line,
				    handle_t *handle, struct buffer_head *bh)
11
{
12 13 14 15 16
	int err = 0;

	if (ext4_handle_valid(handle)) {
		err = jbd2_journal_get_write_access(handle, bh);
		if (err)
17
			ext4_journal_abort_handle(where, line, __func__, bh,
18 19
						  handle, err);
	}
20 21 22
	return err;
}

23 24 25 26 27 28 29 30 31 32 33 34
/*
 * The ext4 forget function must perform a revoke if we are freeing data
 * which has been journaled.  Metadata (eg. indirect blocks) must be
 * revoked in all cases.
 *
 * "bh" may be NULL: a metadata block may have been freed from memory
 * but there may still be a record of it in the journal, and that record
 * still needs to be revoked.
 *
 * If the handle isn't valid we're not journaling, but we still need to
 * call into ext4_journal_revoke() to put the buffer head.
 */
35 36 37
int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
		  int is_metadata, struct inode *inode,
		  struct buffer_head *bh, ext4_fsblk_t blocknr)
38 39 40 41 42 43 44 45 46 47 48 49 50
{
	int err;

	might_sleep();

	trace_ext4_forget(inode, is_metadata, blocknr);
	BUFFER_TRACE(bh, "enter");

	jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
		  "data mode %x\n",
		  bh, is_metadata, inode->i_mode,
		  test_opt(inode->i_sb, DATA_FLAGS));

51 52 53 54 55 56
	/* In the no journal case, we can just do a bforget and return */
	if (!ext4_handle_valid(handle)) {
		bforget(bh);
		return 0;
	}

57 58 59 60 61 62 63 64 65
	/* Never use the revoke function if we are doing full data
	 * journaling: there is no need to, and a V1 superblock won't
	 * support it.  Otherwise, only skip the revoke on un-journaled
	 * data blocks. */

	if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
	    (!is_metadata && !ext4_should_journal_data(inode))) {
		if (bh) {
			BUFFER_TRACE(bh, "call jbd2_journal_forget");
66 67
			err = jbd2_journal_forget(handle, bh);
			if (err)
68 69
				ext4_journal_abort_handle(where, line, __func__,
							  bh, handle, err);
70
			return err;
71 72 73 74 75 76 77
		}
		return 0;
	}

	/*
	 * data!=journal && (is_metadata || should_journal_data(inode))
	 */
78 79 80
	BUFFER_TRACE(bh, "call jbd2_journal_revoke");
	err = jbd2_journal_revoke(handle, blocknr, bh);
	if (err) {
81 82
		ext4_journal_abort_handle(where, line, __func__,
					  bh, handle, err);
83 84
		__ext4_abort(inode->i_sb, where, line,
			   "error %d when attempting revoke", err);
85
	}
86 87 88 89
	BUFFER_TRACE(bh, "exit");
	return err;
}

90
int __ext4_journal_get_create_access(const char *where, unsigned int line,
91 92
				handle_t *handle, struct buffer_head *bh)
{
93 94 95 96 97
	int err = 0;

	if (ext4_handle_valid(handle)) {
		err = jbd2_journal_get_create_access(handle, bh);
		if (err)
98 99
			ext4_journal_abort_handle(where, line, __func__,
						  bh, handle, err);
100
	}
101 102 103
	return err;
}

104 105 106
int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
				 handle_t *handle, struct inode *inode,
				 struct buffer_head *bh)
107
{
108 109 110 111
	int err = 0;

	if (ext4_handle_valid(handle)) {
		err = jbd2_journal_dirty_metadata(handle, bh);
112 113 114 115 116
		if (err) {
			/* Errors can only happen if there is a bug */
			handle->h_err = err;
			__ext4_journal_stop(where, line, handle);
		}
117
	} else {
118
		if (inode)
119 120 121
			mark_buffer_dirty_inode(bh, inode);
		else
			mark_buffer_dirty(bh);
122 123 124
		if (inode && inode_needs_sync(inode)) {
			sync_dirty_buffer(bh);
			if (buffer_req(bh) && !buffer_uptodate(bh)) {
125 126 127 128 129
				struct ext4_super_block *es;

				es = EXT4_SB(inode->i_sb)->s_es;
				es->s_last_error_block =
					cpu_to_le64(bh->b_blocknr);
130 131 132
				ext4_error_inode(inode, where, line,
						 bh->b_blocknr,
					"IO error syncing itable block");
133 134 135 136
				err = -EIO;
			}
		}
	}
137 138
	return err;
}
T
Theodore Ts'o 已提交
139

140
int __ext4_handle_dirty_super(const char *where, unsigned int line,
141
			      handle_t *handle, struct super_block *sb)
T
Theodore Ts'o 已提交
142 143 144 145
{
	struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
	int err = 0;

146
	ext4_superblock_csum_set(sb);
T
Theodore Ts'o 已提交
147 148 149
	if (ext4_handle_valid(handle)) {
		err = jbd2_journal_dirty_metadata(handle, bh);
		if (err)
150 151
			ext4_journal_abort_handle(where, line, __func__,
						  bh, handle, err);
152
	} else
153
		mark_buffer_dirty(bh);
T
Theodore Ts'o 已提交
154 155
	return err;
}