Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 2 | /* |
| 3 | * Interface between ext4 and JBD |
| 4 | */ |
| 5 | |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 6 | #include "ext4_jbd2.h" |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 7 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 8 | #include <trace/events/ext4.h> |
| 9 | |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 10 | /* Just increment the non-pointer handle value */ |
| 11 | static handle_t *ext4_get_nojournal(void) |
| 12 | { |
| 13 | handle_t *handle = current->journal_info; |
| 14 | unsigned long ref_cnt = (unsigned long)handle; |
| 15 | |
| 16 | BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT); |
| 17 | |
| 18 | ref_cnt++; |
| 19 | handle = (handle_t *)ref_cnt; |
| 20 | |
| 21 | current->journal_info = handle; |
| 22 | return handle; |
| 23 | } |
| 24 | |
| 25 | |
| 26 | /* Decrement the non-pointer handle value */ |
| 27 | static void ext4_put_nojournal(handle_t *handle) |
| 28 | { |
| 29 | unsigned long ref_cnt = (unsigned long)handle; |
| 30 | |
| 31 | BUG_ON(ref_cnt == 0); |
| 32 | |
| 33 | ref_cnt--; |
| 34 | handle = (handle_t *)ref_cnt; |
| 35 | |
| 36 | current->journal_info = handle; |
| 37 | } |
| 38 | |
| 39 | /* |
| 40 | * Wrappers for jbd2_journal_start/end. |
| 41 | */ |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 42 | static int ext4_journal_check_start(struct super_block *sb) |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 43 | { |
| 44 | journal_t *journal; |
| 45 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 46 | might_sleep(); |
Theodore Ts'o | 0db1ff2 | 2017-02-05 01:28:48 -0500 | [diff] [blame] | 47 | |
| 48 | if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) |
| 49 | return -EIO; |
| 50 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 51 | if (sb_rdonly(sb)) |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 52 | return -EROFS; |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 53 | WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE); |
| 54 | journal = EXT4_SB(sb)->s_journal; |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 55 | /* |
| 56 | * Special case here: if the journal has aborted behind our |
| 57 | * backs (eg. EIO in the commit thread), then we still need to |
| 58 | * take the FS itself readonly cleanly. |
| 59 | */ |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 60 | if (journal && is_journal_aborted(journal)) { |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 61 | ext4_abort(sb, "Detected aborted journal"); |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 62 | return -EROFS; |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 63 | } |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line, |
| 68 | int type, int blocks, int rsv_blocks) |
| 69 | { |
| 70 | journal_t *journal; |
| 71 | int err; |
| 72 | |
| 73 | trace_ext4_journal_start(sb, blocks, rsv_blocks, _RET_IP_); |
| 74 | err = ext4_journal_check_start(sb); |
| 75 | if (err < 0) |
| 76 | return ERR_PTR(err); |
| 77 | |
| 78 | journal = EXT4_SB(sb)->s_journal; |
| 79 | if (!journal) |
| 80 | return ext4_get_nojournal(); |
| 81 | return jbd2__journal_start(journal, blocks, rsv_blocks, GFP_NOFS, |
| 82 | type, line); |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle) |
| 86 | { |
| 87 | struct super_block *sb; |
| 88 | int err; |
| 89 | int rc; |
| 90 | |
| 91 | if (!ext4_handle_valid(handle)) { |
| 92 | ext4_put_nojournal(handle); |
| 93 | return 0; |
| 94 | } |
Lukas Czerner | 9d50659 | 2015-05-14 18:55:18 -0400 | [diff] [blame] | 95 | |
Lukas Czerner | 6934da9 | 2015-10-17 22:57:06 -0400 | [diff] [blame] | 96 | err = handle->h_err; |
Lukas Czerner | 9d50659 | 2015-05-14 18:55:18 -0400 | [diff] [blame] | 97 | if (!handle->h_transaction) { |
Lukas Czerner | 6934da9 | 2015-10-17 22:57:06 -0400 | [diff] [blame] | 98 | rc = jbd2_journal_stop(handle); |
| 99 | return err ? err : rc; |
Lukas Czerner | 9d50659 | 2015-05-14 18:55:18 -0400 | [diff] [blame] | 100 | } |
| 101 | |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 102 | sb = handle->h_transaction->t_journal->j_private; |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 103 | rc = jbd2_journal_stop(handle); |
| 104 | |
| 105 | if (!err) |
| 106 | err = rc; |
| 107 | if (err) |
| 108 | __ext4_std_error(sb, where, line, err); |
| 109 | return err; |
| 110 | } |
| 111 | |
Jan Kara | 5fe2fe8 | 2013-06-04 12:37:50 -0400 | [diff] [blame] | 112 | handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line, |
| 113 | int type) |
| 114 | { |
| 115 | struct super_block *sb; |
| 116 | int err; |
| 117 | |
| 118 | if (!ext4_handle_valid(handle)) |
| 119 | return ext4_get_nojournal(); |
| 120 | |
| 121 | sb = handle->h_journal->j_private; |
| 122 | trace_ext4_journal_start_reserved(sb, handle->h_buffer_credits, |
| 123 | _RET_IP_); |
| 124 | err = ext4_journal_check_start(sb); |
| 125 | if (err < 0) { |
| 126 | jbd2_journal_free_reserved(handle); |
| 127 | return ERR_PTR(err); |
| 128 | } |
| 129 | |
| 130 | err = jbd2_journal_start_reserved(handle, type, line); |
| 131 | if (err < 0) |
| 132 | return ERR_PTR(err); |
| 133 | return handle; |
| 134 | } |
| 135 | |
Stephen Hemminger | c197855 | 2014-05-12 10:50:23 -0400 | [diff] [blame] | 136 | static void ext4_journal_abort_handle(const char *caller, unsigned int line, |
| 137 | const char *err_fn, |
| 138 | struct buffer_head *bh, |
| 139 | handle_t *handle, int err) |
Theodore Ts'o | 722887d | 2013-02-08 13:00:31 -0500 | [diff] [blame] | 140 | { |
| 141 | char nbuf[16]; |
| 142 | const char *errstr = ext4_decode_error(NULL, err, nbuf); |
| 143 | |
| 144 | BUG_ON(!ext4_handle_valid(handle)); |
| 145 | |
| 146 | if (bh) |
| 147 | BUFFER_TRACE(bh, "abort"); |
| 148 | |
| 149 | if (!handle->h_err) |
| 150 | handle->h_err = err; |
| 151 | |
| 152 | if (is_handle_aborted(handle)) |
| 153 | return; |
| 154 | |
| 155 | printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n", |
| 156 | caller, line, errstr, err_fn); |
| 157 | |
| 158 | jbd2_journal_abort_handle(handle); |
| 159 | } |
| 160 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 161 | int __ext4_journal_get_write_access(const char *where, unsigned int line, |
| 162 | handle_t *handle, struct buffer_head *bh) |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 163 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 164 | int err = 0; |
| 165 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 166 | might_sleep(); |
| 167 | |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 168 | if (ext4_handle_valid(handle)) { |
| 169 | err = jbd2_journal_get_write_access(handle, bh); |
| 170 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 171 | ext4_journal_abort_handle(where, line, __func__, bh, |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 172 | handle, err); |
| 173 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 174 | return err; |
| 175 | } |
| 176 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 177 | /* |
| 178 | * The ext4 forget function must perform a revoke if we are freeing data |
| 179 | * which has been journaled. Metadata (eg. indirect blocks) must be |
| 180 | * revoked in all cases. |
| 181 | * |
| 182 | * "bh" may be NULL: a metadata block may have been freed from memory |
| 183 | * but there may still be a record of it in the journal, and that record |
| 184 | * still needs to be revoked. |
| 185 | * |
| 186 | * If the handle isn't valid we're not journaling, but we still need to |
| 187 | * call into ext4_journal_revoke() to put the buffer head. |
| 188 | */ |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 189 | int __ext4_forget(const char *where, unsigned int line, handle_t *handle, |
| 190 | int is_metadata, struct inode *inode, |
| 191 | struct buffer_head *bh, ext4_fsblk_t blocknr) |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 192 | { |
| 193 | int err; |
| 194 | |
| 195 | might_sleep(); |
| 196 | |
| 197 | trace_ext4_forget(inode, is_metadata, blocknr); |
| 198 | BUFFER_TRACE(bh, "enter"); |
| 199 | |
| 200 | jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, " |
| 201 | "data mode %x\n", |
| 202 | bh, is_metadata, inode->i_mode, |
| 203 | test_opt(inode->i_sb, DATA_FLAGS)); |
| 204 | |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 205 | /* In the no journal case, we can just do a bforget and return */ |
| 206 | if (!ext4_handle_valid(handle)) { |
| 207 | bforget(bh); |
| 208 | return 0; |
| 209 | } |
| 210 | |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 211 | /* Never use the revoke function if we are doing full data |
| 212 | * journaling: there is no need to, and a V1 superblock won't |
| 213 | * support it. Otherwise, only skip the revoke on un-journaled |
| 214 | * data blocks. */ |
| 215 | |
| 216 | if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA || |
| 217 | (!is_metadata && !ext4_should_journal_data(inode))) { |
| 218 | if (bh) { |
| 219 | BUFFER_TRACE(bh, "call jbd2_journal_forget"); |
Theodore Ts'o | b7e57e7 | 2009-11-22 21:00:13 -0500 | [diff] [blame] | 220 | err = jbd2_journal_forget(handle, bh); |
| 221 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 222 | ext4_journal_abort_handle(where, line, __func__, |
| 223 | bh, handle, err); |
Theodore Ts'o | b7e57e7 | 2009-11-22 21:00:13 -0500 | [diff] [blame] | 224 | return err; |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * data!=journal && (is_metadata || should_journal_data(inode)) |
| 231 | */ |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 232 | BUFFER_TRACE(bh, "call jbd2_journal_revoke"); |
| 233 | err = jbd2_journal_revoke(handle, blocknr, bh); |
| 234 | if (err) { |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 235 | ext4_journal_abort_handle(where, line, __func__, |
| 236 | bh, handle, err); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 237 | __ext4_abort(inode->i_sb, where, line, |
| 238 | "error %d when attempting revoke", err); |
Theodore Ts'o | e4684b3 | 2009-11-24 11:05:59 -0500 | [diff] [blame] | 239 | } |
Theodore Ts'o | d6797d1 | 2009-11-22 20:52:12 -0500 | [diff] [blame] | 240 | BUFFER_TRACE(bh, "exit"); |
| 241 | return err; |
| 242 | } |
| 243 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 244 | int __ext4_journal_get_create_access(const char *where, unsigned int line, |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 245 | handle_t *handle, struct buffer_head *bh) |
| 246 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 247 | int err = 0; |
| 248 | |
| 249 | if (ext4_handle_valid(handle)) { |
| 250 | err = jbd2_journal_get_create_access(handle, bh); |
| 251 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 252 | ext4_journal_abort_handle(where, line, __func__, |
| 253 | bh, handle, err); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 254 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 255 | return err; |
| 256 | } |
| 257 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 258 | int __ext4_handle_dirty_metadata(const char *where, unsigned int line, |
| 259 | handle_t *handle, struct inode *inode, |
| 260 | struct buffer_head *bh) |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 261 | { |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 262 | int err = 0; |
| 263 | |
Theodore Ts'o | b10a44c | 2013-04-03 22:00:52 -0400 | [diff] [blame] | 264 | might_sleep(); |
| 265 | |
Theodore Ts'o | 13fca32 | 2013-04-21 16:45:54 -0400 | [diff] [blame] | 266 | set_buffer_meta(bh); |
| 267 | set_buffer_prio(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 268 | if (ext4_handle_valid(handle)) { |
| 269 | err = jbd2_journal_dirty_metadata(handle, bh); |
Dmitry Monakhov | c5d3119 | 2014-10-01 22:23:15 -0400 | [diff] [blame] | 270 | /* Errors can only happen due to aborted journal or a nasty bug */ |
| 271 | if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) { |
Jan Kara | 91aa11f | 2013-08-12 09:53:28 -0400 | [diff] [blame] | 272 | ext4_journal_abort_handle(where, line, __func__, bh, |
| 273 | handle, err); |
Theodore Ts'o | 66a4cb1 | 2014-03-12 16:38:03 -0400 | [diff] [blame] | 274 | if (inode == NULL) { |
| 275 | pr_err("EXT4: jbd2_journal_dirty_metadata " |
| 276 | "failed: handle type %u started at " |
| 277 | "line %u, credits %u/%u, errcode %d", |
| 278 | handle->h_type, |
| 279 | handle->h_line_no, |
| 280 | handle->h_requested_credits, |
| 281 | handle->h_buffer_credits, err); |
| 282 | return err; |
| 283 | } |
Theodore Ts'o | ae1495b | 2013-12-02 09:31:36 -0500 | [diff] [blame] | 284 | ext4_error_inode(inode, where, line, |
| 285 | bh->b_blocknr, |
| 286 | "journal_dirty_metadata failed: " |
| 287 | "handle type %u started at line %u, " |
| 288 | "credits %u/%u, errcode %d", |
| 289 | handle->h_type, |
| 290 | handle->h_line_no, |
| 291 | handle->h_requested_credits, |
| 292 | handle->h_buffer_credits, err); |
Theodore Ts'o | 9ea7a0d | 2011-09-04 10:18:14 -0400 | [diff] [blame] | 293 | } |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 294 | } else { |
Curt Wohlgemuth | 73b50c1 | 2010-02-16 15:06:29 -0500 | [diff] [blame] | 295 | if (inode) |
Theodore Ts'o | fe188c0 | 2009-09-12 13:41:55 -0400 | [diff] [blame] | 296 | mark_buffer_dirty_inode(bh, inode); |
| 297 | else |
| 298 | mark_buffer_dirty(bh); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 299 | if (inode && inode_needs_sync(inode)) { |
| 300 | sync_dirty_buffer(bh); |
| 301 | if (buffer_req(bh) && !buffer_uptodate(bh)) { |
Theodore Ts'o | 1c13d5c | 2010-07-27 11:56:03 -0400 | [diff] [blame] | 302 | struct ext4_super_block *es; |
| 303 | |
| 304 | es = EXT4_SB(inode->i_sb)->s_es; |
| 305 | es->s_last_error_block = |
| 306 | cpu_to_le64(bh->b_blocknr); |
Theodore Ts'o | c398eda | 2010-07-27 11:56:40 -0400 | [diff] [blame] | 307 | ext4_error_inode(inode, where, line, |
| 308 | bh->b_blocknr, |
| 309 | "IO error syncing itable block"); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 310 | err = -EIO; |
| 311 | } |
| 312 | } |
| 313 | } |
Andrew Morton | 8984d13 | 2006-12-06 20:37:15 -0800 | [diff] [blame] | 314 | return err; |
| 315 | } |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 316 | |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 317 | int __ext4_handle_dirty_super(const char *where, unsigned int line, |
Artem Bityutskiy | b50924c | 2012-07-22 20:37:31 -0400 | [diff] [blame] | 318 | handle_t *handle, struct super_block *sb) |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 319 | { |
| 320 | struct buffer_head *bh = EXT4_SB(sb)->s_sbh; |
| 321 | int err = 0; |
| 322 | |
Theodore Ts'o | 06db49e | 2012-10-10 01:06:58 -0400 | [diff] [blame] | 323 | ext4_superblock_csum_set(sb); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 324 | if (ext4_handle_valid(handle)) { |
| 325 | err = jbd2_journal_dirty_metadata(handle, bh); |
| 326 | if (err) |
Theodore Ts'o | 90c7201 | 2010-06-29 14:53:24 -0400 | [diff] [blame] | 327 | ext4_journal_abort_handle(where, line, __func__, |
| 328 | bh, handle, err); |
Theodore Ts'o | 06db49e | 2012-10-10 01:06:58 -0400 | [diff] [blame] | 329 | } else |
Darrick J. Wong | a9c47317 | 2012-04-29 18:29:10 -0400 | [diff] [blame] | 330 | mark_buffer_dirty(bh); |
Theodore Ts'o | a037515 | 2010-06-11 23:14:04 -0400 | [diff] [blame] | 331 | return err; |
| 332 | } |