blob: 6f61145255356a71ecb77644fa95566da9b3af4d [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 */
41handle_t *ext4_journal_start_sb(struct super_block *sb, int nblocks)
42{
43 journal_t *journal;
44
45 trace_ext4_journal_start(sb, nblocks, _RET_IP_);
46 if (sb->s_flags & MS_RDONLY)
47 return ERR_PTR(-EROFS);
48
49 WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
50 journal = EXT4_SB(sb)->s_journal;
51 if (!journal)
52 return ext4_get_nojournal();
53 /*
54 * Special case here: if the journal has aborted behind our
55 * backs (eg. EIO in the commit thread), then we still need to
56 * take the FS itself readonly cleanly.
57 */
58 if (is_journal_aborted(journal)) {
59 ext4_abort(sb, "Detected aborted journal");
60 return ERR_PTR(-EROFS);
61 }
62 return jbd2_journal_start(journal, nblocks);
63}
64
65int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
66{
67 struct super_block *sb;
68 int err;
69 int rc;
70
71 if (!ext4_handle_valid(handle)) {
72 ext4_put_nojournal(handle);
73 return 0;
74 }
75 sb = handle->h_transaction->t_journal->j_private;
76 err = handle->h_err;
77 rc = jbd2_journal_stop(handle);
78
79 if (!err)
80 err = rc;
81 if (err)
82 __ext4_std_error(sb, where, line, err);
83 return err;
84}
85
86void ext4_journal_abort_handle(const char *caller, unsigned int line,
87 const char *err_fn, struct buffer_head *bh,
88 handle_t *handle, int err)
89{
90 char nbuf[16];
91 const char *errstr = ext4_decode_error(NULL, err, nbuf);
92
93 BUG_ON(!ext4_handle_valid(handle));
94
95 if (bh)
96 BUFFER_TRACE(bh, "abort");
97
98 if (!handle->h_err)
99 handle->h_err = err;
100
101 if (is_handle_aborted(handle))
102 return;
103
104 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
105 caller, line, errstr, err_fn);
106
107 jbd2_journal_abort_handle(handle);
108}
109
Theodore Ts'o90c72012010-06-29 14:53:24 -0400110int __ext4_journal_get_write_access(const char *where, unsigned int line,
111 handle_t *handle, struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800112{
Frank Mayhar03901312009-01-07 00:06:22 -0500113 int err = 0;
114
115 if (ext4_handle_valid(handle)) {
116 err = jbd2_journal_get_write_access(handle, bh);
117 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400118 ext4_journal_abort_handle(where, line, __func__, bh,
Frank Mayhar03901312009-01-07 00:06:22 -0500119 handle, err);
120 }
Andrew Morton8984d132006-12-06 20:37:15 -0800121 return err;
122}
123
Theodore Ts'od6797d12009-11-22 20:52:12 -0500124/*
125 * The ext4 forget function must perform a revoke if we are freeing data
126 * which has been journaled. Metadata (eg. indirect blocks) must be
127 * revoked in all cases.
128 *
129 * "bh" may be NULL: a metadata block may have been freed from memory
130 * but there may still be a record of it in the journal, and that record
131 * still needs to be revoked.
132 *
133 * If the handle isn't valid we're not journaling, but we still need to
134 * call into ext4_journal_revoke() to put the buffer head.
135 */
Theodore Ts'o90c72012010-06-29 14:53:24 -0400136int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
137 int is_metadata, struct inode *inode,
138 struct buffer_head *bh, ext4_fsblk_t blocknr)
Theodore Ts'od6797d12009-11-22 20:52:12 -0500139{
140 int err;
141
142 might_sleep();
143
144 trace_ext4_forget(inode, is_metadata, blocknr);
145 BUFFER_TRACE(bh, "enter");
146
147 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
148 "data mode %x\n",
149 bh, is_metadata, inode->i_mode,
150 test_opt(inode->i_sb, DATA_FLAGS));
151
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500152 /* In the no journal case, we can just do a bforget and return */
153 if (!ext4_handle_valid(handle)) {
154 bforget(bh);
155 return 0;
156 }
157
Theodore Ts'od6797d12009-11-22 20:52:12 -0500158 /* Never use the revoke function if we are doing full data
159 * journaling: there is no need to, and a V1 superblock won't
160 * support it. Otherwise, only skip the revoke on un-journaled
161 * data blocks. */
162
163 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
164 (!is_metadata && !ext4_should_journal_data(inode))) {
165 if (bh) {
166 BUFFER_TRACE(bh, "call jbd2_journal_forget");
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500167 err = jbd2_journal_forget(handle, bh);
168 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400169 ext4_journal_abort_handle(where, line, __func__,
170 bh, handle, err);
Theodore Ts'ob7e57e72009-11-22 21:00:13 -0500171 return err;
Theodore Ts'od6797d12009-11-22 20:52:12 -0500172 }
173 return 0;
174 }
175
176 /*
177 * data!=journal && (is_metadata || should_journal_data(inode))
178 */
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500179 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
180 err = jbd2_journal_revoke(handle, blocknr, bh);
181 if (err) {
Theodore Ts'o90c72012010-06-29 14:53:24 -0400182 ext4_journal_abort_handle(where, line, __func__,
183 bh, handle, err);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400184 __ext4_abort(inode->i_sb, where, line,
185 "error %d when attempting revoke", err);
Theodore Ts'oe4684b32009-11-24 11:05:59 -0500186 }
Theodore Ts'od6797d12009-11-22 20:52:12 -0500187 BUFFER_TRACE(bh, "exit");
188 return err;
189}
190
Theodore Ts'o90c72012010-06-29 14:53:24 -0400191int __ext4_journal_get_create_access(const char *where, unsigned int line,
Andrew Morton8984d132006-12-06 20:37:15 -0800192 handle_t *handle, struct buffer_head *bh)
193{
Frank Mayhar03901312009-01-07 00:06:22 -0500194 int err = 0;
195
196 if (ext4_handle_valid(handle)) {
197 err = jbd2_journal_get_create_access(handle, bh);
198 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400199 ext4_journal_abort_handle(where, line, __func__,
200 bh, handle, err);
Frank Mayhar03901312009-01-07 00:06:22 -0500201 }
Andrew Morton8984d132006-12-06 20:37:15 -0800202 return err;
203}
204
Theodore Ts'o90c72012010-06-29 14:53:24 -0400205int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
206 handle_t *handle, struct inode *inode,
207 struct buffer_head *bh)
Andrew Morton8984d132006-12-06 20:37:15 -0800208{
Frank Mayhar03901312009-01-07 00:06:22 -0500209 int err = 0;
210
211 if (ext4_handle_valid(handle)) {
212 err = jbd2_journal_dirty_metadata(handle, bh);
Theodore Ts'o9ea7a0d2011-09-04 10:18:14 -0400213 if (err) {
214 /* Errors can only happen if there is a bug */
215 handle->h_err = err;
216 __ext4_journal_stop(where, line, handle);
217 }
Frank Mayhar03901312009-01-07 00:06:22 -0500218 } else {
Curt Wohlgemuth73b50c12010-02-16 15:06:29 -0500219 if (inode)
Theodore Ts'ofe188c02009-09-12 13:41:55 -0400220 mark_buffer_dirty_inode(bh, inode);
221 else
222 mark_buffer_dirty(bh);
Frank Mayhar03901312009-01-07 00:06:22 -0500223 if (inode && inode_needs_sync(inode)) {
224 sync_dirty_buffer(bh);
225 if (buffer_req(bh) && !buffer_uptodate(bh)) {
Theodore Ts'o1c13d5c2010-07-27 11:56:03 -0400226 struct ext4_super_block *es;
227
228 es = EXT4_SB(inode->i_sb)->s_es;
229 es->s_last_error_block =
230 cpu_to_le64(bh->b_blocknr);
Theodore Ts'oc398eda2010-07-27 11:56:40 -0400231 ext4_error_inode(inode, where, line,
232 bh->b_blocknr,
233 "IO error syncing itable block");
Frank Mayhar03901312009-01-07 00:06:22 -0500234 err = -EIO;
235 }
236 }
237 }
Andrew Morton8984d132006-12-06 20:37:15 -0800238 return err;
239}
Theodore Ts'oa0375152010-06-11 23:14:04 -0400240
Theodore Ts'o90c72012010-06-29 14:53:24 -0400241int __ext4_handle_dirty_super(const char *where, unsigned int line,
Artem Bityutskiyb50924c2012-07-22 20:37:31 -0400242 handle_t *handle, struct super_block *sb)
Theodore Ts'oa0375152010-06-11 23:14:04 -0400243{
244 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
245 int err = 0;
246
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400247 ext4_superblock_csum_set(sb);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400248 if (ext4_handle_valid(handle)) {
249 err = jbd2_journal_dirty_metadata(handle, bh);
250 if (err)
Theodore Ts'o90c72012010-06-29 14:53:24 -0400251 ext4_journal_abort_handle(where, line, __func__,
252 bh, handle, err);
Theodore Ts'o06db49e2012-10-10 01:06:58 -0400253 } else
Darrick J. Wonga9c473172012-04-29 18:29:10 -0400254 mark_buffer_dirty(bh);
Theodore Ts'oa0375152010-06-11 23:14:04 -0400255 return err;
256}