buffer_head_io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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/highmem.h>
  28. #include <cluster/masklog.h>
  29. #include "ocfs2.h"
  30. #include "alloc.h"
  31. #include "inode.h"
  32. #include "journal.h"
  33. #include "uptodate.h"
  34. #include "buffer_head_io.h"
  35. #include "ocfs2_trace.h"
  36. /*
  37. * Bits on bh->b_state used by ocfs2.
  38. *
  39. * These MUST be after the JBD2 bits. Hence, we use BH_JBDPrivateStart.
  40. */
  41. enum ocfs2_state_bits {
  42. BH_NeedsValidate = BH_JBDPrivateStart,
  43. };
  44. /* Expand the magic b_state functions */
  45. BUFFER_FNS(NeedsValidate, needs_validate);
  46. int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
  47. struct ocfs2_caching_info *ci)
  48. {
  49. int ret = 0;
  50. trace_ocfs2_write_block((unsigned long long)bh->b_blocknr, ci);
  51. BUG_ON(bh->b_blocknr < OCFS2_SUPER_BLOCK_BLKNO);
  52. 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. mlog_errno(ret);
  59. goto out;
  60. }
  61. ocfs2_metadata_cache_io_lock(ci);
  62. lock_buffer(bh);
  63. set_buffer_uptodate(bh);
  64. /* remove from dirty list before I/O. */
  65. clear_buffer_dirty(bh);
  66. get_bh(bh); /* for end_buffer_write_sync() */
  67. bh->b_end_io = end_buffer_write_sync;
  68. submit_bh(REQ_OP_WRITE, 0, bh);
  69. wait_on_buffer(bh);
  70. if (buffer_uptodate(bh)) {
  71. ocfs2_set_buffer_uptodate(ci, bh);
  72. } else {
  73. /* We don't need to remove the clustered uptodate
  74. * information for this bh as it's not marked locally
  75. * uptodate. */
  76. ret = -EIO;
  77. mlog_errno(ret);
  78. }
  79. ocfs2_metadata_cache_io_unlock(ci);
  80. out:
  81. return ret;
  82. }
  83. int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
  84. unsigned int nr, struct buffer_head *bhs[])
  85. {
  86. int status = 0;
  87. unsigned int i;
  88. struct buffer_head *bh;
  89. trace_ocfs2_read_blocks_sync((unsigned long long)block, nr);
  90. if (!nr)
  91. goto bail;
  92. for (i = 0 ; i < nr ; i++) {
  93. if (bhs[i] == NULL) {
  94. bhs[i] = sb_getblk(osb->sb, block++);
  95. if (bhs[i] == NULL) {
  96. status = -ENOMEM;
  97. mlog_errno(status);
  98. goto bail;
  99. }
  100. }
  101. bh = bhs[i];
  102. if (buffer_jbd(bh)) {
  103. trace_ocfs2_read_blocks_sync_jbd(
  104. (unsigned long long)bh->b_blocknr);
  105. continue;
  106. }
  107. if (buffer_dirty(bh)) {
  108. /* This should probably be a BUG, or
  109. * at least return an error. */
  110. mlog(ML_ERROR,
  111. "trying to sync read a dirty "
  112. "buffer! (blocknr = %llu), skipping\n",
  113. (unsigned long long)bh->b_blocknr);
  114. continue;
  115. }
  116. lock_buffer(bh);
  117. if (buffer_jbd(bh)) {
  118. #ifdef CATCH_BH_JBD_RACES
  119. mlog(ML_ERROR,
  120. "block %llu had the JBD bit set "
  121. "while I was in lock_buffer!",
  122. (unsigned long long)bh->b_blocknr);
  123. BUG();
  124. #else
  125. unlock_buffer(bh);
  126. continue;
  127. #endif
  128. }
  129. clear_buffer_uptodate(bh);
  130. get_bh(bh); /* for end_buffer_read_sync() */
  131. bh->b_end_io = end_buffer_read_sync;
  132. submit_bh(REQ_OP_READ, 0, bh);
  133. }
  134. for (i = nr; i > 0; i--) {
  135. bh = bhs[i - 1];
  136. /* No need to wait on the buffer if it's managed by JBD. */
  137. if (!buffer_jbd(bh))
  138. wait_on_buffer(bh);
  139. if (!buffer_uptodate(bh)) {
  140. /* Status won't be cleared from here on out,
  141. * so we can safely record this and loop back
  142. * to cleanup the other buffers. */
  143. status = -EIO;
  144. put_bh(bh);
  145. bhs[i - 1] = NULL;
  146. }
  147. }
  148. bail:
  149. return status;
  150. }
  151. int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
  152. struct buffer_head *bhs[], int flags,
  153. int (*validate)(struct super_block *sb,
  154. struct buffer_head *bh))
  155. {
  156. int status = 0;
  157. int i, ignore_cache = 0;
  158. struct buffer_head *bh;
  159. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  160. trace_ocfs2_read_blocks_begin(ci, (unsigned long long)block, nr, flags);
  161. BUG_ON(!ci);
  162. BUG_ON((flags & OCFS2_BH_READAHEAD) &&
  163. (flags & OCFS2_BH_IGNORE_CACHE));
  164. if (bhs == NULL) {
  165. status = -EINVAL;
  166. mlog_errno(status);
  167. goto bail;
  168. }
  169. if (nr < 0) {
  170. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  171. status = -EINVAL;
  172. mlog_errno(status);
  173. goto bail;
  174. }
  175. if (nr == 0) {
  176. status = 0;
  177. goto bail;
  178. }
  179. ocfs2_metadata_cache_io_lock(ci);
  180. for (i = 0 ; i < nr ; i++) {
  181. if (bhs[i] == NULL) {
  182. bhs[i] = sb_getblk(sb, block++);
  183. if (bhs[i] == NULL) {
  184. ocfs2_metadata_cache_io_unlock(ci);
  185. status = -ENOMEM;
  186. mlog_errno(status);
  187. goto bail;
  188. }
  189. }
  190. bh = bhs[i];
  191. ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
  192. /* There are three read-ahead cases here which we need to
  193. * be concerned with. All three assume a buffer has
  194. * previously been submitted with OCFS2_BH_READAHEAD
  195. * and it hasn't yet completed I/O.
  196. *
  197. * 1) The current request is sync to disk. This rarely
  198. * happens these days, and never when performance
  199. * matters - the code can just wait on the buffer
  200. * lock and re-submit.
  201. *
  202. * 2) The current request is cached, but not
  203. * readahead. ocfs2_buffer_uptodate() will return
  204. * false anyway, so we'll wind up waiting on the
  205. * buffer lock to do I/O. We re-check the request
  206. * with after getting the lock to avoid a re-submit.
  207. *
  208. * 3) The current request is readahead (and so must
  209. * also be a caching one). We short circuit if the
  210. * buffer is locked (under I/O) and if it's in the
  211. * uptodate cache. The re-check from #2 catches the
  212. * case that the previous read-ahead completes just
  213. * before our is-it-in-flight check.
  214. */
  215. if (!ignore_cache && !ocfs2_buffer_uptodate(ci, bh)) {
  216. trace_ocfs2_read_blocks_from_disk(
  217. (unsigned long long)bh->b_blocknr,
  218. (unsigned long long)ocfs2_metadata_cache_owner(ci));
  219. /* We're using ignore_cache here to say
  220. * "go to disk" */
  221. ignore_cache = 1;
  222. }
  223. trace_ocfs2_read_blocks_bh((unsigned long long)bh->b_blocknr,
  224. ignore_cache, buffer_jbd(bh), buffer_dirty(bh));
  225. if (buffer_jbd(bh)) {
  226. continue;
  227. }
  228. if (ignore_cache) {
  229. if (buffer_dirty(bh)) {
  230. /* This should probably be a BUG, or
  231. * at least return an error. */
  232. continue;
  233. }
  234. /* A read-ahead request was made - if the
  235. * buffer is already under read-ahead from a
  236. * previously submitted request than we are
  237. * done here. */
  238. if ((flags & OCFS2_BH_READAHEAD)
  239. && ocfs2_buffer_read_ahead(ci, bh))
  240. continue;
  241. lock_buffer(bh);
  242. if (buffer_jbd(bh)) {
  243. #ifdef CATCH_BH_JBD_RACES
  244. mlog(ML_ERROR, "block %llu had the JBD bit set "
  245. "while I was in lock_buffer!",
  246. (unsigned long long)bh->b_blocknr);
  247. BUG();
  248. #else
  249. unlock_buffer(bh);
  250. continue;
  251. #endif
  252. }
  253. /* Re-check ocfs2_buffer_uptodate() as a
  254. * previously read-ahead buffer may have
  255. * completed I/O while we were waiting for the
  256. * buffer lock. */
  257. if (!(flags & OCFS2_BH_IGNORE_CACHE)
  258. && !(flags & OCFS2_BH_READAHEAD)
  259. && ocfs2_buffer_uptodate(ci, bh)) {
  260. unlock_buffer(bh);
  261. continue;
  262. }
  263. clear_buffer_uptodate(bh);
  264. get_bh(bh); /* for end_buffer_read_sync() */
  265. if (validate)
  266. set_buffer_needs_validate(bh);
  267. bh->b_end_io = end_buffer_read_sync;
  268. submit_bh(REQ_OP_READ, 0, bh);
  269. continue;
  270. }
  271. }
  272. status = 0;
  273. for (i = (nr - 1); i >= 0; i--) {
  274. bh = bhs[i];
  275. if (!(flags & OCFS2_BH_READAHEAD)) {
  276. if (status) {
  277. /* Clear the rest of the buffers on error */
  278. put_bh(bh);
  279. bhs[i] = NULL;
  280. continue;
  281. }
  282. /* We know this can't have changed as we hold the
  283. * owner sem. Avoid doing any work on the bh if the
  284. * journal has it. */
  285. if (!buffer_jbd(bh))
  286. wait_on_buffer(bh);
  287. if (!buffer_uptodate(bh)) {
  288. /* Status won't be cleared from here on out,
  289. * so we can safely record this and loop back
  290. * to cleanup the other buffers. Don't need to
  291. * remove the clustered uptodate information
  292. * for this bh as it's not marked locally
  293. * uptodate. */
  294. status = -EIO;
  295. put_bh(bh);
  296. bhs[i] = NULL;
  297. continue;
  298. }
  299. if (buffer_needs_validate(bh)) {
  300. /* We never set NeedsValidate if the
  301. * buffer was held by the journal, so
  302. * that better not have changed */
  303. BUG_ON(buffer_jbd(bh));
  304. clear_buffer_needs_validate(bh);
  305. status = validate(sb, bh);
  306. if (status) {
  307. put_bh(bh);
  308. bhs[i] = NULL;
  309. continue;
  310. }
  311. }
  312. }
  313. /* Always set the buffer in the cache, even if it was
  314. * a forced read, or read-ahead which hasn't yet
  315. * completed. */
  316. ocfs2_set_buffer_uptodate(ci, bh);
  317. }
  318. ocfs2_metadata_cache_io_unlock(ci);
  319. trace_ocfs2_read_blocks_end((unsigned long long)block, nr,
  320. flags, ignore_cache);
  321. bail:
  322. return status;
  323. }
  324. /* Check whether the blkno is the super block or one of the backups. */
  325. static void ocfs2_check_super_or_backup(struct super_block *sb,
  326. sector_t blkno)
  327. {
  328. int i;
  329. u64 backup_blkno;
  330. if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
  331. return;
  332. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  333. backup_blkno = ocfs2_backup_super_blkno(sb, i);
  334. if (backup_blkno == blkno)
  335. return;
  336. }
  337. BUG();
  338. }
  339. /*
  340. * Write super block and backups doesn't need to collaborate with journal,
  341. * so we don't need to lock ip_io_mutex and ci doesn't need to bea passed
  342. * into this function.
  343. */
  344. int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
  345. struct buffer_head *bh)
  346. {
  347. int ret = 0;
  348. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  349. BUG_ON(buffer_jbd(bh));
  350. ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
  351. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
  352. ret = -EROFS;
  353. mlog_errno(ret);
  354. goto out;
  355. }
  356. lock_buffer(bh);
  357. set_buffer_uptodate(bh);
  358. /* remove from dirty list before I/O. */
  359. clear_buffer_dirty(bh);
  360. get_bh(bh); /* for end_buffer_write_sync() */
  361. bh->b_end_io = end_buffer_write_sync;
  362. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &di->i_check);
  363. submit_bh(REQ_OP_WRITE, 0, bh);
  364. wait_on_buffer(bh);
  365. if (!buffer_uptodate(bh)) {
  366. ret = -EIO;
  367. mlog_errno(ret);
  368. }
  369. out:
  370. return ret;
  371. }