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,
74 trace_ext4_journal_start(sb, blocks, rsv_blocks, revoke_creds,
76 err = ext4_journal_check_start(sb);
80 journal = EXT4_SB(sb)->s_journal;
82 return ext4_get_nojournal();
83 return jbd2__journal_start(journal, blocks, rsv_blocks, revoke_creds,
84 GFP_NOFS, type, line);
87 int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
89 struct super_block *sb;
93 if (!ext4_handle_valid(handle)) {
94 ext4_put_nojournal(handle);
99 if (!handle->h_transaction) {
100 rc = jbd2_journal_stop(handle);
101 return err ? err : rc;
104 sb = handle->h_transaction->t_journal->j_private;
105 rc = jbd2_journal_stop(handle);
110 __ext4_std_error(sb, where, line, err);
114 handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line,
117 struct super_block *sb;
120 if (!ext4_handle_valid(handle))
121 return ext4_get_nojournal();
123 sb = handle->h_journal->j_private;
124 trace_ext4_journal_start_reserved(sb,
125 jbd2_handle_buffer_credits(handle), _RET_IP_);
126 err = ext4_journal_check_start(sb);
128 jbd2_journal_free_reserved(handle);
132 err = jbd2_journal_start_reserved(handle, type, line);
138 int __ext4_journal_ensure_credits(handle_t *handle, int check_cred,
139 int extend_cred, int revoke_cred)
141 if (!ext4_handle_valid(handle))
143 if (jbd2_handle_buffer_credits(handle) >= check_cred &&
144 handle->h_revoke_credits >= revoke_cred)
146 extend_cred = max(0, extend_cred - jbd2_handle_buffer_credits(handle));
147 revoke_cred = max(0, revoke_cred - handle->h_revoke_credits);
148 return ext4_journal_extend(handle, extend_cred, revoke_cred);
151 static void ext4_journal_abort_handle(const char *caller, unsigned int line,
153 struct buffer_head *bh,
154 handle_t *handle, int err)
157 const char *errstr = ext4_decode_error(NULL, err, nbuf);
159 BUG_ON(!ext4_handle_valid(handle));
162 BUFFER_TRACE(bh, "abort");
167 if (is_handle_aborted(handle))
170 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
171 caller, line, errstr, err_fn);
173 jbd2_journal_abort_handle(handle);
176 int __ext4_journal_get_write_access(const char *where, unsigned int line,
177 handle_t *handle, struct buffer_head *bh)
183 if (ext4_handle_valid(handle)) {
184 err = jbd2_journal_get_write_access(handle, bh);
186 ext4_journal_abort_handle(where, line, __func__, bh,
193 * The ext4 forget function must perform a revoke if we are freeing data
194 * which has been journaled. Metadata (eg. indirect blocks) must be
195 * revoked in all cases.
197 * "bh" may be NULL: a metadata block may have been freed from memory
198 * but there may still be a record of it in the journal, and that record
199 * still needs to be revoked.
201 * If the handle isn't valid we're not journaling, but we still need to
202 * call into ext4_journal_revoke() to put the buffer head.
204 int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
205 int is_metadata, struct inode *inode,
206 struct buffer_head *bh, ext4_fsblk_t blocknr)
212 trace_ext4_forget(inode, is_metadata, blocknr);
213 BUFFER_TRACE(bh, "enter");
215 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
217 bh, is_metadata, inode->i_mode,
218 test_opt(inode->i_sb, DATA_FLAGS));
220 /* In the no journal case, we can just do a bforget and return */
221 if (!ext4_handle_valid(handle)) {
226 /* Never use the revoke function if we are doing full data
227 * journaling: there is no need to, and a V1 superblock won't
228 * support it. Otherwise, only skip the revoke on un-journaled
231 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
232 (!is_metadata && !ext4_should_journal_data(inode))) {
234 BUFFER_TRACE(bh, "call jbd2_journal_forget");
235 err = jbd2_journal_forget(handle, bh);
237 ext4_journal_abort_handle(where, line, __func__,
245 * data!=journal && (is_metadata || should_journal_data(inode))
247 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
248 err = jbd2_journal_revoke(handle, blocknr, bh);
250 ext4_journal_abort_handle(where, line, __func__,
252 __ext4_abort(inode->i_sb, where, line,
253 "error %d when attempting revoke", err);
255 BUFFER_TRACE(bh, "exit");
259 int __ext4_journal_get_create_access(const char *where, unsigned int line,
260 handle_t *handle, struct buffer_head *bh)
264 if (ext4_handle_valid(handle)) {
265 err = jbd2_journal_get_create_access(handle, bh);
267 ext4_journal_abort_handle(where, line, __func__,
273 int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
274 handle_t *handle, struct inode *inode,
275 struct buffer_head *bh)
283 if (ext4_handle_valid(handle)) {
284 err = jbd2_journal_dirty_metadata(handle, bh);
285 /* Errors can only happen due to aborted journal or a nasty bug */
286 if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) {
287 ext4_journal_abort_handle(where, line, __func__, bh,
290 pr_err("EXT4: jbd2_journal_dirty_metadata "
291 "failed: handle type %u started at "
292 "line %u, credits %u/%u, errcode %d",
295 handle->h_requested_credits,
296 jbd2_handle_buffer_credits(handle), err);
299 ext4_error_inode(inode, where, line,
301 "journal_dirty_metadata failed: "
302 "handle type %u started at line %u, "
303 "credits %u/%u, errcode %d",
306 handle->h_requested_credits,
307 jbd2_handle_buffer_credits(handle),
312 mark_buffer_dirty_inode(bh, inode);
314 mark_buffer_dirty(bh);
315 if (inode && inode_needs_sync(inode)) {
316 sync_dirty_buffer(bh);
317 if (buffer_req(bh) && !buffer_uptodate(bh)) {
318 struct ext4_super_block *es;
320 es = EXT4_SB(inode->i_sb)->s_es;
321 es->s_last_error_block =
322 cpu_to_le64(bh->b_blocknr);
323 ext4_error_inode(inode, where, line,
325 "IO error syncing itable block");
333 int __ext4_handle_dirty_super(const char *where, unsigned int line,
334 handle_t *handle, struct super_block *sb)
336 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
339 ext4_superblock_csum_set(sb);
340 if (ext4_handle_valid(handle)) {
341 err = jbd2_journal_dirty_metadata(handle, bh);
343 ext4_journal_abort_handle(where, line, __func__,
346 mark_buffer_dirty(bh);