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 | */ | |
9924a92a TT |
41 | handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line, |
42 | int type, int nblocks) | |
722887dd TT |
43 | { |
44 | journal_t *journal; | |
45 | ||
b10a44c3 TT |
46 | might_sleep(); |
47 | ||
722887dd TT |
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 | } | |
9924a92a | 65 | return jbd2__journal_start(journal, nblocks, GFP_NOFS, type, line); |
722887dd TT |
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 | } | |
4b9e9796 | 78 | |
722887dd | 79 | err = handle->h_err; |
4b9e9796 S |
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; | |
722887dd TT |
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 | ||
90c7201b TT |
119 | int __ext4_journal_get_write_access(const char *where, unsigned int line, |
120 | handle_t *handle, struct buffer_head *bh) | |
8984d137 | 121 | { |
0390131b FM |
122 | int err = 0; |
123 | ||
b10a44c3 TT |
124 | might_sleep(); |
125 | ||
0390131b FM |
126 | if (ext4_handle_valid(handle)) { |
127 | err = jbd2_journal_get_write_access(handle, bh); | |
128 | if (err) | |
90c7201b | 129 | ext4_journal_abort_handle(where, line, __func__, bh, |
0390131b FM |
130 | handle, err); |
131 | } | |
8984d137 AM |
132 | return err; |
133 | } | |
134 | ||
d6797d14 TT |
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 | */ | |
90c7201b TT |
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) | |
d6797d14 TT |
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 | ||
e4684b3f TT |
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 | ||
d6797d14 TT |
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"); | |
b7e57e7c TT |
178 | err = jbd2_journal_forget(handle, bh); |
179 | if (err) | |
90c7201b TT |
180 | ext4_journal_abort_handle(where, line, __func__, |
181 | bh, handle, err); | |
b7e57e7c | 182 | return err; |
d6797d14 TT |
183 | } |
184 | return 0; | |
185 | } | |
186 | ||
187 | /* | |
188 | * data!=journal && (is_metadata || should_journal_data(inode)) | |
189 | */ | |
e4684b3f TT |
190 | BUFFER_TRACE(bh, "call jbd2_journal_revoke"); |
191 | err = jbd2_journal_revoke(handle, blocknr, bh); | |
192 | if (err) { | |
90c7201b TT |
193 | ext4_journal_abort_handle(where, line, __func__, |
194 | bh, handle, err); | |
c398eda0 TT |
195 | __ext4_abort(inode->i_sb, where, line, |
196 | "error %d when attempting revoke", err); | |
e4684b3f | 197 | } |
d6797d14 TT |
198 | BUFFER_TRACE(bh, "exit"); |
199 | return err; | |
200 | } | |
201 | ||
90c7201b | 202 | int __ext4_journal_get_create_access(const char *where, unsigned int line, |
8984d137 AM |
203 | handle_t *handle, struct buffer_head *bh) |
204 | { | |
0390131b FM |
205 | int err = 0; |
206 | ||
207 | if (ext4_handle_valid(handle)) { | |
208 | err = jbd2_journal_get_create_access(handle, bh); | |
209 | if (err) | |
90c7201b TT |
210 | ext4_journal_abort_handle(where, line, __func__, |
211 | bh, handle, err); | |
0390131b | 212 | } |
8984d137 AM |
213 | return err; |
214 | } | |
215 | ||
90c7201b TT |
216 | int __ext4_handle_dirty_metadata(const char *where, unsigned int line, |
217 | handle_t *handle, struct inode *inode, | |
218 | struct buffer_head *bh) | |
8984d137 | 219 | { |
0390131b FM |
220 | int err = 0; |
221 | ||
b10a44c3 TT |
222 | might_sleep(); |
223 | ||
13fca323 TT |
224 | set_buffer_meta(bh); |
225 | set_buffer_prio(bh); | |
0390131b FM |
226 | if (ext4_handle_valid(handle)) { |
227 | err = jbd2_journal_dirty_metadata(handle, bh); | |
94aa327e JK |
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); | |
6b858821 TT |
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); | |
9ea7a0df | 241 | } |
0390131b | 242 | } else { |
73b50c1c | 243 | if (inode) |
fe188c0e TT |
244 | mark_buffer_dirty_inode(bh, inode); |
245 | else | |
246 | mark_buffer_dirty(bh); | |
0390131b FM |
247 | if (inode && inode_needs_sync(inode)) { |
248 | sync_dirty_buffer(bh); | |
249 | if (buffer_req(bh) && !buffer_uptodate(bh)) { | |
1c13d5c0 TT |
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); | |
c398eda0 TT |
255 | ext4_error_inode(inode, where, line, |
256 | bh->b_blocknr, | |
257 | "IO error syncing itable block"); | |
0390131b FM |
258 | err = -EIO; |
259 | } | |
260 | } | |
261 | } | |
8984d137 AM |
262 | return err; |
263 | } | |
a0375156 | 264 | |
90c7201b | 265 | int __ext4_handle_dirty_super(const char *where, unsigned int line, |
b50924c2 | 266 | handle_t *handle, struct super_block *sb) |
a0375156 TT |
267 | { |
268 | struct buffer_head *bh = EXT4_SB(sb)->s_sbh; | |
269 | int err = 0; | |
270 | ||
06db49e6 | 271 | ext4_superblock_csum_set(sb); |
a0375156 TT |
272 | if (ext4_handle_valid(handle)) { |
273 | err = jbd2_journal_dirty_metadata(handle, bh); | |
274 | if (err) | |
90c7201b TT |
275 | ext4_journal_abort_handle(where, line, __func__, |
276 | bh, handle, err); | |
06db49e6 | 277 | } else |
a9c47317 | 278 | mark_buffer_dirty(bh); |
a0375156 TT |
279 | return err; |
280 | } |