blob: 451eb404533095fc323218fe5ea0f3caa0af8b16 [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 */
Theodore Ts'o9924a922013-02-08 21:59:22 -050041handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
42 int type, int nblocks)
Theodore Ts'o722887d2013-02-08 13:00:31 -050043{
44 journal_t *journal;
45
Theodore Ts'ob10a44c2013-04-03 22:00:52 -040046 might_sleep();
47
Theodore Ts'o722887d2013-02-08 13:00:31 -050048 trace_ext4_journal_start(sb, nblocks, _RET_IP_);
49 if (sb->s_flags & MS_RDONLY)
50 return ERR_PTR(-EROFS);
51
52 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
53 journal = EXT4_SB(sb)->s_journal;
54 if (!journal)
55 return ext4_get_nojournal();
56 /*
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.
60 */
61 if (is_journal_aborted(journal)) {
62 ext4_abort(sb, "Detected aborted journal");
63 return ERR_PTR(-EROFS);
64 }
Theodore Ts'o9924a922013-02-08 21:59:22 -050065 return jbd2__journal_start(journal, nblocks, GFP_NOFS, type, line);
Theodore Ts'o722887d2013-02-08 13:00:31 -050066}
67
68int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
69{
70 struct super_block *sb;
71 int err;
72 int rc;
73
74 if (!ext4_handle_valid(handle)) {
75 ext4_put_nojournal(handle);
76 return 0;
77 }
78 sb = handle->h_transaction->t_journal->j_private;
79 err = handle->h_err;
80 rc = jbd2_journal_stop(handle);
81
82 if (!err)
83 err = rc;
84 if (err)
85 __ext4_std_error(sb, where, line, err);
86 return err;
87}
88
89void ext4_journal_abort_handle(const char *caller, unsigned int line,
90 const char *err_fn, struct buffer_head *bh,
91 handle_t *handle, int err)
92{
93 char nbuf[16];
94 const char *errstr = ext4_decode_error(NULL, err, nbuf);
95
96 BUG_ON(!ext4_handle_valid(handle));
97
98 if (bh)
99 BUFFER_TRACE(bh, "abort");
100
101 if (!handle->h_err)
102 handle->h_err = err;
103
104 if (is_handle_aborted(handle))
105 return;
106
107 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
108 caller, line, errstr, err_fn);
109
110 jbd2_journal_abort_handle(handle);
111}
112
Theodore Ts'o90c72012010-06-29 14:53:24 -0400113int __ext4_journal_get_write_access(const char *where, unsigned int line,
114 handle_t *handle, struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800115{
Frank Mayhar03901312009-01-07 00:06:22 -0500116 int err = 0;
117
Theodore Ts'ob10a44c2013-04-03 22:00:52 -0400118 might_sleep();
119
Frank Mayhar03901312009-01-07 00:06:22 -0500120 if (ext4_handle_valid(handle)) {
121 err = jbd2_journal_get_write_access(handle, bh);
122 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400123 ext4_journal_abort_handle(where, line, __func__, bh,
Frank Mayhar03901312009-01-07 00:06:22 -0500124 handle, err);
125 }
Andrew Morton8984d132006-12-06 20:37:15 -0800126 return err;
127}
128
Theodore Ts'od6797d12009-11-22 20:52:12 -0500129/*
130 * The ext4 forget function must perform a revoke if we are freeing data
131 * which has been journaled. Metadata (eg. indirect blocks) must be
132 * revoked in all cases.
133 *
134 * "bh" may be NULL: a metadata block may have been freed from memory
135 * but there may still be a record of it in the journal, and that record
136 * still needs to be revoked.
137 *
138 * If the handle isn't valid we're not journaling, but we still need to
139 * call into ext4_journal_revoke() to put the buffer head.
140 */
Theodore Ts'o90c72012010-06-29 14:53:24 -0400141int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
142 int is_metadata, struct inode *inode,
143 struct buffer_head *bh, ext4_fsblk_t blocknr)
Theodore Ts'od6797d12009-11-22 20:52:12 -0500144{
145 int err;
146
147 might_sleep();
148
149 trace_ext4_forget(inode, is_metadata, blocknr);
150 BUFFER_TRACE(bh, "enter");
151
152 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
153 "data mode %x\n",
154 bh, is_metadata, inode->i_mode,
155 test_opt(inode->i_sb, DATA_FLAGS));
156
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500157 /* In the no journal case, we can just do a bforget and return */
158 if (!ext4_handle_valid(handle)) {
159 bforget(bh);
160 return 0;
161 }
162
Theodore Ts'od6797d12009-11-22 20:52:12 -0500163 /* Never use the revoke function if we are doing full data
164 * journaling: there is no need to, and a V1 superblock won't
165 * support it. Otherwise, only skip the revoke on un-journaled
166 * data blocks. */
167
168 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
169 (!is_metadata && !ext4_should_journal_data(inode))) {
170 if (bh) {
171 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500172 err = jbd2_journal_forget(handle, bh);
173 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400174 ext4_journal_abort_handle(where, line, __func__,
175 bh, handle, err);
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500176 return err;
Theodore Ts'od6797d12009-11-22 20:52:12 -0500177 }
178 return 0;
179 }
180
181 /*
182 * data!=journal && (is_metadata || should_journal_data(inode))
183 */
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500184 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
185 err = jbd2_journal_revoke(handle, blocknr, bh);
186 if (err) {
Theodore Ts'o90c72012010-06-29 14:53:24 -0400187 ext4_journal_abort_handle(where, line, __func__,
188 bh, handle, err);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400189 __ext4_abort(inode->i_sb, where, line,
190 "error %d when attempting revoke", err);
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500191 }
Theodore Ts'od6797d12009-11-22 20:52:12 -0500192 BUFFER_TRACE(bh, "exit");
193 return err;
194}
195
Theodore Ts'o90c72012010-06-29 14:53:24 -0400196int __ext4_journal_get_create_access(const char *where, unsigned int line,
Andrew Morton8984d132006-12-06 20:37:15 -0800197 handle_t *handle, struct buffer_head *bh)
198{
Frank Mayhar03901312009-01-07 00:06:22 -0500199 int err = 0;
200
201 if (ext4_handle_valid(handle)) {
202 err = jbd2_journal_get_create_access(handle, bh);
203 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400204 ext4_journal_abort_handle(where, line, __func__,
205 bh, handle, err);
Frank Mayhar03901312009-01-07 00:06:22 -0500206 }
Andrew Morton8984d132006-12-06 20:37:15 -0800207 return err;
208}
209
Theodore Ts'o90c72012010-06-29 14:53:24 -0400210int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
211 handle_t *handle, struct inode *inode,
212 struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800213{
Frank Mayhar03901312009-01-07 00:06:22 -0500214 int err = 0;
215
Theodore Ts'ob10a44c2013-04-03 22:00:52 -0400216 might_sleep();
217
Theodore Ts'o13fca322013-04-21 16:45:54 -0400218 set_buffer_meta(bh);
219 set_buffer_prio(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500220 if (ext4_handle_valid(handle)) {
221 err = jbd2_journal_dirty_metadata(handle, bh);
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400222 if (err) {
223 /* Errors can only happen if there is a bug */
224 handle->h_err = err;
225 __ext4_journal_stop(where, line, handle);
226 }
Frank Mayhar03901312009-01-07 00:06:22 -0500227 } else {
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -0500228 if (inode)
Theodore Ts'ofe188c02009-09-12 13:41:55 -0400229 mark_buffer_dirty_inode(bh, inode);
230 else
231 mark_buffer_dirty(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500232 if (inode && inode_needs_sync(inode)) {
233 sync_dirty_buffer(bh);
234 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400235 struct ext4_super_block *es;
236
237 es = EXT4_SB(inode->i_sb)->s_es;
238 es->s_last_error_block =
239 cpu_to_le64(bh->b_blocknr);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400240 ext4_error_inode(inode, where, line,
241 bh->b_blocknr,
242 "IO error syncing itable block");
Frank Mayhar03901312009-01-07 00:06:22 -0500243 err = -EIO;
244 }
245 }
246 }
Andrew Morton8984d132006-12-06 20:37:15 -0800247 return err;
248}
Theodore Ts'oa0375152010-06-11 23:14:04 -0400249
Theodore Ts'o90c72012010-06-29 14:53:24 -0400250int __ext4_handle_dirty_super(const char *where, unsigned int line,
Artem Bityutskiyb50924c2012-07-22 20:37:31 -0400251 handle_t *handle, struct super_block *sb)
Theodore Ts'oa0375152010-06-11 23:14:04 -0400252{
253 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
254 int err = 0;
255
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400256 ext4_superblock_csum_set(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400257 if (ext4_handle_valid(handle)) {
258 err = jbd2_journal_dirty_metadata(handle, bh);
259 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400260 ext4_journal_abort_handle(where, line, __func__,
261 bh, handle, err);
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400262 } else
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400263 mark_buffer_dirty(bh);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400264 return err;
265}