buffer_head_io.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. put_bh(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_sync(struct ocfs2_super *osb, u64 block,
  75. unsigned int nr, struct buffer_head *bhs[])
  76. {
  77. int status = 0;
  78. unsigned int i;
  79. struct buffer_head *bh;
  80. if (!nr) {
  81. mlog(ML_BH_IO, "No buffers will be read!\n");
  82. goto bail;
  83. }
  84. for (i = 0 ; i < nr ; i++) {
  85. if (bhs[i] == NULL) {
  86. bhs[i] = sb_getblk(osb->sb, block++);
  87. if (bhs[i] == NULL) {
  88. status = -EIO;
  89. mlog_errno(status);
  90. goto bail;
  91. }
  92. }
  93. bh = bhs[i];
  94. if (buffer_jbd(bh)) {
  95. mlog(ML_ERROR,
  96. "trying to sync read a jbd "
  97. "managed bh (blocknr = %llu), skipping\n",
  98. (unsigned long long)bh->b_blocknr);
  99. continue;
  100. }
  101. if (buffer_dirty(bh)) {
  102. /* This should probably be a BUG, or
  103. * at least return an error. */
  104. mlog(ML_ERROR,
  105. "trying to sync read a dirty "
  106. "buffer! (blocknr = %llu), skipping\n",
  107. (unsigned long long)bh->b_blocknr);
  108. continue;
  109. }
  110. lock_buffer(bh);
  111. if (buffer_jbd(bh)) {
  112. mlog(ML_ERROR,
  113. "block %llu had the JBD bit set "
  114. "while I was in lock_buffer!",
  115. (unsigned long long)bh->b_blocknr);
  116. BUG();
  117. }
  118. clear_buffer_uptodate(bh);
  119. get_bh(bh); /* for end_buffer_read_sync() */
  120. bh->b_end_io = end_buffer_read_sync;
  121. submit_bh(READ, bh);
  122. }
  123. for (i = nr; i > 0; i--) {
  124. bh = bhs[i - 1];
  125. if (buffer_jbd(bh)) {
  126. mlog(ML_ERROR,
  127. "the journal got the buffer while it was "
  128. "locked for io! (blocknr = %llu)\n",
  129. (unsigned long long)bh->b_blocknr);
  130. BUG();
  131. }
  132. wait_on_buffer(bh);
  133. if (!buffer_uptodate(bh)) {
  134. /* Status won't be cleared from here on out,
  135. * so we can safely record this and loop back
  136. * to cleanup the other buffers. */
  137. status = -EIO;
  138. put_bh(bh);
  139. bhs[i - 1] = NULL;
  140. }
  141. }
  142. bail:
  143. return status;
  144. }
  145. int ocfs2_read_blocks(struct inode *inode, u64 block, int nr,
  146. struct buffer_head *bhs[], int flags)
  147. {
  148. int status = 0;
  149. int i, ignore_cache = 0;
  150. struct buffer_head *bh;
  151. mlog_entry("(inode=%p, block=(%llu), nr=(%d), flags=%d)\n",
  152. inode, (unsigned long long)block, nr, flags);
  153. BUG_ON(!inode);
  154. BUG_ON((flags & OCFS2_BH_READAHEAD) &&
  155. (flags & OCFS2_BH_IGNORE_CACHE));
  156. if (bhs == NULL) {
  157. status = -EINVAL;
  158. mlog_errno(status);
  159. goto bail;
  160. }
  161. if (nr < 0) {
  162. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  163. status = -EINVAL;
  164. mlog_errno(status);
  165. goto bail;
  166. }
  167. if (nr == 0) {
  168. mlog(ML_BH_IO, "No buffers will be read!\n");
  169. status = 0;
  170. goto bail;
  171. }
  172. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  173. for (i = 0 ; i < nr ; i++) {
  174. if (bhs[i] == NULL) {
  175. bhs[i] = sb_getblk(inode->i_sb, block++);
  176. if (bhs[i] == NULL) {
  177. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  178. status = -EIO;
  179. mlog_errno(status);
  180. goto bail;
  181. }
  182. }
  183. bh = bhs[i];
  184. ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
  185. /* There are three read-ahead cases here which we need to
  186. * be concerned with. All three assume a buffer has
  187. * previously been submitted with OCFS2_BH_READAHEAD
  188. * and it hasn't yet completed I/O.
  189. *
  190. * 1) The current request is sync to disk. This rarely
  191. * happens these days, and never when performance
  192. * matters - the code can just wait on the buffer
  193. * lock and re-submit.
  194. *
  195. * 2) The current request is cached, but not
  196. * readahead. ocfs2_buffer_uptodate() will return
  197. * false anyway, so we'll wind up waiting on the
  198. * buffer lock to do I/O. We re-check the request
  199. * with after getting the lock to avoid a re-submit.
  200. *
  201. * 3) The current request is readahead (and so must
  202. * also be a caching one). We short circuit if the
  203. * buffer is locked (under I/O) and if it's in the
  204. * uptodate cache. The re-check from #2 catches the
  205. * case that the previous read-ahead completes just
  206. * before our is-it-in-flight check.
  207. */
  208. if (!ignore_cache && !ocfs2_buffer_uptodate(inode, bh)) {
  209. mlog(ML_UPTODATE,
  210. "bh (%llu), inode %llu not uptodate\n",
  211. (unsigned long long)bh->b_blocknr,
  212. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  213. /* We're using ignore_cache here to say
  214. * "go to disk" */
  215. ignore_cache = 1;
  216. }
  217. /* XXX: Can we ever get this and *not* have the cached
  218. * flag set? */
  219. if (buffer_jbd(bh)) {
  220. if (ignore_cache)
  221. mlog(ML_BH_IO, "trying to sync read a jbd "
  222. "managed bh (blocknr = %llu)\n",
  223. (unsigned long long)bh->b_blocknr);
  224. continue;
  225. }
  226. if (ignore_cache) {
  227. if (buffer_dirty(bh)) {
  228. /* This should probably be a BUG, or
  229. * at least return an error. */
  230. mlog(ML_BH_IO, "asking me to sync read a dirty "
  231. "buffer! (blocknr = %llu)\n",
  232. (unsigned long long)bh->b_blocknr);
  233. continue;
  234. }
  235. /* A read-ahead request was made - if the
  236. * buffer is already under read-ahead from a
  237. * previously submitted request than we are
  238. * done here. */
  239. if ((flags & OCFS2_BH_READAHEAD)
  240. && ocfs2_buffer_read_ahead(inode, bh))
  241. continue;
  242. lock_buffer(bh);
  243. if (buffer_jbd(bh)) {
  244. #ifdef CATCH_BH_JBD_RACES
  245. mlog(ML_ERROR, "block %llu had the JBD bit set "
  246. "while I was in lock_buffer!",
  247. (unsigned long long)bh->b_blocknr);
  248. BUG();
  249. #else
  250. unlock_buffer(bh);
  251. continue;
  252. #endif
  253. }
  254. /* Re-check ocfs2_buffer_uptodate() as a
  255. * previously read-ahead buffer may have
  256. * completed I/O while we were waiting for the
  257. * buffer lock. */
  258. if (!(flags & OCFS2_BH_IGNORE_CACHE)
  259. && !(flags & OCFS2_BH_READAHEAD)
  260. && ocfs2_buffer_uptodate(inode, bh)) {
  261. unlock_buffer(bh);
  262. continue;
  263. }
  264. clear_buffer_uptodate(bh);
  265. get_bh(bh); /* for end_buffer_read_sync() */
  266. bh->b_end_io = end_buffer_read_sync;
  267. submit_bh(READ, bh);
  268. continue;
  269. }
  270. }
  271. status = 0;
  272. for (i = (nr - 1); i >= 0; i--) {
  273. bh = bhs[i];
  274. if (!(flags & OCFS2_BH_READAHEAD)) {
  275. /* We know this can't have changed as we hold the
  276. * inode sem. Avoid doing any work on the bh if the
  277. * journal has it. */
  278. if (!buffer_jbd(bh))
  279. wait_on_buffer(bh);
  280. if (!buffer_uptodate(bh)) {
  281. /* Status won't be cleared from here on out,
  282. * so we can safely record this and loop back
  283. * to cleanup the other buffers. Don't need to
  284. * remove the clustered uptodate information
  285. * for this bh as it's not marked locally
  286. * uptodate. */
  287. status = -EIO;
  288. put_bh(bh);
  289. bhs[i] = NULL;
  290. continue;
  291. }
  292. }
  293. /* Always set the buffer in the cache, even if it was
  294. * a forced read, or read-ahead which hasn't yet
  295. * completed. */
  296. ocfs2_set_buffer_uptodate(inode, bh);
  297. }
  298. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  299. mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n",
  300. (unsigned long long)block, nr,
  301. ((flags & OCFS2_BH_IGNORE_CACHE) || ignore_cache) ? "no" : "yes",
  302. flags);
  303. bail:
  304. mlog_exit(status);
  305. return status;
  306. }
  307. /* Check whether the blkno is the super block or one of the backups. */
  308. static void ocfs2_check_super_or_backup(struct super_block *sb,
  309. sector_t blkno)
  310. {
  311. int i;
  312. u64 backup_blkno;
  313. if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
  314. return;
  315. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  316. backup_blkno = ocfs2_backup_super_blkno(sb, i);
  317. if (backup_blkno == blkno)
  318. return;
  319. }
  320. BUG();
  321. }
  322. /*
  323. * Write super block and backups doesn't need to collaborate with journal,
  324. * so we don't need to lock ip_io_mutex and inode doesn't need to bea passed
  325. * into this function.
  326. */
  327. int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
  328. struct buffer_head *bh)
  329. {
  330. int ret = 0;
  331. mlog_entry_void();
  332. BUG_ON(buffer_jbd(bh));
  333. ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
  334. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
  335. ret = -EROFS;
  336. goto out;
  337. }
  338. lock_buffer(bh);
  339. set_buffer_uptodate(bh);
  340. /* remove from dirty list before I/O. */
  341. clear_buffer_dirty(bh);
  342. get_bh(bh); /* for end_buffer_write_sync() */
  343. bh->b_end_io = end_buffer_write_sync;
  344. submit_bh(WRITE, bh);
  345. wait_on_buffer(bh);
  346. if (!buffer_uptodate(bh)) {
  347. ret = -EIO;
  348. put_bh(bh);
  349. }
  350. out:
  351. mlog_exit(ret);
  352. return ret;
  353. }