1 // SPDX-License-Identifier: GPL-2.0
3 * Interface between ext4 and JBD
8 #include <trace/events/ext4.h>
10 /* Just increment the non-pointer handle value */
11 static handle_t *ext4_get_nojournal(void)
13 handle_t *handle = current->journal_info;
14 unsigned long ref_cnt = (unsigned long)handle;
16 BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
19 handle = (handle_t *)ref_cnt;
21 current->journal_info = handle;
26 /* Decrement the non-pointer handle value */
27 static void ext4_put_nojournal(handle_t *handle)
29 unsigned long ref_cnt = (unsigned long)handle;
34 handle = (handle_t *)ref_cnt;
36 current->journal_info = handle;
40 * Wrappers for jbd2_journal_start/end.
42 static int ext4_journal_check_start(struct super_block *sb)
48 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
53 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
54 journal = EXT4_SB(sb)->s_journal;
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.
60 if (journal && is_journal_aborted(journal)) {
61 ext4_abort(sb, "Detected aborted journal");
67 handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
68 int type, int blocks, int rsv_blocks)
73 trace_ext4_journal_start(sb, blocks, rsv_blocks, _RET_IP_);
74 err = ext4_journal_check_start(sb);
78 journal = EXT4_SB(sb)->s_journal;
80 return ext4_get_nojournal();
81 return jbd2__journal_start(journal, blocks, rsv_blocks, GFP_NOFS,
85 int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
87 struct super_block *sb;
91 if (!ext4_handle_valid(handle)) {
92 ext4_put_nojournal(handle);
97 if (!handle->h_transaction) {
98 rc = jbd2_journal_stop(handle);
99 return err ? err : rc;
102 sb = handle->h_transaction->t_journal->j_private;
103 rc = jbd2_journal_stop(handle);
108 __ext4_std_error(sb, where, line, err);
112 handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line,
115 struct super_block *sb;
118 if (!ext4_handle_valid(handle))
119 return ext4_get_nojournal();
121 sb = handle->h_journal->j_private;
122 trace_ext4_journal_start_reserved(sb, handle->h_buffer_credits,
124 err = ext4_journal_check_start(sb);
126 jbd2_journal_free_reserved(handle);
130 err = jbd2_journal_start_reserved(handle, type, line);
136 static void ext4_journal_abort_handle(const char *caller, unsigned int line,
138 struct buffer_head *bh,
139 handle_t *handle, int err)
142 const char *errstr = ext4_decode_error(NULL, err, nbuf);
144 BUG_ON(!ext4_handle_valid(handle));
147 BUFFER_TRACE(bh, "abort");
152 if (is_handle_aborted(handle))
155 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
156 caller, line, errstr, err_fn);
158 jbd2_journal_abort_handle(handle);
161 int __ext4_journal_get_write_access(const char *where, unsigned int line,
162 handle_t *handle, struct buffer_head *bh)
168 if (ext4_handle_valid(handle)) {
169 err = jbd2_journal_get_write_access(handle, bh);
171 ext4_journal_abort_handle(where, line, __func__, bh,
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.
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.
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.
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)
197 trace_ext4_forget(inode, is_metadata, blocknr);
198 BUFFER_TRACE(bh, "enter");
200 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
202 bh, is_metadata, inode->i_mode,
203 test_opt(inode->i_sb, DATA_FLAGS));
205 /* In the no journal case, we can just do a bforget and return */
206 if (!ext4_handle_valid(handle)) {
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
216 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
217 (!is_metadata && !ext4_should_journal_data(inode))) {
219 BUFFER_TRACE(bh, "call jbd2_journal_forget");
220 err = jbd2_journal_forget(handle, bh);
222 ext4_journal_abort_handle(where, line, __func__,
230 * data!=journal && (is_metadata || should_journal_data(inode))
232 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
233 err = jbd2_journal_revoke(handle, blocknr, bh);
235 ext4_journal_abort_handle(where, line, __func__,
237 __ext4_abort(inode->i_sb, where, line,
238 "error %d when attempting revoke", err);
240 BUFFER_TRACE(bh, "exit");
244 int __ext4_journal_get_create_access(const char *where, unsigned int line,
245 handle_t *handle, struct buffer_head *bh)
249 if (ext4_handle_valid(handle)) {
250 err = jbd2_journal_get_create_access(handle, bh);
252 ext4_journal_abort_handle(where, line, __func__,
258 int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
259 handle_t *handle, struct inode *inode,
260 struct buffer_head *bh)
268 if (ext4_handle_valid(handle)) {
269 err = jbd2_journal_dirty_metadata(handle, bh);
270 /* Errors can only happen due to aborted journal or a nasty bug */
271 if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) {
272 ext4_journal_abort_handle(where, line, __func__, bh,
275 pr_err("EXT4: jbd2_journal_dirty_metadata "
276 "failed: handle type %u started at "
277 "line %u, credits %u/%u, errcode %d",
280 handle->h_requested_credits,
281 handle->h_buffer_credits, err);
284 ext4_error_inode(inode, where, line,
286 "journal_dirty_metadata failed: "
287 "handle type %u started at line %u, "
288 "credits %u/%u, errcode %d",
291 handle->h_requested_credits,
292 handle->h_buffer_credits, err);
296 mark_buffer_dirty_inode(bh, inode);
298 mark_buffer_dirty(bh);
299 if (inode && inode_needs_sync(inode)) {
300 sync_dirty_buffer(bh);
301 if (buffer_req(bh) && !buffer_uptodate(bh)) {
302 struct ext4_super_block *es;
304 es = EXT4_SB(inode->i_sb)->s_es;
305 es->s_last_error_block =
306 cpu_to_le64(bh->b_blocknr);
307 ext4_error_inode(inode, where, line,
309 "IO error syncing itable block");
317 int __ext4_handle_dirty_super(const char *where, unsigned int line,
318 handle_t *handle, struct super_block *sb)
320 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
323 ext4_superblock_csum_set(sb);
324 if (ext4_handle_valid(handle)) {
325 err = jbd2_journal_dirty_metadata(handle, bh);
327 ext4_journal_abort_handle(where, line, __func__,
330 mark_buffer_dirty(bh);