2 * Interface between ext4 and JBD
7 #include <trace/events/ext4.h>
9 /* Just increment the non-pointer handle value */
10 static handle_t
*ext4_get_nojournal(void)
12 handle_t
*handle
= current
->journal_info
;
13 unsigned long ref_cnt
= (unsigned long)handle
;
15 BUG_ON(ref_cnt
>= EXT4_NOJOURNAL_MAX_REF_COUNT
);
18 handle
= (handle_t
*)ref_cnt
;
20 current
->journal_info
= handle
;
25 /* Decrement the non-pointer handle value */
26 static void ext4_put_nojournal(handle_t
*handle
)
28 unsigned long ref_cnt
= (unsigned long)handle
;
33 handle
= (handle_t
*)ref_cnt
;
35 current
->journal_info
= handle
;
39 * Wrappers for jbd2_journal_start/end.
41 static int ext4_journal_check_start(struct super_block
*sb
)
47 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb
))))
50 if (sb
->s_flags
& MS_RDONLY
)
52 WARN_ON(sb
->s_writers
.frozen
== SB_FREEZE_COMPLETE
);
53 journal
= EXT4_SB(sb
)->s_journal
;
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.
59 if (journal
&& is_journal_aborted(journal
)) {
60 ext4_abort(sb
, "Detected aborted journal");
66 handle_t
*__ext4_journal_start_sb(struct super_block
*sb
, unsigned int line
,
67 int type
, int blocks
, int rsv_blocks
)
72 trace_ext4_journal_start(sb
, blocks
, rsv_blocks
, _RET_IP_
);
73 err
= ext4_journal_check_start(sb
);
77 journal
= EXT4_SB(sb
)->s_journal
;
79 return ext4_get_nojournal();
80 return jbd2__journal_start(journal
, blocks
, rsv_blocks
, GFP_NOFS
,
84 int __ext4_journal_stop(const char *where
, unsigned int line
, handle_t
*handle
)
86 struct super_block
*sb
;
90 if (!ext4_handle_valid(handle
)) {
91 ext4_put_nojournal(handle
);
96 if (!handle
->h_transaction
) {
97 rc
= jbd2_journal_stop(handle
);
98 return err
? err
: rc
;
101 sb
= handle
->h_transaction
->t_journal
->j_private
;
102 rc
= jbd2_journal_stop(handle
);
107 __ext4_std_error(sb
, where
, line
, err
);
111 handle_t
*__ext4_journal_start_reserved(handle_t
*handle
, unsigned int line
,
114 struct super_block
*sb
;
117 if (!ext4_handle_valid(handle
))
118 return ext4_get_nojournal();
120 sb
= handle
->h_journal
->j_private
;
121 trace_ext4_journal_start_reserved(sb
, handle
->h_buffer_credits
,
123 err
= ext4_journal_check_start(sb
);
125 jbd2_journal_free_reserved(handle
);
129 err
= jbd2_journal_start_reserved(handle
, type
, line
);
135 static void ext4_journal_abort_handle(const char *caller
, unsigned int line
,
137 struct buffer_head
*bh
,
138 handle_t
*handle
, int err
)
141 const char *errstr
= ext4_decode_error(NULL
, err
, nbuf
);
143 BUG_ON(!ext4_handle_valid(handle
));
146 BUFFER_TRACE(bh
, "abort");
151 if (is_handle_aborted(handle
))
154 printk(KERN_ERR
"EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
155 caller
, line
, errstr
, err_fn
);
157 jbd2_journal_abort_handle(handle
);
160 int __ext4_journal_get_write_access(const char *where
, unsigned int line
,
161 handle_t
*handle
, struct buffer_head
*bh
)
167 if (ext4_handle_valid(handle
)) {
168 struct super_block
*sb
;
170 sb
= handle
->h_transaction
->t_journal
->j_private
;
171 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb
)))) {
172 jbd2_journal_abort_handle(handle
);
175 err
= jbd2_journal_get_write_access(handle
, bh
);
177 ext4_journal_abort_handle(where
, line
, __func__
, bh
,
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.
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.
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.
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
)
203 trace_ext4_forget(inode
, is_metadata
, blocknr
);
204 BUFFER_TRACE(bh
, "enter");
206 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
208 bh
, is_metadata
, inode
->i_mode
,
209 test_opt(inode
->i_sb
, DATA_FLAGS
));
211 /* In the no journal case, we can just do a bforget and return */
212 if (!ext4_handle_valid(handle
)) {
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
222 if (test_opt(inode
->i_sb
, DATA_FLAGS
) == EXT4_MOUNT_JOURNAL_DATA
||
223 (!is_metadata
&& !ext4_should_journal_data(inode
))) {
225 BUFFER_TRACE(bh
, "call jbd2_journal_forget");
226 err
= jbd2_journal_forget(handle
, bh
);
228 ext4_journal_abort_handle(where
, line
, __func__
,
236 * data!=journal && (is_metadata || should_journal_data(inode))
238 BUFFER_TRACE(bh
, "call jbd2_journal_revoke");
239 err
= jbd2_journal_revoke(handle
, blocknr
, bh
);
241 ext4_journal_abort_handle(where
, line
, __func__
,
243 __ext4_abort(inode
->i_sb
, where
, line
,
244 "error %d when attempting revoke", err
);
246 BUFFER_TRACE(bh
, "exit");
250 int __ext4_journal_get_create_access(const char *where
, unsigned int line
,
251 handle_t
*handle
, struct buffer_head
*bh
)
255 if (ext4_handle_valid(handle
)) {
256 err
= jbd2_journal_get_create_access(handle
, bh
);
258 ext4_journal_abort_handle(where
, line
, __func__
,
264 int __ext4_handle_dirty_metadata(const char *where
, unsigned int line
,
265 handle_t
*handle
, struct inode
*inode
,
266 struct buffer_head
*bh
)
274 if (ext4_handle_valid(handle
)) {
275 err
= jbd2_journal_dirty_metadata(handle
, bh
);
276 /* Errors can only happen due to aborted journal or a nasty bug */
277 if (!is_handle_aborted(handle
) && WARN_ON_ONCE(err
)) {
278 ext4_journal_abort_handle(where
, line
, __func__
, bh
,
281 pr_err("EXT4: jbd2_journal_dirty_metadata "
282 "failed: handle type %u started at "
283 "line %u, credits %u/%u, errcode %d",
286 handle
->h_requested_credits
,
287 handle
->h_buffer_credits
, err
);
290 ext4_error_inode(inode
, where
, line
,
292 "journal_dirty_metadata failed: "
293 "handle type %u started at line %u, "
294 "credits %u/%u, errcode %d",
297 handle
->h_requested_credits
,
298 handle
->h_buffer_credits
, err
);
302 mark_buffer_dirty_inode(bh
, inode
);
304 mark_buffer_dirty(bh
);
305 if (inode
&& inode_needs_sync(inode
)) {
306 sync_dirty_buffer(bh
);
307 if (buffer_req(bh
) && !buffer_uptodate(bh
)) {
308 struct ext4_super_block
*es
;
310 es
= EXT4_SB(inode
->i_sb
)->s_es
;
311 es
->s_last_error_block
=
312 cpu_to_le64(bh
->b_blocknr
);
313 ext4_error_inode(inode
, where
, line
,
315 "IO error syncing itable block");
323 int __ext4_handle_dirty_super(const char *where
, unsigned int line
,
324 handle_t
*handle
, struct super_block
*sb
)
326 struct buffer_head
*bh
= EXT4_SB(sb
)->s_sbh
;
329 ext4_superblock_csum_set(sb
);
330 if (ext4_handle_valid(handle
)) {
331 err
= jbd2_journal_dirty_metadata(handle
, bh
);
333 ext4_journal_abort_handle(where
, line
, __func__
,
336 mark_buffer_dirty(bh
);