buffer_head_io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. /* Caller must provide a bhs[] with all NULL or non-NULL entries, so it
  85. * will be easier to handle read failure.
  86. */
  87. int ocfs2_read_blocks_sync(struct ocfs2_super *osb, u64 block,
  88. unsigned int nr, struct buffer_head *bhs[])
  89. {
  90. int status = 0;
  91. unsigned int i;
  92. struct buffer_head *bh;
  93. int new_bh = 0;
  94. trace_ocfs2_read_blocks_sync((unsigned long long)block, nr);
  95. if (!nr)
  96. goto bail;
  97. /* Don't put buffer head and re-assign it to NULL if it is allocated
  98. * outside since the caller can't be aware of this alternation!
  99. */
  100. new_bh = (bhs[0] == NULL);
  101. for (i = 0 ; i < nr ; i++) {
  102. if (bhs[i] == NULL) {
  103. bhs[i] = sb_getblk(osb->sb, block++);
  104. if (bhs[i] == NULL) {
  105. status = -ENOMEM;
  106. mlog_errno(status);
  107. break;
  108. }
  109. }
  110. bh = bhs[i];
  111. if (buffer_jbd(bh)) {
  112. trace_ocfs2_read_blocks_sync_jbd(
  113. (unsigned long long)bh->b_blocknr);
  114. continue;
  115. }
  116. if (buffer_dirty(bh)) {
  117. /* This should probably be a BUG, or
  118. * at least return an error. */
  119. mlog(ML_ERROR,
  120. "trying to sync read a dirty "
  121. "buffer! (blocknr = %llu), skipping\n",
  122. (unsigned long long)bh->b_blocknr);
  123. continue;
  124. }
  125. lock_buffer(bh);
  126. if (buffer_jbd(bh)) {
  127. #ifdef CATCH_BH_JBD_RACES
  128. mlog(ML_ERROR,
  129. "block %llu had the JBD bit set "
  130. "while I was in lock_buffer!",
  131. (unsigned long long)bh->b_blocknr);
  132. BUG();
  133. #else
  134. unlock_buffer(bh);
  135. continue;
  136. #endif
  137. }
  138. clear_buffer_uptodate(bh);
  139. get_bh(bh); /* for end_buffer_read_sync() */
  140. bh->b_end_io = end_buffer_read_sync;
  141. submit_bh(REQ_OP_READ, 0, bh);
  142. }
  143. read_failure:
  144. for (i = nr; i > 0; i--) {
  145. bh = bhs[i - 1];
  146. if (unlikely(status)) {
  147. if (new_bh && bh) {
  148. /* If middle bh fails, let previous bh
  149. * finish its read and then put it to
  150. * aovoid bh leak
  151. */
  152. if (!buffer_jbd(bh))
  153. wait_on_buffer(bh);
  154. put_bh(bh);
  155. bhs[i - 1] = NULL;
  156. } else if (bh && buffer_uptodate(bh)) {
  157. clear_buffer_uptodate(bh);
  158. }
  159. continue;
  160. }
  161. /* No need to wait on the buffer if it's managed by JBD. */
  162. if (!buffer_jbd(bh))
  163. wait_on_buffer(bh);
  164. if (!buffer_uptodate(bh)) {
  165. /* Status won't be cleared from here on out,
  166. * so we can safely record this and loop back
  167. * to cleanup the other buffers. */
  168. status = -EIO;
  169. goto read_failure;
  170. }
  171. }
  172. bail:
  173. return status;
  174. }
  175. /* Caller must provide a bhs[] with all NULL or non-NULL entries, so it
  176. * will be easier to handle read failure.
  177. */
  178. int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
  179. struct buffer_head *bhs[], int flags,
  180. int (*validate)(struct super_block *sb,
  181. struct buffer_head *bh))
  182. {
  183. int status = 0;
  184. int i, ignore_cache = 0;
  185. struct buffer_head *bh;
  186. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  187. int new_bh = 0;
  188. trace_ocfs2_read_blocks_begin(ci, (unsigned long long)block, nr, flags);
  189. BUG_ON(!ci);
  190. BUG_ON((flags & OCFS2_BH_READAHEAD) &&
  191. (flags & OCFS2_BH_IGNORE_CACHE));
  192. if (bhs == NULL) {
  193. status = -EINVAL;
  194. mlog_errno(status);
  195. goto bail;
  196. }
  197. if (nr < 0) {
  198. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  199. status = -EINVAL;
  200. mlog_errno(status);
  201. goto bail;
  202. }
  203. if (nr == 0) {
  204. status = 0;
  205. goto bail;
  206. }
  207. /* Don't put buffer head and re-assign it to NULL if it is allocated
  208. * outside since the caller can't be aware of this alternation!
  209. */
  210. new_bh = (bhs[0] == NULL);
  211. ocfs2_metadata_cache_io_lock(ci);
  212. for (i = 0 ; i < nr ; i++) {
  213. if (bhs[i] == NULL) {
  214. bhs[i] = sb_getblk(sb, block++);
  215. if (bhs[i] == NULL) {
  216. ocfs2_metadata_cache_io_unlock(ci);
  217. status = -ENOMEM;
  218. mlog_errno(status);
  219. /* Don't forget to put previous bh! */
  220. break;
  221. }
  222. }
  223. bh = bhs[i];
  224. ignore_cache = (flags & OCFS2_BH_IGNORE_CACHE);
  225. /* There are three read-ahead cases here which we need to
  226. * be concerned with. All three assume a buffer has
  227. * previously been submitted with OCFS2_BH_READAHEAD
  228. * and it hasn't yet completed I/O.
  229. *
  230. * 1) The current request is sync to disk. This rarely
  231. * happens these days, and never when performance
  232. * matters - the code can just wait on the buffer
  233. * lock and re-submit.
  234. *
  235. * 2) The current request is cached, but not
  236. * readahead. ocfs2_buffer_uptodate() will return
  237. * false anyway, so we'll wind up waiting on the
  238. * buffer lock to do I/O. We re-check the request
  239. * with after getting the lock to avoid a re-submit.
  240. *
  241. * 3) The current request is readahead (and so must
  242. * also be a caching one). We short circuit if the
  243. * buffer is locked (under I/O) and if it's in the
  244. * uptodate cache. The re-check from #2 catches the
  245. * case that the previous read-ahead completes just
  246. * before our is-it-in-flight check.
  247. */
  248. if (!ignore_cache && !ocfs2_buffer_uptodate(ci, bh)) {
  249. trace_ocfs2_read_blocks_from_disk(
  250. (unsigned long long)bh->b_blocknr,
  251. (unsigned long long)ocfs2_metadata_cache_owner(ci));
  252. /* We're using ignore_cache here to say
  253. * "go to disk" */
  254. ignore_cache = 1;
  255. }
  256. trace_ocfs2_read_blocks_bh((unsigned long long)bh->b_blocknr,
  257. ignore_cache, buffer_jbd(bh), buffer_dirty(bh));
  258. if (buffer_jbd(bh)) {
  259. continue;
  260. }
  261. if (ignore_cache) {
  262. if (buffer_dirty(bh)) {
  263. /* This should probably be a BUG, or
  264. * at least return an error. */
  265. continue;
  266. }
  267. /* A read-ahead request was made - if the
  268. * buffer is already under read-ahead from a
  269. * previously submitted request than we are
  270. * done here. */
  271. if ((flags & OCFS2_BH_READAHEAD)
  272. && ocfs2_buffer_read_ahead(ci, bh))
  273. continue;
  274. lock_buffer(bh);
  275. if (buffer_jbd(bh)) {
  276. #ifdef CATCH_BH_JBD_RACES
  277. mlog(ML_ERROR, "block %llu had the JBD bit set "
  278. "while I was in lock_buffer!",
  279. (unsigned long long)bh->b_blocknr);
  280. BUG();
  281. #else
  282. unlock_buffer(bh);
  283. continue;
  284. #endif
  285. }
  286. /* Re-check ocfs2_buffer_uptodate() as a
  287. * previously read-ahead buffer may have
  288. * completed I/O while we were waiting for the
  289. * buffer lock. */
  290. if (!(flags & OCFS2_BH_IGNORE_CACHE)
  291. && !(flags & OCFS2_BH_READAHEAD)
  292. && ocfs2_buffer_uptodate(ci, bh)) {
  293. unlock_buffer(bh);
  294. continue;
  295. }
  296. clear_buffer_uptodate(bh);
  297. get_bh(bh); /* for end_buffer_read_sync() */
  298. if (validate)
  299. set_buffer_needs_validate(bh);
  300. bh->b_end_io = end_buffer_read_sync;
  301. submit_bh(REQ_OP_READ, 0, bh);
  302. continue;
  303. }
  304. }
  305. read_failure:
  306. for (i = (nr - 1); i >= 0; i--) {
  307. bh = bhs[i];
  308. if (!(flags & OCFS2_BH_READAHEAD)) {
  309. if (unlikely(status)) {
  310. /* Clear the buffers on error including those
  311. * ever succeeded in reading
  312. */
  313. if (new_bh && bh) {
  314. /* If middle bh fails, let previous bh
  315. * finish its read and then put it to
  316. * aovoid bh leak
  317. */
  318. if (!buffer_jbd(bh))
  319. wait_on_buffer(bh);
  320. put_bh(bh);
  321. bhs[i] = NULL;
  322. } else if (bh && buffer_uptodate(bh)) {
  323. clear_buffer_uptodate(bh);
  324. }
  325. continue;
  326. }
  327. /* We know this can't have changed as we hold the
  328. * owner sem. Avoid doing any work on the bh if the
  329. * journal has it. */
  330. if (!buffer_jbd(bh))
  331. wait_on_buffer(bh);
  332. if (!buffer_uptodate(bh)) {
  333. /* Status won't be cleared from here on out,
  334. * so we can safely record this and loop back
  335. * to cleanup the other buffers. Don't need to
  336. * remove the clustered uptodate information
  337. * for this bh as it's not marked locally
  338. * uptodate. */
  339. status = -EIO;
  340. clear_buffer_needs_validate(bh);
  341. goto read_failure;
  342. }
  343. if (buffer_needs_validate(bh)) {
  344. /* We never set NeedsValidate if the
  345. * buffer was held by the journal, so
  346. * that better not have changed */
  347. BUG_ON(buffer_jbd(bh));
  348. clear_buffer_needs_validate(bh);
  349. status = validate(sb, bh);
  350. if (status)
  351. goto read_failure;
  352. }
  353. }
  354. /* Always set the buffer in the cache, even if it was
  355. * a forced read, or read-ahead which hasn't yet
  356. * completed. */
  357. ocfs2_set_buffer_uptodate(ci, bh);
  358. }
  359. ocfs2_metadata_cache_io_unlock(ci);
  360. trace_ocfs2_read_blocks_end((unsigned long long)block, nr,
  361. flags, ignore_cache);
  362. bail:
  363. return status;
  364. }
  365. /* Check whether the blkno is the super block or one of the backups. */
  366. static void ocfs2_check_super_or_backup(struct super_block *sb,
  367. sector_t blkno)
  368. {
  369. int i;
  370. u64 backup_blkno;
  371. if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
  372. return;
  373. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  374. backup_blkno = ocfs2_backup_super_blkno(sb, i);
  375. if (backup_blkno == blkno)
  376. return;
  377. }
  378. BUG();
  379. }
  380. /*
  381. * Write super block and backups doesn't need to collaborate with journal,
  382. * so we don't need to lock ip_io_mutex and ci doesn't need to bea passed
  383. * into this function.
  384. */
  385. int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
  386. struct buffer_head *bh)
  387. {
  388. int ret = 0;
  389. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  390. BUG_ON(buffer_jbd(bh));
  391. ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
  392. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
  393. ret = -EROFS;
  394. mlog_errno(ret);
  395. goto out;
  396. }
  397. lock_buffer(bh);
  398. set_buffer_uptodate(bh);
  399. /* remove from dirty list before I/O. */
  400. clear_buffer_dirty(bh);
  401. get_bh(bh); /* for end_buffer_write_sync() */
  402. bh->b_end_io = end_buffer_write_sync;
  403. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &di->i_check);
  404. submit_bh(REQ_OP_WRITE, 0, bh);
  405. wait_on_buffer(bh);
  406. if (!buffer_uptodate(bh)) {
  407. ret = -EIO;
  408. mlog_errno(ret);
  409. }
  410. out:
  411. return ret;
  412. }