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