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