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