buffer_head_io.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * io.c
  5. *
  6. * Buffer cache handling
  7. *
  8. * Copyright (C) 2002, 2004 Oracle. All rights reserved.
  9. *
  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.
  14. *
  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.
  19. *
  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.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/types.h>
  27. #include <linux/slab.h>
  28. #include <linux/highmem.h>
  29. #include <cluster/masklog.h>
  30. #include "ocfs2.h"
  31. #include "alloc.h"
  32. #include "inode.h"
  33. #include "journal.h"
  34. #include "uptodate.h"
  35. #include "buffer_head_io.h"
  36. int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
  37. struct inode *inode)
  38. {
  39. int ret = 0;
  40. mlog_entry("(bh->b_blocknr = %llu, inode=%p)\n",
  41. (unsigned long long)bh->b_blocknr, inode);
  42. BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
  43. BUG_ON(buffer_jbd(bh));
  44. /* No need to check for a soft readonly file system here. non
  45. * journalled writes are only ever done on system files which
  46. * can get modified during recovery even if read-only. */
  47. if (ocfs2_is_hard_readonly(osb)) {
  48. ret = -EROFS;
  49. goto out;
  50. }
  51. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  52. lock_buffer(bh);
  53. set_buffer_uptodate(bh);
  54. /* remove from dirty list before I/O. */
  55. clear_buffer_dirty(bh);
  56. get_bh(bh); /* for end_buffer_write_sync() */
  57. bh->b_end_io = end_buffer_write_sync;
  58. submit_bh(WRITE, bh);
  59. wait_on_buffer(bh);
  60. if (buffer_uptodate(bh)) {
  61. ocfs2_set_buffer_uptodate(inode, bh);
  62. } else {
  63. /* We don't need to remove the clustered uptodate
  64. * information for this bh as it's not marked locally
  65. * uptodate. */
  66. ret = -EIO;
  67. brelse(bh);
  68. }
  69. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  70. out:
  71. mlog_exit(ret);
  72. return ret;
  73. }
  74. int ocfs2_read_blocks(struct ocfs2_super *osb, u64 block, int nr,
  75. struct buffer_head *bhs[], int flags,
  76. struct inode *inode)
  77. {
  78. int status = 0;
  79. struct super_block *sb;
  80. int i, ignore_cache = 0;
  81. struct buffer_head *bh;
  82. mlog_entry("(block=(%llu), nr=(%d), flags=%d, inode=%p)\n",
  83. (unsigned long long)block, nr, flags, inode);
  84. BUG_ON((flags & OCFS2_BH_READAHEAD) &&
  85. (!inode || !(flags & OCFS2_BH_CACHED)));
  86. if (osb == NULL || osb->sb == NULL || bhs == NULL) {
  87. status = -EINVAL;
  88. mlog_errno(status);
  89. goto bail;
  90. }
  91. if (nr < 0) {
  92. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  93. status = -EINVAL;
  94. mlog_errno(status);
  95. goto bail;
  96. }
  97. if (nr == 0) {
  98. mlog(ML_BH_IO, "No buffers will be read!\n");
  99. status = 0;
  100. goto bail;
  101. }
  102. sb = osb->sb;
  103. if (flags & OCFS2_BH_CACHED && !inode)
  104. flags &= ~OCFS2_BH_CACHED;
  105. if (inode)
  106. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  107. for (i = 0 ; i < nr ; i++) {
  108. if (bhs[i] == NULL) {
  109. bhs[i] = sb_getblk(sb, block++);
  110. if (bhs[i] == NULL) {
  111. if (inode)
  112. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  113. status = -EIO;
  114. mlog_errno(status);
  115. goto bail;
  116. }
  117. }
  118. bh = bhs[i];
  119. ignore_cache = 0;
  120. /* There are three read-ahead cases here which we need to
  121. * be concerned with. All three assume a buffer has
  122. * previously been submitted with OCFS2_BH_READAHEAD
  123. * and it hasn't yet completed I/O.
  124. *
  125. * 1) The current request is sync to disk. This rarely
  126. * happens these days, and never when performance
  127. * matters - the code can just wait on the buffer
  128. * lock and re-submit.
  129. *
  130. * 2) The current request is cached, but not
  131. * readahead. ocfs2_buffer_uptodate() will return
  132. * false anyway, so we'll wind up waiting on the
  133. * buffer lock to do I/O. We re-check the request
  134. * with after getting the lock to avoid a re-submit.
  135. *
  136. * 3) The current request is readahead (and so must
  137. * also be a caching one). We short circuit if the
  138. * buffer is locked (under I/O) and if it's in the
  139. * uptodate cache. The re-check from #2 catches the
  140. * case that the previous read-ahead completes just
  141. * before our is-it-in-flight check.
  142. */
  143. if (flags & OCFS2_BH_CACHED &&
  144. !ocfs2_buffer_uptodate(inode, bh)) {
  145. mlog(ML_UPTODATE,
  146. "bh (%llu), inode %llu not uptodate\n",
  147. (unsigned long long)bh->b_blocknr,
  148. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  149. ignore_cache = 1;
  150. }
  151. /* XXX: Can we ever get this and *not* have the cached
  152. * flag set? */
  153. if (buffer_jbd(bh)) {
  154. if (!(flags & OCFS2_BH_CACHED) || ignore_cache)
  155. mlog(ML_BH_IO, "trying to sync read a jbd "
  156. "managed bh (blocknr = %llu)\n",
  157. (unsigned long long)bh->b_blocknr);
  158. continue;
  159. }
  160. if (!(flags & OCFS2_BH_CACHED) || ignore_cache) {
  161. if (buffer_dirty(bh)) {
  162. /* This should probably be a BUG, or
  163. * at least return an error. */
  164. mlog(ML_BH_IO, "asking me to sync read a dirty "
  165. "buffer! (blocknr = %llu)\n",
  166. (unsigned long long)bh->b_blocknr);
  167. continue;
  168. }
  169. /* A read-ahead request was made - if the
  170. * buffer is already under read-ahead from a
  171. * previously submitted request than we are
  172. * done here. */
  173. if ((flags & OCFS2_BH_READAHEAD)
  174. && ocfs2_buffer_read_ahead(inode, bh))
  175. continue;
  176. lock_buffer(bh);
  177. if (buffer_jbd(bh)) {
  178. #ifdef CATCH_BH_JBD_RACES
  179. mlog(ML_ERROR, "block %llu had the JBD bit set "
  180. "while I was in lock_buffer!",
  181. (unsigned long long)bh->b_blocknr);
  182. BUG();
  183. #else
  184. unlock_buffer(bh);
  185. continue;
  186. #endif
  187. }
  188. /* Re-check ocfs2_buffer_uptodate() as a
  189. * previously read-ahead buffer may have
  190. * completed I/O while we were waiting for the
  191. * buffer lock. */
  192. if ((flags & OCFS2_BH_CACHED)
  193. && !(flags & OCFS2_BH_READAHEAD)
  194. && ocfs2_buffer_uptodate(inode, bh)) {
  195. unlock_buffer(bh);
  196. continue;
  197. }
  198. clear_buffer_uptodate(bh);
  199. get_bh(bh); /* for end_buffer_read_sync() */
  200. bh->b_end_io = end_buffer_read_sync;
  201. submit_bh(READ, bh);
  202. continue;
  203. }
  204. }
  205. status = 0;
  206. for (i = (nr - 1); i >= 0; i--) {
  207. bh = bhs[i];
  208. if (!(flags & OCFS2_BH_READAHEAD)) {
  209. /* We know this can't have changed as we hold the
  210. * inode sem. Avoid doing any work on the bh if the
  211. * journal has it. */
  212. if (!buffer_jbd(bh))
  213. wait_on_buffer(bh);
  214. if (!buffer_uptodate(bh)) {
  215. /* Status won't be cleared from here on out,
  216. * so we can safely record this and loop back
  217. * to cleanup the other buffers. Don't need to
  218. * remove the clustered uptodate information
  219. * for this bh as it's not marked locally
  220. * uptodate. */
  221. status = -EIO;
  222. brelse(bh);
  223. bhs[i] = NULL;
  224. continue;
  225. }
  226. }
  227. /* Always set the buffer in the cache, even if it was
  228. * a forced read, or read-ahead which hasn't yet
  229. * completed. */
  230. if (inode)
  231. ocfs2_set_buffer_uptodate(inode, bh);
  232. }
  233. if (inode)
  234. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  235. mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n",
  236. (unsigned long long)block, nr,
  237. (!(flags & OCFS2_BH_CACHED) || ignore_cache) ? "no" : "yes", flags);
  238. bail:
  239. mlog_exit(status);
  240. return status;
  241. }