blob: c8a6e3480be1cb55d0cbe3ddad7e142863b7090f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/* -*- mode: c; c-basic-offset: 8; -*-
3 * vim: noexpandtab sw=8 ts=8 sts=0:
4 *
5 * io.c
6 *
7 * Buffer cache handling
8 *
9 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 */
11
12#include <linux/fs.h>
13#include <linux/types.h>
14#include <linux/highmem.h>
15#include <linux/bio.h>
16
17#include <cluster/masklog.h>
18
19#include "ocfs2.h"
20
21#include "alloc.h"
22#include "inode.h"
23#include "journal.h"
24#include "uptodate.h"
25#include "buffer_head_io.h"
26#include "ocfs2_trace.h"
27
28/*
29 * Bits on bh->b_state used by ocfs2.
30 *
31 * These MUST be after the JBD2 bits. Hence, we use BH_JBDPrivateStart.
32 */
33enum ocfs2_state_bits {
34 BH_NeedsValidate = BH_JBDPrivateStart,
35};
36
37/* Expand the magic b_state functions */
38BUFFER_FNS(NeedsValidate, needs_validate);
39
40int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
41 struct ocfs2_caching_info *ci)
42{
43 int ret = 0;
44
45 trace_ocfs2_write_block((unsigned long long)bh->b_blocknr, ci);
46
47 BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
48 BUG_ON(buffer_jbd(bh));
49
50 /* No need to check for a soft readonly file system here. non
51 * journalled writes are only ever done on system files which
52 * can get modified during recovery even if read-only. */
53 if (ocfs2_is_hard_readonly(osb)) {
54 ret = -EROFS;
55 mlog_errno(ret);
56 goto out;
57 }
58
59 ocfs2_metadata_cache_io_lock(ci);
60
61 lock_buffer(bh);
62 set_buffer_uptodate(bh);
63
64 /* remove from dirty list before I/O. */
65 clear_buffer_dirty(bh);
66
67 get_bh(bh); /* for end_buffer_write_sync() */
68 bh->b_end_io = end_buffer_write_sync;
69 submit_bh(REQ_OP_WRITE, 0, bh);
70
71 wait_on_buffer(bh);
72
73 if (buffer_uptodate(bh)) {
74 ocfs2_set_buffer_uptodate(ci, bh);
75 } else {
76 /* We don't need to remove the clustered uptodate
77 * information for this bh as it's not marked locally
78 * uptodate. */
79 ret = -EIO;
80 mlog_errno(ret);
81 }
82
83 ocfs2_metadata_cache_io_unlock(ci);
84out:
85 return ret;
86}
87
88/* Caller must provide a bhs[] with all NULL or non-NULL entries, so it
89 * will be easier to handle read failure.
90 */
91int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
92 unsigned int nr, struct buffer_head *bhs[])
93{
94 int status = 0;
95 unsigned int i;
96 struct buffer_head *bh;
97 int new_bh = 0;
98
99 trace_ocfs2_read_blocks_sync((unsigned long long)block, nr);
100
101 if (!nr)
102 goto bail;
103
104 /* Don't put buffer head and re-assign it to NULL if it is allocated
105 * outside since the caller can't be aware of this alternation!
106 */
107 new_bh = (bhs[0] == NULL);
108
109 for (i = 0 ; i < nr ; i++) {
110 if (bhs[i] == NULL) {
111 bhs[i] = sb_getblk(osb->sb, block++);
112 if (bhs[i] == NULL) {
113 status = -ENOMEM;
114 mlog_errno(status);
115 break;
116 }
117 }
118 bh = bhs[i];
119
120 if (buffer_jbd(bh)) {
121 trace_ocfs2_read_blocks_sync_jbd(
122 (unsigned long long)bh->b_blocknr);
123 continue;
124 }
125
126 if (buffer_dirty(bh)) {
127 /* This should probably be a BUG, or
128 * at least return an error. */
129 mlog(ML_ERROR,
130 "trying to sync read a dirty "
131 "buffer! (blocknr = %llu), skipping\n",
132 (unsigned long long)bh->b_blocknr);
133 continue;
134 }
135
136 lock_buffer(bh);
137 if (buffer_jbd(bh)) {
138#ifdef CATCH_BH_JBD_RACES
139 mlog(ML_ERROR,
140 "block %llu had the JBD bit set "
141 "while I was in lock_buffer!",
142 (unsigned long long)bh->b_blocknr);
143 BUG();
144#else
145 unlock_buffer(bh);
146 continue;
147#endif
148 }
149
150 get_bh(bh); /* for end_buffer_read_sync() */
151 bh->b_end_io = end_buffer_read_sync;
152 submit_bh(REQ_OP_READ, 0, bh);
153 }
154
155read_failure:
156 for (i = nr; i > 0; i--) {
157 bh = bhs[i - 1];
158
159 if (unlikely(status)) {
160 if (new_bh && bh) {
161 /* If middle bh fails, let previous bh
162 * finish its read and then put it to
163 * aovoid bh leak
164 */
165 if (!buffer_jbd(bh))
166 wait_on_buffer(bh);
167 put_bh(bh);
168 bhs[i - 1] = NULL;
169 } else if (bh && buffer_uptodate(bh)) {
170 clear_buffer_uptodate(bh);
171 }
172 continue;
173 }
174
175 /* No need to wait on the buffer if it's managed by JBD. */
176 if (!buffer_jbd(bh))
177 wait_on_buffer(bh);
178
179 if (!buffer_uptodate(bh)) {
180 /* Status won't be cleared from here on out,
181 * so we can safely record this and loop back
182 * to cleanup the other buffers. */
183 status = -EIO;
184 goto read_failure;
185 }
186 }
187
188bail:
189 return status;
190}
191
192/* Caller must provide a bhs[] with all NULL or non-NULL entries, so it
193 * will be easier to handle read failure.
194 */
195int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
196 struct buffer_head *bhs[], int flags,
197 int (*validate)(struct super_block *sb,
198 struct buffer_head *bh))
199{
200 int status = 0;
201 int i, ignore_cache = 0;
202 struct buffer_head *bh;
203 struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
204 int new_bh = 0;
205
206 trace_ocfs2_read_blocks_begin(ci, (unsigned long long)block, nr, flags);
207
208 BUG_ON(!ci);
209 BUG_ON((flags & OCFS2_BH_READAHEAD) &&
210 (flags & OCFS2_BH_IGNORE_CACHE));
211
212 if (bhs == NULL) {
213 status = -EINVAL;
214 mlog_errno(status);
215 goto bail;
216 }
217
218 if (nr < 0) {
219 mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
220 status = -EINVAL;
221 mlog_errno(status);
222 goto bail;
223 }
224
225 if (nr == 0) {
226 status = 0;
227 goto bail;
228 }
229
230 /* Don't put buffer head and re-assign it to NULL if it is allocated
231 * outside since the caller can't be aware of this alternation!
232 */
233 new_bh = (bhs[0] == NULL);
234
235 ocfs2_metadata_cache_io_lock(ci);
236 for (i = 0 ; i < nr ; i++) {
237 if (bhs[i] == NULL) {
238 bhs[i] = sb_getblk(sb, block++);
239 if (bhs[i] == NULL) {
240 status = -ENOMEM;
241 mlog_errno(status);
242 /* Don't forget to put previous bh! */
243 break;
244 }
245 }
246 bh = bhs[i];
247 ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
248
249 /* There are three read-ahead cases here which we need to
250 * be concerned with. All three assume a buffer has
251 * previously been submitted with OCFS2_BH_READAHEAD
252 * and it hasn't yet completed I/O.
253 *
254 * 1) The current request is sync to disk. This rarely
255 * happens these days, and never when performance
256 * matters - the code can just wait on the buffer
257 * lock and re-submit.
258 *
259 * 2) The current request is cached, but not
260 * readahead. ocfs2_buffer_uptodate() will return
261 * false anyway, so we'll wind up waiting on the
262 * buffer lock to do I/O. We re-check the request
263 * with after getting the lock to avoid a re-submit.
264 *
265 * 3) The current request is readahead (and so must
266 * also be a caching one). We short circuit if the
267 * buffer is locked (under I/O) and if it's in the
268 * uptodate cache. The re-check from #2 catches the
269 * case that the previous read-ahead completes just
270 * before our is-it-in-flight check.
271 */
272
273 if (!ignore_cache && !ocfs2_buffer_uptodate(ci, bh)) {
274 trace_ocfs2_read_blocks_from_disk(
275 (unsigned long long)bh->b_blocknr,
276 (unsigned long long)ocfs2_metadata_cache_owner(ci));
277 /* We're using ignore_cache here to say
278 * "go to disk" */
279 ignore_cache = 1;
280 }
281
282 trace_ocfs2_read_blocks_bh((unsigned long long)bh->b_blocknr,
283 ignore_cache, buffer_jbd(bh), buffer_dirty(bh));
284
285 if (buffer_jbd(bh)) {
286 continue;
287 }
288
289 if (ignore_cache) {
290 if (buffer_dirty(bh)) {
291 /* This should probably be a BUG, or
292 * at least return an error. */
293 continue;
294 }
295
296 /* A read-ahead request was made - if the
297 * buffer is already under read-ahead from a
298 * previously submitted request than we are
299 * done here. */
300 if ((flags & OCFS2_BH_READAHEAD)
301 && ocfs2_buffer_read_ahead(ci, bh))
302 continue;
303
304 lock_buffer(bh);
305 if (buffer_jbd(bh)) {
306#ifdef CATCH_BH_JBD_RACES
307 mlog(ML_ERROR, "block %llu had the JBD bit set "
308 "while I was in lock_buffer!",
309 (unsigned long long)bh->b_blocknr);
310 BUG();
311#else
312 unlock_buffer(bh);
313 continue;
314#endif
315 }
316
317 /* Re-check ocfs2_buffer_uptodate() as a
318 * previously read-ahead buffer may have
319 * completed I/O while we were waiting for the
320 * buffer lock. */
321 if (!(flags & OCFS2_BH_IGNORE_CACHE)
322 && !(flags & OCFS2_BH_READAHEAD)
323 && ocfs2_buffer_uptodate(ci, bh)) {
324 unlock_buffer(bh);
325 continue;
326 }
327
328 get_bh(bh); /* for end_buffer_read_sync() */
329 if (validate)
330 set_buffer_needs_validate(bh);
331 bh->b_end_io = end_buffer_read_sync;
332 submit_bh(REQ_OP_READ, 0, bh);
333 continue;
334 }
335 }
336
337read_failure:
338 for (i = (nr - 1); i >= 0; i--) {
339 bh = bhs[i];
340
341 if (!(flags & OCFS2_BH_READAHEAD)) {
342 if (unlikely(status)) {
343 /* Clear the buffers on error including those
344 * ever succeeded in reading
345 */
346 if (new_bh && bh) {
347 /* If middle bh fails, let previous bh
348 * finish its read and then put it to
349 * aovoid bh leak
350 */
351 if (!buffer_jbd(bh))
352 wait_on_buffer(bh);
353 put_bh(bh);
354 bhs[i] = NULL;
355 } else if (bh && buffer_uptodate(bh)) {
356 clear_buffer_uptodate(bh);
357 }
358 continue;
359 }
360 /* We know this can't have changed as we hold the
361 * owner sem. Avoid doing any work on the bh if the
362 * journal has it. */
363 if (!buffer_jbd(bh))
364 wait_on_buffer(bh);
365
366 if (!buffer_uptodate(bh)) {
367 /* Status won't be cleared from here on out,
368 * so we can safely record this and loop back
369 * to cleanup the other buffers. Don't need to
370 * remove the clustered uptodate information
371 * for this bh as it's not marked locally
372 * uptodate. */
373 status = -EIO;
374 clear_buffer_needs_validate(bh);
375 goto read_failure;
376 }
377
378 if (buffer_needs_validate(bh)) {
379 /* We never set NeedsValidate if the
380 * buffer was held by the journal, so
381 * that better not have changed */
382 BUG_ON(buffer_jbd(bh));
383 clear_buffer_needs_validate(bh);
384 status = validate(sb, bh);
385 if (status)
386 goto read_failure;
387 }
388 }
389
390 /* Always set the buffer in the cache, even if it was
391 * a forced read, or read-ahead which hasn't yet
392 * completed. */
393 if (bh)
394 ocfs2_set_buffer_uptodate(ci, bh);
395 }
396 ocfs2_metadata_cache_io_unlock(ci);
397
398 trace_ocfs2_read_blocks_end((unsigned long long)block, nr,
399 flags, ignore_cache);
400
401bail:
402
403 return status;
404}
405
406/* Check whether the blkno is the super block or one of the backups. */
407static void ocfs2_check_super_or_backup(struct super_block *sb,
408 sector_t blkno)
409{
410 int i;
411 u64 backup_blkno;
412
413 if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
414 return;
415
416 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
417 backup_blkno = ocfs2_backup_super_blkno(sb, i);
418 if (backup_blkno == blkno)
419 return;
420 }
421
422 BUG();
423}
424
425/*
426 * Write super block and backups doesn't need to collaborate with journal,
427 * so we don't need to lock ip_io_mutex and ci doesn't need to bea passed
428 * into this function.
429 */
430int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
431 struct buffer_head *bh)
432{
433 int ret = 0;
434 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
435
436 BUG_ON(buffer_jbd(bh));
437 ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
438
439 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
440 ret = -EROFS;
441 mlog_errno(ret);
442 goto out;
443 }
444
445 lock_buffer(bh);
446 set_buffer_uptodate(bh);
447
448 /* remove from dirty list before I/O. */
449 clear_buffer_dirty(bh);
450
451 get_bh(bh); /* for end_buffer_write_sync() */
452 bh->b_end_io = end_buffer_write_sync;
453 ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &di->i_check);
454 submit_bh(REQ_OP_WRITE, 0, bh);
455
456 wait_on_buffer(bh);
457
458 if (!buffer_uptodate(bh)) {
459 ret = -EIO;
460 mlog_errno(ret);
461 }
462
463out:
464 return ret;
465}