blob: dd106b1d5d89155d850f5320b7739342455959bd [file] [log] [blame]
Andrew Morton8984d132006-12-06 20:37:15 -08001/*
2 * Interface between ext4 and JBD
3 */
4
Christoph Hellwig3dcf5452008-04-29 18:13:32 -04005#include "ext4_jbd2.h"
Andrew Morton8984d132006-12-06 20:37:15 -08006
Theodore Ts'od6797d12009-11-22 20:52:12 -05007#include <trace/events/ext4.h>
8
Theodore Ts'o722887d2013-02-08 13:00:31 -05009/* Just increment the non-pointer handle value */
10static 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 */
26static 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 Kara5fe2fe82013-06-04 12:37:50 -040041static int ext4_journal_check_start(struct super_block *sb)
Theodore Ts'o722887d2013-02-08 13:00:31 -050042{
43 journal_t *journal;
44
Theodore Ts'ob10a44c2013-04-03 22:00:52 -040045 might_sleep();
Theodore Ts'o0db1ff22017-02-05 01:28:48 -050046
47 if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
48 return -EIO;
49
Theodore Ts'o722887d2013-02-08 13:00:31 -050050 if (sb->s_flags & MS_RDONLY)
Jan Kara5fe2fe82013-06-04 12:37:50 -040051 return -EROFS;
Theodore Ts'o722887d2013-02-08 13:00:31 -050052 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
53 journal = EXT4_SB(sb)->s_journal;
Theodore Ts'o722887d2013-02-08 13:00:31 -050054 /*
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 Kara5fe2fe82013-06-04 12:37:50 -040059 if (journal && is_journal_aborted(journal)) {
Theodore Ts'o722887d2013-02-08 13:00:31 -050060 ext4_abort(sb, "Detected aborted journal");
Jan Kara5fe2fe82013-06-04 12:37:50 -040061 return -EROFS;
Theodore Ts'o722887d2013-02-08 13:00:31 -050062 }
Jan Kara5fe2fe82013-06-04 12:37:50 -040063 return 0;
64}
65
66handle_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'o722887d2013-02-08 13:00:31 -050082}
83
84int __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 Czerner9d506592015-05-14 18:55:18 -040094
Lukas Czerner6934da92015-10-17 22:57:06 -040095 err = handle->h_err;
Lukas Czerner9d506592015-05-14 18:55:18 -040096 if (!handle->h_transaction) {
Lukas Czerner6934da92015-10-17 22:57:06 -040097 rc = jbd2_journal_stop(handle);
98 return err ? err : rc;
Lukas Czerner9d506592015-05-14 18:55:18 -040099 }
100
Theodore Ts'o722887d2013-02-08 13:00:31 -0500101 sb = handle->h_transaction->t_journal->j_private;
Theodore Ts'o722887d2013-02-08 13:00:31 -0500102 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 Kara5fe2fe82013-06-04 12:37:50 -0400111handle_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 Hemmingerc1978552014-05-12 10:50:23 -0400135static 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'o722887d2013-02-08 13:00:31 -0500139{
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'o90c72012010-06-29 14:53:24 -0400160int __ext4_journal_get_write_access(const char *where, unsigned int line,
161 handle_t *handle, struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800162{
Frank Mayhar03901312009-01-07 00:06:22 -0500163 int err = 0;
164
Theodore Ts'ob10a44c2013-04-03 22:00:52 -0400165 might_sleep();
166
Frank Mayhar03901312009-01-07 00:06:22 -0500167 if (ext4_handle_valid(handle)) {
Theodore Ts'o0db1ff22017-02-05 01:28:48 -0500168 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 Mayhar03901312009-01-07 00:06:22 -0500175 err = jbd2_journal_get_write_access(handle, bh);
176 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400177 ext4_journal_abort_handle(where, line, __func__, bh,
Frank Mayhar03901312009-01-07 00:06:22 -0500178 handle, err);
179 }
Andrew Morton8984d132006-12-06 20:37:15 -0800180 return err;
181}
182
Theodore Ts'od6797d12009-11-22 20:52:12 -0500183/*
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'o90c72012010-06-29 14:53:24 -0400195int __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'od6797d12009-11-22 20:52:12 -0500198{
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'oe4684b32009-11-24 11:05:59 -0500211 /* 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'od6797d12009-11-22 20:52:12 -0500217 /* 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'ob7e57e72009-11-22 21:00:13 -0500226 err = jbd2_journal_forget(handle, bh);
227 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400228 ext4_journal_abort_handle(where, line, __func__,
229 bh, handle, err);
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500230 return err;
Theodore Ts'od6797d12009-11-22 20:52:12 -0500231 }
232 return 0;
233 }
234
235 /*
236 * data!=journal && (is_metadata || should_journal_data(inode))
237 */
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500238 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
239 err = jbd2_journal_revoke(handle, blocknr, bh);
240 if (err) {
Theodore Ts'o90c72012010-06-29 14:53:24 -0400241 ext4_journal_abort_handle(where, line, __func__,
242 bh, handle, err);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400243 __ext4_abort(inode->i_sb, where, line,
244 "error %d when attempting revoke", err);
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500245 }
Theodore Ts'od6797d12009-11-22 20:52:12 -0500246 BUFFER_TRACE(bh, "exit");
247 return err;
248}
249
Theodore Ts'o90c72012010-06-29 14:53:24 -0400250int __ext4_journal_get_create_access(const char *where, unsigned int line,
Andrew Morton8984d132006-12-06 20:37:15 -0800251 handle_t *handle, struct buffer_head *bh)
252{
Frank Mayhar03901312009-01-07 00:06:22 -0500253 int err = 0;
254
255 if (ext4_handle_valid(handle)) {
256 err = jbd2_journal_get_create_access(handle, bh);
257 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400258 ext4_journal_abort_handle(where, line, __func__,
259 bh, handle, err);
Frank Mayhar03901312009-01-07 00:06:22 -0500260 }
Andrew Morton8984d132006-12-06 20:37:15 -0800261 return err;
262}
263
Theodore Ts'o90c72012010-06-29 14:53:24 -0400264int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
265 handle_t *handle, struct inode *inode,
266 struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800267{
Frank Mayhar03901312009-01-07 00:06:22 -0500268 int err = 0;
269
Theodore Ts'ob10a44c2013-04-03 22:00:52 -0400270 might_sleep();
271
Theodore Ts'o13fca322013-04-21 16:45:54 -0400272 set_buffer_meta(bh);
273 set_buffer_prio(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500274 if (ext4_handle_valid(handle)) {
275 err = jbd2_journal_dirty_metadata(handle, bh);
Dmitry Monakhovc5d31192014-10-01 22:23:15 -0400276 /* Errors can only happen due to aborted journal or a nasty bug */
277 if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) {
Jan Kara91aa11f2013-08-12 09:53:28 -0400278 ext4_journal_abort_handle(where, line, __func__, bh,
279 handle, err);
Theodore Ts'o66a4cb12014-03-12 16:38:03 -0400280 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'oae1495b2013-12-02 09:31:36 -0500290 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'o9ea7a0d2011-09-04 10:18:14 -0400299 }
Frank Mayhar03901312009-01-07 00:06:22 -0500300 } else {
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -0500301 if (inode)
Theodore Ts'ofe188c02009-09-12 13:41:55 -0400302 mark_buffer_dirty_inode(bh, inode);
303 else
304 mark_buffer_dirty(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500305 if (inode && inode_needs_sync(inode)) {
306 sync_dirty_buffer(bh);
307 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400308 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'oc398eda2010-07-27 11:56:40 -0400313 ext4_error_inode(inode, where, line,
314 bh->b_blocknr,
315 "IO error syncing itable block");
Frank Mayhar03901312009-01-07 00:06:22 -0500316 err = -EIO;
317 }
318 }
319 }
Andrew Morton8984d132006-12-06 20:37:15 -0800320 return err;
321}
Theodore Ts'oa0375152010-06-11 23:14:04 -0400322
Theodore Ts'o90c72012010-06-29 14:53:24 -0400323int __ext4_handle_dirty_super(const char *where, unsigned int line,
Artem Bityutskiyb50924c2012-07-22 20:37:31 -0400324 handle_t *handle, struct super_block *sb)
Theodore Ts'oa0375152010-06-11 23:14:04 -0400325{
326 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
327 int err = 0;
328
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400329 ext4_superblock_csum_set(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400330 if (ext4_handle_valid(handle)) {
331 err = jbd2_journal_dirty_metadata(handle, bh);
332 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400333 ext4_journal_abort_handle(where, line, __func__,
334 bh, handle, err);
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400335 } else
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400336 mark_buffer_dirty(bh);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400337 return err;
338}