Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Interface between ext4 and JBD |
| 3 | */ |
| 4 | |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 5 | #include "ext4_jbd2.h" |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 6 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 7 | #include <trace/events/ext4.h> |
| 8 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 9 | int __ext4_journal_get_write_access(const char *where, unsigned int line, |
| 10 | handle_t *handle, struct buffer_head *bh) |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 11 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 12 | int err = 0; |
| 13 | |
| 14 | if (ext4_handle_valid(handle)) { |
| 15 | err = jbd2_journal_get_write_access(handle, bh); |
| 16 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 17 | ext4_journal_abort_handle(where, line, __func__, bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 18 | handle, err); |
| 19 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 20 | return err; |
| 21 | } |
| 22 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 23 | /* |
| 24 | * The ext4 forget function must perform a revoke if we are freeing data |
| 25 | * which has been journaled. Metadata (eg. indirect blocks) must be |
| 26 | * revoked in all cases. |
| 27 | * |
| 28 | * "bh" may be NULL: a metadata block may have been freed from memory |
| 29 | * but there may still be a record of it in the journal, and that record |
| 30 | * still needs to be revoked. |
| 31 | * |
| 32 | * If the handle isn't valid we're not journaling, but we still need to |
| 33 | * call into ext4_journal_revoke() to put the buffer head. |
| 34 | */ |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 35 | int __ext4_forget(const char *where, unsigned int line, handle_t *handle, |
| 36 | int is_metadata, struct inode *inode, |
| 37 | struct buffer_head *bh, ext4_fsblk_t blocknr) |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 38 | { |
| 39 | int err; |
| 40 | |
| 41 | might_sleep(); |
| 42 | |
| 43 | trace_ext4_forget(inode, is_metadata, blocknr); |
| 44 | BUFFER_TRACE(bh, "enter"); |
| 45 | |
| 46 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " |
| 47 | "data mode %x\n", |
| 48 | bh, is_metadata, inode->i_mode, |
| 49 | test_opt(inode->i_sb, DATA_FLAGS)); |
| 50 | |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 51 | /* In the no journal case, we can just do a bforget and return */ |
| 52 | if (!ext4_handle_valid(handle)) { |
| 53 | bforget(bh); |
| 54 | return 0; |
| 55 | } |
| 56 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 57 | /* Never use the revoke function if we are doing full data |
| 58 | * journaling: there is no need to, and a V1 superblock won't |
| 59 | * support it. Otherwise, only skip the revoke on un-journaled |
| 60 | * data blocks. */ |
| 61 | |
| 62 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || |
| 63 | (!is_metadata && !ext4_should_journal_data(inode))) { |
| 64 | if (bh) { |
| 65 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); |
Theodore Ts'o | b7e57e7 | 2009-11-22 21:00:13 -0500 | [diff] [blame] | 66 | err = jbd2_journal_forget(handle, bh); |
| 67 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 68 | ext4_journal_abort_handle(where, line, __func__, |
| 69 | bh, handle, err); |
Theodore Ts'o | b7e57e7 | 2009-11-22 21:00:13 -0500 | [diff] [blame] | 70 | return err; |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 71 | } |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | /* |
| 76 | * data!=journal && (is_metadata || should_journal_data(inode)) |
| 77 | */ |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 78 | BUFFER_TRACE(bh, "call jbd2_journal_revoke"); |
| 79 | err = jbd2_journal_revoke(handle, blocknr, bh); |
| 80 | if (err) { |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 81 | ext4_journal_abort_handle(where, line, __func__, |
| 82 | bh, handle, err); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 83 | __ext4_abort(inode->i_sb, where, line, |
| 84 | "error %d when attempting revoke", err); |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 85 | } |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 86 | BUFFER_TRACE(bh, "exit"); |
| 87 | return err; |
| 88 | } |
| 89 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 90 | int __ext4_journal_get_create_access(const char *where, unsigned int line, |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 91 | handle_t *handle, struct buffer_head *bh) |
| 92 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 93 | int err = 0; |
| 94 | |
| 95 | if (ext4_handle_valid(handle)) { |
| 96 | err = jbd2_journal_get_create_access(handle, bh); |
| 97 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 98 | ext4_journal_abort_handle(where, line, __func__, |
| 99 | bh, handle, err); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 100 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 101 | return err; |
| 102 | } |
| 103 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 104 | int __ext4_handle_dirty_metadata(const char *where, unsigned int line, |
| 105 | handle_t *handle, struct inode *inode, |
| 106 | struct buffer_head *bh) |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 107 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 108 | int err = 0; |
| 109 | |
| 110 | if (ext4_handle_valid(handle)) { |
| 111 | err = jbd2_journal_dirty_metadata(handle, bh); |
Jan Kara | 55d93e1 | 2013-08-12 13:53:28 +0000 | [diff] [blame] | 112 | /* Errors can only happen if there is a bug */ |
| 113 | if (WARN_ON_ONCE(err)) { |
| 114 | ext4_journal_abort_handle(where, line, __func__, bh, |
| 115 | handle, err); |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 116 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 117 | } else { |
Curt Wohlgemuth | 73b50c1 | 2010-02-16 15:06:29 -0500 | [diff] [blame] | 118 | if (inode) |
Theodore Ts'o | fe188c0 | 2009-09-12 13:41:55 -0400 | [diff] [blame] | 119 | mark_buffer_dirty_inode(bh, inode); |
| 120 | else |
| 121 | mark_buffer_dirty(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 122 | if (inode && inode_needs_sync(inode)) { |
| 123 | sync_dirty_buffer(bh); |
| 124 | if (buffer_req(bh) && !buffer_uptodate(bh)) { |
Theodore Ts'o | 1c13d5c | 2010-07-27 11:56:03 -0400 | [diff] [blame] | 125 | struct ext4_super_block *es; |
| 126 | |
| 127 | es = EXT4_SB(inode->i_sb)->s_es; |
| 128 | es->s_last_error_block = |
| 129 | cpu_to_le64(bh->b_blocknr); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 130 | ext4_error_inode(inode, where, line, |
| 131 | bh->b_blocknr, |
| 132 | "IO error syncing itable block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 133 | err = -EIO; |
| 134 | } |
| 135 | } |
| 136 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 137 | return err; |
| 138 | } |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 139 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 140 | int __ext4_handle_dirty_super(const char *where, unsigned int line, |
| 141 | handle_t *handle, struct super_block *sb) |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 142 | { |
| 143 | struct buffer_head *bh = EXT4_SB(sb)->s_sbh; |
| 144 | int err = 0; |
| 145 | |
| 146 | if (ext4_handle_valid(handle)) { |
| 147 | err = jbd2_journal_dirty_metadata(handle, bh); |
| 148 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 149 | ext4_journal_abort_handle(where, line, __func__, |
| 150 | bh, handle, err); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 151 | } else |
| 152 | sb->s_dirt = 1; |
| 153 | return err; |
| 154 | } |