buffer_head_io.c 12 KB

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