buffer_head_io.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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=(%"MLFu64"), nr=(%d), flags=%d, inode=%p)\n",
  83. block, nr, flags, inode);
  84. if (osb == NULL || osb->sb == NULL || bhs == NULL) {
  85. status = -EINVAL;
  86. mlog_errno(status);
  87. goto bail;
  88. }
  89. if (nr < 0) {
  90. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  91. status = -EINVAL;
  92. mlog_errno(status);
  93. goto bail;
  94. }
  95. if (nr == 0) {
  96. mlog(ML_BH_IO, "No buffers will be read!\n");
  97. status = 0;
  98. goto bail;
  99. }
  100. sb = osb->sb;
  101. if (flags & OCFS2_BH_CACHED && !inode)
  102. flags &= ~OCFS2_BH_CACHED;
  103. if (inode)
  104. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  105. for (i = 0 ; i < nr ; i++) {
  106. if (bhs[i] == NULL) {
  107. bhs[i] = sb_getblk(sb, block++);
  108. if (bhs[i] == NULL) {
  109. if (inode)
  110. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  111. status = -EIO;
  112. mlog_errno(status);
  113. goto bail;
  114. }
  115. }
  116. bh = bhs[i];
  117. ignore_cache = 0;
  118. if (flags & OCFS2_BH_CACHED &&
  119. !ocfs2_buffer_uptodate(inode, bh)) {
  120. mlog(ML_UPTODATE,
  121. "bh (%llu), inode %"MLFu64" not uptodate\n",
  122. (unsigned long long)bh->b_blocknr,
  123. OCFS2_I(inode)->ip_blkno);
  124. ignore_cache = 1;
  125. }
  126. /* XXX: Can we ever get this and *not* have the cached
  127. * flag set? */
  128. if (buffer_jbd(bh)) {
  129. if (!(flags & OCFS2_BH_CACHED) || ignore_cache)
  130. mlog(ML_BH_IO, "trying to sync read a jbd "
  131. "managed bh (blocknr = %llu)\n",
  132. (unsigned long long)bh->b_blocknr);
  133. continue;
  134. }
  135. if (!(flags & OCFS2_BH_CACHED) || ignore_cache) {
  136. if (buffer_dirty(bh)) {
  137. /* This should probably be a BUG, or
  138. * at least return an error. */
  139. mlog(ML_BH_IO, "asking me to sync read a dirty "
  140. "buffer! (blocknr = %llu)\n",
  141. (unsigned long long)bh->b_blocknr);
  142. continue;
  143. }
  144. lock_buffer(bh);
  145. if (buffer_jbd(bh)) {
  146. #ifdef CATCH_BH_JBD_RACES
  147. mlog(ML_ERROR, "block %llu had the JBD bit set "
  148. "while I was in lock_buffer!",
  149. (unsigned long long)bh->b_blocknr);
  150. BUG();
  151. #else
  152. unlock_buffer(bh);
  153. continue;
  154. #endif
  155. }
  156. clear_buffer_uptodate(bh);
  157. get_bh(bh); /* for end_buffer_read_sync() */
  158. bh->b_end_io = end_buffer_read_sync;
  159. if (flags & OCFS2_BH_READAHEAD)
  160. submit_bh(READA, bh);
  161. else
  162. submit_bh(READ, bh);
  163. continue;
  164. }
  165. }
  166. status = 0;
  167. for (i = (nr - 1); i >= 0; i--) {
  168. bh = bhs[i];
  169. /* We know this can't have changed as we hold the
  170. * inode sem. Avoid doing any work on the bh if the
  171. * journal has it. */
  172. if (!buffer_jbd(bh))
  173. wait_on_buffer(bh);
  174. if (!buffer_uptodate(bh)) {
  175. /* Status won't be cleared from here on out,
  176. * so we can safely record this and loop back
  177. * to cleanup the other buffers. Don't need to
  178. * remove the clustered uptodate information
  179. * for this bh as it's not marked locally
  180. * uptodate. */
  181. status = -EIO;
  182. brelse(bh);
  183. bhs[i] = NULL;
  184. continue;
  185. }
  186. if (inode)
  187. ocfs2_set_buffer_uptodate(inode, bh);
  188. }
  189. if (inode)
  190. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  191. mlog(ML_BH_IO, "block=(%"MLFu64"), nr=(%d), cached=%s\n", block, nr,
  192. (!(flags & OCFS2_BH_CACHED) || ignore_cache) ? "no" : "yes");
  193. bail:
  194. mlog_exit(status);
  195. return status;
  196. }