hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/fs/ext4/ext4_jbd2.c
....@@ -7,6 +7,28 @@
77
88 #include <trace/events/ext4.h>
99
10
+int ext4_inode_journal_mode(struct inode *inode)
11
+{
12
+ if (EXT4_JOURNAL(inode) == NULL)
13
+ return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */
14
+ /* We do not support data journalling with delayed allocation */
15
+ if (!S_ISREG(inode->i_mode) ||
16
+ ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) ||
17
+ test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
18
+ (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) &&
19
+ !test_opt(inode->i_sb, DELALLOC))) {
20
+ /* We do not support data journalling for encrypted data */
21
+ if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
22
+ return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */
23
+ return EXT4_INODE_JOURNAL_DATA_MODE; /* journal data */
24
+ }
25
+ if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
26
+ return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */
27
+ if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
28
+ return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */
29
+ BUG();
30
+}
31
+
1032 /* Just increment the non-pointer handle value */
1133 static handle_t *ext4_get_nojournal(void)
1234 {
....@@ -58,28 +80,30 @@
5880 * take the FS itself readonly cleanly.
5981 */
6082 if (journal && is_journal_aborted(journal)) {
61
- ext4_abort(sb, "Detected aborted journal");
83
+ ext4_abort(sb, -journal->j_errno, "Detected aborted journal");
6284 return -EROFS;
6385 }
6486 return 0;
6587 }
6688
6789 handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
68
- int type, int blocks, int rsv_blocks)
90
+ int type, int blocks, int rsv_blocks,
91
+ int revoke_creds)
6992 {
7093 journal_t *journal;
7194 int err;
7295
73
- trace_ext4_journal_start(sb, blocks, rsv_blocks, _RET_IP_);
96
+ trace_ext4_journal_start(sb, blocks, rsv_blocks, revoke_creds,
97
+ _RET_IP_);
7498 err = ext4_journal_check_start(sb);
7599 if (err < 0)
76100 return ERR_PTR(err);
77101
78102 journal = EXT4_SB(sb)->s_journal;
79
- if (!journal)
103
+ if (!journal || (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
80104 return ext4_get_nojournal();
81
- return jbd2__journal_start(journal, blocks, rsv_blocks, GFP_NOFS,
82
- type, line);
105
+ return jbd2__journal_start(journal, blocks, rsv_blocks, revoke_creds,
106
+ GFP_NOFS, type, line);
83107 }
84108
85109 int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
....@@ -119,8 +143,8 @@
119143 return ext4_get_nojournal();
120144
121145 sb = handle->h_journal->j_private;
122
- trace_ext4_journal_start_reserved(sb, handle->h_buffer_credits,
123
- _RET_IP_);
146
+ trace_ext4_journal_start_reserved(sb,
147
+ jbd2_handle_buffer_credits(handle), _RET_IP_);
124148 err = ext4_journal_check_start(sb);
125149 if (err < 0) {
126150 jbd2_journal_free_reserved(handle);
....@@ -131,6 +155,21 @@
131155 if (err < 0)
132156 return ERR_PTR(err);
133157 return handle;
158
+}
159
+
160
+int __ext4_journal_ensure_credits(handle_t *handle, int check_cred,
161
+ int extend_cred, int revoke_cred)
162
+{
163
+ if (!ext4_handle_valid(handle))
164
+ return 0;
165
+ if (is_handle_aborted(handle))
166
+ return -EROFS;
167
+ if (jbd2_handle_buffer_credits(handle) >= check_cred &&
168
+ handle->h_revoke_credits >= revoke_cred)
169
+ return 0;
170
+ extend_cred = max(0, extend_cred - jbd2_handle_buffer_credits(handle));
171
+ revoke_cred = max(0, revoke_cred - handle->h_revoke_credits);
172
+ return ext4_journal_extend(handle, extend_cred, revoke_cred);
134173 }
135174
136175 static void ext4_journal_abort_handle(const char *caller, unsigned int line,
....@@ -158,12 +197,37 @@
158197 jbd2_journal_abort_handle(handle);
159198 }
160199
200
+static void ext4_check_bdev_write_error(struct super_block *sb)
201
+{
202
+ struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
203
+ struct ext4_sb_info *sbi = EXT4_SB(sb);
204
+ int err;
205
+
206
+ /*
207
+ * If the block device has write error flag, it may have failed to
208
+ * async write out metadata buffers in the background. In this case,
209
+ * we could read old data from disk and write it out again, which
210
+ * may lead to on-disk filesystem inconsistency.
211
+ */
212
+ if (errseq_check(&mapping->wb_err, READ_ONCE(sbi->s_bdev_wb_err))) {
213
+ spin_lock(&sbi->s_bdev_wb_lock);
214
+ err = errseq_check_and_advance(&mapping->wb_err, &sbi->s_bdev_wb_err);
215
+ spin_unlock(&sbi->s_bdev_wb_lock);
216
+ if (err)
217
+ ext4_error_err(sb, -err,
218
+ "Error while async write back metadata");
219
+ }
220
+}
221
+
161222 int __ext4_journal_get_write_access(const char *where, unsigned int line,
162223 handle_t *handle, struct buffer_head *bh)
163224 {
164225 int err = 0;
165226
166227 might_sleep();
228
+
229
+ if (bh->b_bdev->bd_super)
230
+ ext4_check_bdev_write_error(bh->b_bdev->bd_super);
167231
168232 if (ext4_handle_valid(handle)) {
169233 err = jbd2_journal_get_write_access(handle, bh);
....@@ -234,7 +298,7 @@
234298 if (err) {
235299 ext4_journal_abort_handle(where, line, __func__,
236300 bh, handle, err);
237
- __ext4_abort(inode->i_sb, where, line,
301
+ __ext4_abort(inode->i_sb, where, line, -err,
238302 "error %d when attempting revoke", err);
239303 }
240304 BUFFER_TRACE(bh, "exit");
....@@ -278,7 +342,7 @@
278342 handle->h_type,
279343 handle->h_line_no,
280344 handle->h_requested_credits,
281
- handle->h_buffer_credits, err);
345
+ jbd2_handle_buffer_credits(handle), err);
282346 return err;
283347 }
284348 ext4_error_inode(inode, where, line,
....@@ -289,9 +353,11 @@
289353 handle->h_type,
290354 handle->h_line_no,
291355 handle->h_requested_credits,
292
- handle->h_buffer_credits, err);
356
+ jbd2_handle_buffer_credits(handle),
357
+ err);
293358 }
294359 } else {
360
+ set_buffer_uptodate(bh);
295361 if (inode)
296362 mark_buffer_dirty_inode(bh, inode);
297363 else
....@@ -299,13 +365,8 @@
299365 if (inode && inode_needs_sync(inode)) {
300366 sync_dirty_buffer(bh);
301367 if (buffer_req(bh) && !buffer_uptodate(bh)) {
302
- struct ext4_super_block *es;
303
-
304
- es = EXT4_SB(inode->i_sb)->s_es;
305
- es->s_last_error_block =
306
- cpu_to_le64(bh->b_blocknr);
307
- ext4_error_inode(inode, where, line,
308
- bh->b_blocknr,
368
+ ext4_error_inode_err(inode, where, line,
369
+ bh->b_blocknr, EIO,
309370 "IO error syncing itable block");
310371 err = -EIO;
311372 }