p54: fix misbehavings when firmware can't be found
[linux-2.6/mini2440.git] / fs / ocfs2 / buffer_head_io.c
blob7e947c6724694c74f7a1658392f83e15970fb7cd
1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
4 * io.c
6 * Buffer cache handling
8 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation; either
13 * version 2 of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public
21 * License along with this program; if not, write to the
22 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 * Boston, MA 021110-1307, USA.
26 #include <linux/fs.h>
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
31 #include <cluster/masklog.h>
33 #include "ocfs2.h"
35 #include "alloc.h"
36 #include "inode.h"
37 #include "journal.h"
38 #include "uptodate.h"
40 #include "buffer_head_io.h"
42 int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
43 struct inode *inode)
45 int ret = 0;
47 mlog_entry("(bh->b_blocknr = %llu, inode=%p)\n",
48 (unsigned long long)bh->b_blocknr, inode);
50 BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
51 BUG_ON(buffer_jbd(bh));
53 /* No need to check for a soft readonly file system here. non
54 * journalled writes are only ever done on system files which
55 * can get modified during recovery even if read-only. */
56 if (ocfs2_is_hard_readonly(osb)) {
57 ret = -EROFS;
58 goto out;
61 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
63 lock_buffer(bh);
64 set_buffer_uptodate(bh);
66 /* remove from dirty list before I/O. */
67 clear_buffer_dirty(bh);
69 get_bh(bh); /* for end_buffer_write_sync() */
70 bh->b_end_io = end_buffer_write_sync;
71 submit_bh(WRITE, bh);
73 wait_on_buffer(bh);
75 if (buffer_uptodate(bh)) {
76 ocfs2_set_buffer_uptodate(inode, bh);
77 } else {
78 /* We don't need to remove the clustered uptodate
79 * information for this bh as it's not marked locally
80 * uptodate. */
81 ret = -EIO;
82 put_bh(bh);
85 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
86 out:
87 mlog_exit(ret);
88 return ret;
91 int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
92 unsigned int nr, struct buffer_head *bhs[])
94 int status = 0;
95 unsigned int i;
96 struct buffer_head *bh;
98 if (!nr) {
99 mlog(ML_BH_IO, "No buffers will be read!\n");
100 goto bail;
103 for (i = 0 ; i < nr ; i++) {
104 if (bhs[i] == NULL) {
105 bhs[i] = sb_getblk(osb->sb, block++);
106 if (bhs[i] == NULL) {
107 status = -EIO;
108 mlog_errno(status);
109 goto bail;
112 bh = bhs[i];
114 if (buffer_jbd(bh)) {
115 mlog(ML_ERROR,
116 "trying to sync read a jbd "
117 "managed bh (blocknr = %llu), skipping\n",
118 (unsigned long long)bh->b_blocknr);
119 continue;
122 if (buffer_dirty(bh)) {
123 /* This should probably be a BUG, or
124 * at least return an error. */
125 mlog(ML_ERROR,
126 "trying to sync read a dirty "
127 "buffer! (blocknr = %llu), skipping\n",
128 (unsigned long long)bh->b_blocknr);
129 continue;
132 lock_buffer(bh);
133 if (buffer_jbd(bh)) {
134 mlog(ML_ERROR,
135 "block %llu had the JBD bit set "
136 "while I was in lock_buffer!",
137 (unsigned long long)bh->b_blocknr);
138 BUG();
141 clear_buffer_uptodate(bh);
142 get_bh(bh); /* for end_buffer_read_sync() */
143 bh->b_end_io = end_buffer_read_sync;
144 submit_bh(READ, bh);
147 for (i = nr; i > 0; i--) {
148 bh = bhs[i - 1];
150 if (buffer_jbd(bh)) {
151 mlog(ML_ERROR,
152 "the journal got the buffer while it was "
153 "locked for io! (blocknr = %llu)\n",
154 (unsigned long long)bh->b_blocknr);
155 BUG();
158 wait_on_buffer(bh);
159 if (!buffer_uptodate(bh)) {
160 /* Status won't be cleared from here on out,
161 * so we can safely record this and loop back
162 * to cleanup the other buffers. */
163 status = -EIO;
164 put_bh(bh);
165 bhs[i - 1] = NULL;
169 bail:
170 return status;
173 int ocfs2_read_blocks(struct inode *inode, u64 block, int nr,
174 struct buffer_head *bhs[], int flags)
176 int status = 0;
177 int i, ignore_cache = 0;
178 struct buffer_head *bh;
180 mlog_entry("(inode=%p, block=(%llu), nr=(%d), flags=%d)\n",
181 inode, (unsigned long long)block, nr, flags);
183 BUG_ON(!inode);
184 BUG_ON((flags & OCFS2_BH_READAHEAD) &&
185 (flags & OCFS2_BH_IGNORE_CACHE));
187 if (bhs == NULL) {
188 status = -EINVAL;
189 mlog_errno(status);
190 goto bail;
193 if (nr < 0) {
194 mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
195 status = -EINVAL;
196 mlog_errno(status);
197 goto bail;
200 if (nr == 0) {
201 mlog(ML_BH_IO, "No buffers will be read!\n");
202 status = 0;
203 goto bail;
206 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
207 for (i = 0 ; i < nr ; i++) {
208 if (bhs[i] == NULL) {
209 bhs[i] = sb_getblk(inode->i_sb, block++);
210 if (bhs[i] == NULL) {
211 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
212 status = -EIO;
213 mlog_errno(status);
214 goto bail;
217 bh = bhs[i];
218 ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
220 /* There are three read-ahead cases here which we need to
221 * be concerned with. All three assume a buffer has
222 * previously been submitted with OCFS2_BH_READAHEAD
223 * and it hasn't yet completed I/O.
225 * 1) The current request is sync to disk. This rarely
226 * happens these days, and never when performance
227 * matters - the code can just wait on the buffer
228 * lock and re-submit.
230 * 2) The current request is cached, but not
231 * readahead. ocfs2_buffer_uptodate() will return
232 * false anyway, so we'll wind up waiting on the
233 * buffer lock to do I/O. We re-check the request
234 * with after getting the lock to avoid a re-submit.
236 * 3) The current request is readahead (and so must
237 * also be a caching one). We short circuit if the
238 * buffer is locked (under I/O) and if it's in the
239 * uptodate cache. The re-check from #2 catches the
240 * case that the previous read-ahead completes just
241 * before our is-it-in-flight check.
244 if (!ignore_cache && !ocfs2_buffer_uptodate(inode, bh)) {
245 mlog(ML_UPTODATE,
246 "bh (%llu), inode %llu not uptodate\n",
247 (unsigned long long)bh->b_blocknr,
248 (unsigned long long)OCFS2_I(inode)->ip_blkno);
249 /* We're using ignore_cache here to say
250 * "go to disk" */
251 ignore_cache = 1;
254 /* XXX: Can we ever get this and *not* have the cached
255 * flag set? */
256 if (buffer_jbd(bh)) {
257 if (ignore_cache)
258 mlog(ML_BH_IO, "trying to sync read a jbd "
259 "managed bh (blocknr = %llu)\n",
260 (unsigned long long)bh->b_blocknr);
261 continue;
264 if (ignore_cache) {
265 if (buffer_dirty(bh)) {
266 /* This should probably be a BUG, or
267 * at least return an error. */
268 mlog(ML_BH_IO, "asking me to sync read a dirty "
269 "buffer! (blocknr = %llu)\n",
270 (unsigned long long)bh->b_blocknr);
271 continue;
274 /* A read-ahead request was made - if the
275 * buffer is already under read-ahead from a
276 * previously submitted request than we are
277 * done here. */
278 if ((flags & OCFS2_BH_READAHEAD)
279 && ocfs2_buffer_read_ahead(inode, bh))
280 continue;
282 lock_buffer(bh);
283 if (buffer_jbd(bh)) {
284 #ifdef CATCH_BH_JBD_RACES
285 mlog(ML_ERROR, "block %llu had the JBD bit set "
286 "while I was in lock_buffer!",
287 (unsigned long long)bh->b_blocknr);
288 BUG();
289 #else
290 unlock_buffer(bh);
291 continue;
292 #endif
295 /* Re-check ocfs2_buffer_uptodate() as a
296 * previously read-ahead buffer may have
297 * completed I/O while we were waiting for the
298 * buffer lock. */
299 if (!(flags & OCFS2_BH_IGNORE_CACHE)
300 && !(flags & OCFS2_BH_READAHEAD)
301 && ocfs2_buffer_uptodate(inode, bh)) {
302 unlock_buffer(bh);
303 continue;
306 clear_buffer_uptodate(bh);
307 get_bh(bh); /* for end_buffer_read_sync() */
308 bh->b_end_io = end_buffer_read_sync;
309 submit_bh(READ, bh);
310 continue;
314 status = 0;
316 for (i = (nr - 1); i >= 0; i--) {
317 bh = bhs[i];
319 if (!(flags & OCFS2_BH_READAHEAD)) {
320 /* We know this can't have changed as we hold the
321 * inode sem. Avoid doing any work on the bh if the
322 * journal has it. */
323 if (!buffer_jbd(bh))
324 wait_on_buffer(bh);
326 if (!buffer_uptodate(bh)) {
327 /* Status won't be cleared from here on out,
328 * so we can safely record this and loop back
329 * to cleanup the other buffers. Don't need to
330 * remove the clustered uptodate information
331 * for this bh as it's not marked locally
332 * uptodate. */
333 status = -EIO;
334 put_bh(bh);
335 bhs[i] = NULL;
336 continue;
340 /* Always set the buffer in the cache, even if it was
341 * a forced read, or read-ahead which hasn't yet
342 * completed. */
343 ocfs2_set_buffer_uptodate(inode, bh);
345 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
347 mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n",
348 (unsigned long long)block, nr,
349 ((flags & OCFS2_BH_IGNORE_CACHE) || ignore_cache) ? "no" : "yes",
350 flags);
352 bail:
354 mlog_exit(status);
355 return status;
358 /* Check whether the blkno is the super block or one of the backups. */
359 static void ocfs2_check_super_or_backup(struct super_block *sb,
360 sector_t blkno)
362 int i;
363 u64 backup_blkno;
365 if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
366 return;
368 for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
369 backup_blkno = ocfs2_backup_super_blkno(sb, i);
370 if (backup_blkno == blkno)
371 return;
374 BUG();
378 * Write super block and backups doesn't need to collaborate with journal,
379 * so we don't need to lock ip_io_mutex and inode doesn't need to bea passed
380 * into this function.
382 int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
383 struct buffer_head *bh)
385 int ret = 0;
387 mlog_entry_void();
389 BUG_ON(buffer_jbd(bh));
390 ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
392 if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
393 ret = -EROFS;
394 goto out;
397 lock_buffer(bh);
398 set_buffer_uptodate(bh);
400 /* remove from dirty list before I/O. */
401 clear_buffer_dirty(bh);
403 get_bh(bh); /* for end_buffer_write_sync() */
404 bh->b_end_io = end_buffer_write_sync;
405 submit_bh(WRITE, bh);
407 wait_on_buffer(bh);
409 if (!buffer_uptodate(bh)) {
410 ret = -EIO;
411 put_bh(bh);
414 out:
415 mlog_exit(ret);
416 return ret;