1 /* -*- mode: c; c-basic-offset: 8; -*-
2 * vim: noexpandtab sw=8 ts=8 sts=0:
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.
27 #include <linux/types.h>
28 #include <linux/slab.h>
29 #include <linux/highmem.h>
31 #include <cluster/masklog.h>
40 #include "buffer_head_io.h"
42 int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
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)) {
61 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
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;
75 if (buffer_uptodate(bh)) {
76 ocfs2_set_buffer_uptodate(inode, bh);
78 /* We don't need to remove the clustered uptodate
79 * information for this bh as it's not marked locally
85 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
91 int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
92 struct buffer_head *bhs[], int flags,
96 struct super_block *sb;
97 int i, ignore_cache = 0;
98 struct buffer_head *bh;
100 mlog_entry("(block=(%llu), nr=(%d), flags=%d, inode=%p)\n",
101 (unsigned long long)block, nr, flags, inode);
103 if (osb == NULL || osb->sb == NULL || bhs == NULL) {
110 mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
117 mlog(ML_BH_IO, "No buffers will be read!\n");
124 if (flags & OCFS2_BH_CACHED && !inode)
125 flags &= ~OCFS2_BH_CACHED;
128 mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
129 for (i = 0 ; i < nr ; i++) {
130 if (bhs[i] == NULL) {
131 bhs[i] = sb_getblk(sb, block++);
132 if (bhs[i] == NULL) {
134 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
143 if (flags & OCFS2_BH_CACHED &&
144 !ocfs2_buffer_uptodate(inode, bh)) {
146 "bh (%llu), inode %llu not uptodate\n",
147 (unsigned long long)bh->b_blocknr,
148 (unsigned long long)OCFS2_I(inode)->ip_blkno);
152 /* XXX: Can we ever get this and *not* have the cached
154 if (buffer_jbd(bh)) {
155 if (!(flags & OCFS2_BH_CACHED) || ignore_cache)
156 mlog(ML_BH_IO, "trying to sync read a jbd "
157 "managed bh (blocknr = %llu)\n",
158 (unsigned long long)bh->b_blocknr);
162 if (!(flags & OCFS2_BH_CACHED) || ignore_cache) {
163 if (buffer_dirty(bh)) {
164 /* This should probably be a BUG, or
165 * at least return an error. */
166 mlog(ML_BH_IO, "asking me to sync read a dirty "
167 "buffer! (blocknr = %llu)\n",
168 (unsigned long long)bh->b_blocknr);
173 if (buffer_jbd(bh)) {
174 #ifdef CATCH_BH_JBD_RACES
175 mlog(ML_ERROR, "block %llu had the JBD bit set "
176 "while I was in lock_buffer!",
177 (unsigned long long)bh->b_blocknr);
184 clear_buffer_uptodate(bh);
185 get_bh(bh); /* for end_buffer_read_sync() */
186 bh->b_end_io = end_buffer_read_sync;
187 if (flags & OCFS2_BH_READAHEAD)
188 submit_bh(READA, bh);
197 for (i = (nr - 1); i >= 0; i--) {
200 /* We know this can't have changed as we hold the
201 * inode sem. Avoid doing any work on the bh if the
206 if (!buffer_uptodate(bh)) {
207 /* Status won't be cleared from here on out,
208 * so we can safely record this and loop back
209 * to cleanup the other buffers. Don't need to
210 * remove the clustered uptodate information
211 * for this bh as it's not marked locally
220 ocfs2_set_buffer_uptodate(inode, bh);
223 mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
225 mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s\n",
226 (unsigned long long)block, nr,
227 (!(flags & OCFS2_BH_CACHED) || ignore_cache) ? "no" : "yes");