]>
Commit | Line | Data |
---|---|---|
8984d137 AM |
1 | /* |
2 | * Interface between ext4 and JBD | |
3 | */ | |
4 | ||
3dcf5451 | 5 | #include "ext4_jbd2.h" |
8984d137 | 6 | |
d6797d14 TT |
7 | #include <trace/events/ext4.h> |
8 | ||
722887dd TT |
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 | */ | |
5fe2fe89 | 41 | static int ext4_journal_check_start(struct super_block *sb) |
722887dd TT |
42 | { |
43 | journal_t *journal; | |
44 | ||
b10a44c3 | 45 | might_sleep(); |
0db1ff22 TT |
46 | |
47 | if (unlikely(ext4_forced_shutdown(EXT4_SB(sb)))) | |
48 | return -EIO; | |
49 | ||
bc98a42c | 50 | if (sb_rdonly(sb)) |
5fe2fe89 | 51 | return -EROFS; |
722887dd TT |
52 | WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE); |
53 | journal = EXT4_SB(sb)->s_journal; | |
722887dd TT |
54 | /* |
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 | */ | |
5fe2fe89 | 59 | if (journal && is_journal_aborted(journal)) { |
722887dd | 60 | ext4_abort(sb, "Detected aborted journal"); |
5fe2fe89 | 61 | return -EROFS; |
722887dd | 62 | } |
5fe2fe89 JK |
63 | return 0; |
64 | } | |
65 | ||
66 | handle_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); | |
722887dd TT |
82 | } |
83 | ||
84 | int __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 | } | |
9d506594 | 94 | |
6934da92 | 95 | err = handle->h_err; |
9d506594 | 96 | if (!handle->h_transaction) { |
6934da92 LC |
97 | rc = jbd2_journal_stop(handle); |
98 | return err ? err : rc; | |
9d506594 LC |
99 | } |
100 | ||
722887dd | 101 | sb = handle->h_transaction->t_journal->j_private; |
722887dd TT |
102 | 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 | ||
5fe2fe89 JK |
111 | handle_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 | ||
c197855e SH |
135 | static 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) | |
722887dd TT |
139 | { |
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 | ||
90c7201b TT |
160 | int __ext4_journal_get_write_access(const char *where, unsigned int line, |
161 | handle_t *handle, struct buffer_head *bh) | |
8984d137 | 162 | { |
0390131b FM |
163 | int err = 0; |
164 | ||
b10a44c3 TT |
165 | might_sleep(); |
166 | ||
0390131b | 167 | if (ext4_handle_valid(handle)) { |
0db1ff22 TT |
168 | 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 | } | |
0390131b FM |
175 | err = jbd2_journal_get_write_access(handle, bh); |
176 | if (err) | |
90c7201b | 177 | ext4_journal_abort_handle(where, line, __func__, bh, |
0390131b FM |
178 | handle, err); |
179 | } | |
8984d137 AM |
180 | return err; |
181 | } | |
182 | ||
d6797d14 TT |
183 | /* |
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 | */ | |
90c7201b TT |
195 | int __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) | |
d6797d14 TT |
198 | { |
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 | ||
e4684b3f TT |
211 | /* 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 | ||
d6797d14 TT |
217 | /* 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"); | |
b7e57e7c TT |
226 | err = jbd2_journal_forget(handle, bh); |
227 | if (err) | |
90c7201b TT |
228 | ext4_journal_abort_handle(where, line, __func__, |
229 | bh, handle, err); | |
b7e57e7c | 230 | return err; |
d6797d14 TT |
231 | } |
232 | return 0; | |
233 | } | |
234 | ||
235 | /* | |
236 | * data!=journal && (is_metadata || should_journal_data(inode)) | |
237 | */ | |
e4684b3f TT |
238 | BUFFER_TRACE(bh, "call jbd2_journal_revoke"); |
239 | err = jbd2_journal_revoke(handle, blocknr, bh); | |
240 | if (err) { | |
90c7201b TT |
241 | ext4_journal_abort_handle(where, line, __func__, |
242 | bh, handle, err); | |
c398eda0 TT |
243 | __ext4_abort(inode->i_sb, where, line, |
244 | "error %d when attempting revoke", err); | |
e4684b3f | 245 | } |
d6797d14 TT |
246 | BUFFER_TRACE(bh, "exit"); |
247 | return err; | |
248 | } | |
249 | ||
90c7201b | 250 | int __ext4_journal_get_create_access(const char *where, unsigned int line, |
8984d137 AM |
251 | handle_t *handle, struct buffer_head *bh) |
252 | { | |
0390131b FM |
253 | int err = 0; |
254 | ||
255 | if (ext4_handle_valid(handle)) { | |
256 | err = jbd2_journal_get_create_access(handle, bh); | |
257 | if (err) | |
90c7201b TT |
258 | ext4_journal_abort_handle(where, line, __func__, |
259 | bh, handle, err); | |
0390131b | 260 | } |
8984d137 AM |
261 | return err; |
262 | } | |
263 | ||
90c7201b TT |
264 | int __ext4_handle_dirty_metadata(const char *where, unsigned int line, |
265 | handle_t *handle, struct inode *inode, | |
266 | struct buffer_head *bh) | |
8984d137 | 267 | { |
0390131b FM |
268 | int err = 0; |
269 | ||
b10a44c3 TT |
270 | might_sleep(); |
271 | ||
13fca323 TT |
272 | set_buffer_meta(bh); |
273 | set_buffer_prio(bh); | |
0390131b FM |
274 | if (ext4_handle_valid(handle)) { |
275 | err = jbd2_journal_dirty_metadata(handle, bh); | |
c5d31192 DM |
276 | /* Errors can only happen due to aborted journal or a nasty bug */ |
277 | if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) { | |
91aa11fa JK |
278 | ext4_journal_abort_handle(where, line, __func__, bh, |
279 | handle, err); | |
66a4cb18 TT |
280 | 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 | } | |
ae1495b1 TT |
290 | 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); | |
9ea7a0df | 299 | } |
0390131b | 300 | } else { |
73b50c1c | 301 | if (inode) |
fe188c0e TT |
302 | mark_buffer_dirty_inode(bh, inode); |
303 | else | |
304 | mark_buffer_dirty(bh); | |
0390131b FM |
305 | if (inode && inode_needs_sync(inode)) { |
306 | sync_dirty_buffer(bh); | |
307 | if (buffer_req(bh) && !buffer_uptodate(bh)) { | |
1c13d5c0 TT |
308 | 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); | |
c398eda0 TT |
313 | ext4_error_inode(inode, where, line, |
314 | bh->b_blocknr, | |
315 | "IO error syncing itable block"); | |
0390131b FM |
316 | err = -EIO; |
317 | } | |
318 | } | |
319 | } | |
8984d137 AM |
320 | return err; |
321 | } | |
a0375156 | 322 | |
90c7201b | 323 | int __ext4_handle_dirty_super(const char *where, unsigned int line, |
b50924c2 | 324 | handle_t *handle, struct super_block *sb) |
a0375156 TT |
325 | { |
326 | struct buffer_head *bh = EXT4_SB(sb)->s_sbh; | |
327 | int err = 0; | |
328 | ||
06db49e6 | 329 | ext4_superblock_csum_set(sb); |
a0375156 TT |
330 | if (ext4_handle_valid(handle)) { |
331 | err = jbd2_journal_dirty_metadata(handle, bh); | |
332 | if (err) | |
90c7201b TT |
333 | ext4_journal_abort_handle(where, line, __func__, |
334 | bh, handle, err); | |
06db49e6 | 335 | } else |
a9c47317 | 336 | mark_buffer_dirty(bh); |
a0375156 TT |
337 | return err; |
338 | } |