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 handle_t
*__ext4_journal_start_sb(struct super_block
*sb
, unsigned int line
,
42 int type
, int nblocks
)
48 trace_ext4_journal_start(sb
, nblocks
, _RET_IP_
);
49 if (sb
->s_flags
& MS_RDONLY
)
50 return ERR_PTR(-EROFS
);
52 WARN_ON(sb
->s_writers
.frozen
== SB_FREEZE_COMPLETE
);
53 journal
= EXT4_SB(sb
)->s_journal
;
55 return ext4_get_nojournal();
57 * Special case here: if the journal has aborted behind our
58 * backs (eg. EIO in the commit thread), then we still need to
59 * take the FS itself readonly cleanly.
61 if (is_journal_aborted(journal
)) {
62 ext4_abort(sb
, "Detected aborted journal");
63 return ERR_PTR(-EROFS
);
65 return jbd2__journal_start(journal
, nblocks
, GFP_NOFS
, type
, line
);
68 int __ext4_journal_stop(const char *where
, unsigned int line
, handle_t
*handle
)
70 struct super_block
*sb
;
74 if (!ext4_handle_valid(handle
)) {
75 ext4_put_nojournal(handle
);
80 if (!handle
->h_transaction
) {
81 rc
= jbd2_journal_stop(handle
);
82 return err
? err
: rc
;
85 sb
= handle
->h_transaction
->t_journal
->j_private
;
86 rc
= jbd2_journal_stop(handle
);
91 __ext4_std_error(sb
, where
, line
, err
);
95 void ext4_journal_abort_handle(const char *caller
, unsigned int line
,
96 const char *err_fn
, struct buffer_head
*bh
,
97 handle_t
*handle
, int err
)
100 const char *errstr
= ext4_decode_error(NULL
, err
, nbuf
);
102 BUG_ON(!ext4_handle_valid(handle
));
105 BUFFER_TRACE(bh
, "abort");
110 if (is_handle_aborted(handle
))
113 printk(KERN_ERR
"EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
114 caller
, line
, errstr
, err_fn
);
116 jbd2_journal_abort_handle(handle
);
119 int __ext4_journal_get_write_access(const char *where
, unsigned int line
,
120 handle_t
*handle
, struct buffer_head
*bh
)
126 if (ext4_handle_valid(handle
)) {
127 err
= jbd2_journal_get_write_access(handle
, bh
);
129 ext4_journal_abort_handle(where
, line
, __func__
, bh
,
136 * The ext4 forget function must perform a revoke if we are freeing data
137 * which has been journaled. Metadata (eg. indirect blocks) must be
138 * revoked in all cases.
140 * "bh" may be NULL: a metadata block may have been freed from memory
141 * but there may still be a record of it in the journal, and that record
142 * still needs to be revoked.
144 * If the handle isn't valid we're not journaling, but we still need to
145 * call into ext4_journal_revoke() to put the buffer head.
147 int __ext4_forget(const char *where
, unsigned int line
, handle_t
*handle
,
148 int is_metadata
, struct inode
*inode
,
149 struct buffer_head
*bh
, ext4_fsblk_t blocknr
)
155 trace_ext4_forget(inode
, is_metadata
, blocknr
);
156 BUFFER_TRACE(bh
, "enter");
158 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
160 bh
, is_metadata
, inode
->i_mode
,
161 test_opt(inode
->i_sb
, DATA_FLAGS
));
163 /* In the no journal case, we can just do a bforget and return */
164 if (!ext4_handle_valid(handle
)) {
169 /* Never use the revoke function if we are doing full data
170 * journaling: there is no need to, and a V1 superblock won't
171 * support it. Otherwise, only skip the revoke on un-journaled
174 if (test_opt(inode
->i_sb
, DATA_FLAGS
) == EXT4_MOUNT_JOURNAL_DATA
||
175 (!is_metadata
&& !ext4_should_journal_data(inode
))) {
177 BUFFER_TRACE(bh
, "call jbd2_journal_forget");
178 err
= jbd2_journal_forget(handle
, bh
);
180 ext4_journal_abort_handle(where
, line
, __func__
,
188 * data!=journal && (is_metadata || should_journal_data(inode))
190 BUFFER_TRACE(bh
, "call jbd2_journal_revoke");
191 err
= jbd2_journal_revoke(handle
, blocknr
, bh
);
193 ext4_journal_abort_handle(where
, line
, __func__
,
195 __ext4_abort(inode
->i_sb
, where
, line
,
196 "error %d when attempting revoke", err
);
198 BUFFER_TRACE(bh
, "exit");
202 int __ext4_journal_get_create_access(const char *where
, unsigned int line
,
203 handle_t
*handle
, struct buffer_head
*bh
)
207 if (ext4_handle_valid(handle
)) {
208 err
= jbd2_journal_get_create_access(handle
, bh
);
210 ext4_journal_abort_handle(where
, line
, __func__
,
216 int __ext4_handle_dirty_metadata(const char *where
, unsigned int line
,
217 handle_t
*handle
, struct inode
*inode
,
218 struct buffer_head
*bh
)
226 if (ext4_handle_valid(handle
)) {
227 err
= jbd2_journal_dirty_metadata(handle
, bh
);
228 /* Errors can only happen if there is a bug */
229 if (WARN_ON_ONCE(err
)) {
230 ext4_journal_abort_handle(where
, line
, __func__
, bh
,
232 ext4_error_inode(inode
, where
, line
,
234 "journal_dirty_metadata failed: "
235 "handle type %u started at line %u, "
236 "credits %u/%u, errcode %d",
239 handle
->h_requested_credits
,
240 handle
->h_buffer_credits
, err
);
244 mark_buffer_dirty_inode(bh
, inode
);
246 mark_buffer_dirty(bh
);
247 if (inode
&& inode_needs_sync(inode
)) {
248 sync_dirty_buffer(bh
);
249 if (buffer_req(bh
) && !buffer_uptodate(bh
)) {
250 struct ext4_super_block
*es
;
252 es
= EXT4_SB(inode
->i_sb
)->s_es
;
253 es
->s_last_error_block
=
254 cpu_to_le64(bh
->b_blocknr
);
255 ext4_error_inode(inode
, where
, line
,
257 "IO error syncing itable block");
265 int __ext4_handle_dirty_super(const char *where
, unsigned int line
,
266 handle_t
*handle
, struct super_block
*sb
)
268 struct buffer_head
*bh
= EXT4_SB(sb
)->s_sbh
;
271 ext4_superblock_csum_set(sb
);
272 if (ext4_handle_valid(handle
)) {
273 err
= jbd2_journal_dirty_metadata(handle
, bh
);
275 ext4_journal_abort_handle(where
, line
, __func__
,
278 mark_buffer_dirty(bh
);