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