import PULS_20180308
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / ext4 / ext4_jbd2.c
1 /*
2 * Interface between ext4 and JBD
3 */
4
5 #include "ext4_jbd2.h"
6
7 #include <trace/events/ext4.h>
8
9 /* Just increment the non-pointer handle value */
10 static 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 */
26 static 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 */
41 handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
42 int type, int nblocks)
43 {
44 journal_t *journal;
45
46 might_sleep();
47
48 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 }
65 return jbd2__journal_start(journal, nblocks, GFP_NOFS, type, line);
66 }
67
68 int __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
79 err = handle->h_err;
80 if (!handle->h_transaction) {
81 rc = jbd2_journal_stop(handle);
82 return err ? err : rc;
83 }
84
85 sb = handle->h_transaction->t_journal->j_private;
86 rc = jbd2_journal_stop(handle);
87
88 if (!err)
89 err = rc;
90 if (err)
91 __ext4_std_error(sb, where, line, err);
92 return err;
93 }
94
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)
98 {
99 char nbuf[16];
100 const char *errstr = ext4_decode_error(NULL, err, nbuf);
101
102 BUG_ON(!ext4_handle_valid(handle));
103
104 if (bh)
105 BUFFER_TRACE(bh, "abort");
106
107 if (!handle->h_err)
108 handle->h_err = err;
109
110 if (is_handle_aborted(handle))
111 return;
112
113 printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
114 caller, line, errstr, err_fn);
115
116 jbd2_journal_abort_handle(handle);
117 }
118
119 int __ext4_journal_get_write_access(const char *where, unsigned int line,
120 handle_t *handle, struct buffer_head *bh)
121 {
122 int err = 0;
123
124 might_sleep();
125
126 if (ext4_handle_valid(handle)) {
127 err = jbd2_journal_get_write_access(handle, bh);
128 if (err)
129 ext4_journal_abort_handle(where, line, __func__, bh,
130 handle, err);
131 }
132 return err;
133 }
134
135 /*
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.
139 *
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.
143 *
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.
146 */
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)
150 {
151 int err;
152
153 might_sleep();
154
155 trace_ext4_forget(inode, is_metadata, blocknr);
156 BUFFER_TRACE(bh, "enter");
157
158 jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
159 "data mode %x\n",
160 bh, is_metadata, inode->i_mode,
161 test_opt(inode->i_sb, DATA_FLAGS));
162
163 /* In the no journal case, we can just do a bforget and return */
164 if (!ext4_handle_valid(handle)) {
165 bforget(bh);
166 return 0;
167 }
168
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
172 * data blocks. */
173
174 if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
175 (!is_metadata && !ext4_should_journal_data(inode))) {
176 if (bh) {
177 BUFFER_TRACE(bh, "call jbd2_journal_forget");
178 err = jbd2_journal_forget(handle, bh);
179 if (err)
180 ext4_journal_abort_handle(where, line, __func__,
181 bh, handle, err);
182 return err;
183 }
184 return 0;
185 }
186
187 /*
188 * data!=journal && (is_metadata || should_journal_data(inode))
189 */
190 BUFFER_TRACE(bh, "call jbd2_journal_revoke");
191 err = jbd2_journal_revoke(handle, blocknr, bh);
192 if (err) {
193 ext4_journal_abort_handle(where, line, __func__,
194 bh, handle, err);
195 __ext4_abort(inode->i_sb, where, line,
196 "error %d when attempting revoke", err);
197 }
198 BUFFER_TRACE(bh, "exit");
199 return err;
200 }
201
202 int __ext4_journal_get_create_access(const char *where, unsigned int line,
203 handle_t *handle, struct buffer_head *bh)
204 {
205 int err = 0;
206
207 if (ext4_handle_valid(handle)) {
208 err = jbd2_journal_get_create_access(handle, bh);
209 if (err)
210 ext4_journal_abort_handle(where, line, __func__,
211 bh, handle, err);
212 }
213 return err;
214 }
215
216 int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
217 handle_t *handle, struct inode *inode,
218 struct buffer_head *bh)
219 {
220 int err = 0;
221
222 might_sleep();
223
224 set_buffer_meta(bh);
225 set_buffer_prio(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,
231 handle, err);
232 ext4_error_inode(inode, where, line,
233 bh->b_blocknr,
234 "journal_dirty_metadata failed: "
235 "handle type %u started at line %u, "
236 "credits %u/%u, errcode %d",
237 handle->h_type,
238 handle->h_line_no,
239 handle->h_requested_credits,
240 handle->h_buffer_credits, err);
241 }
242 } else {
243 if (inode)
244 mark_buffer_dirty_inode(bh, inode);
245 else
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;
251
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,
256 bh->b_blocknr,
257 "IO error syncing itable block");
258 err = -EIO;
259 }
260 }
261 }
262 return err;
263 }
264
265 int __ext4_handle_dirty_super(const char *where, unsigned int line,
266 handle_t *handle, struct super_block *sb)
267 {
268 struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
269 int err = 0;
270
271 ext4_superblock_csum_set(sb);
272 if (ext4_handle_valid(handle)) {
273 err = jbd2_journal_dirty_metadata(handle, bh);
274 if (err)
275 ext4_journal_abort_handle(where, line, __func__,
276 bh, handle, err);
277 } else
278 mark_buffer_dirty(bh);
279 return err;
280 }