buffer_head_io.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. /*
  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 inode *inode)
  48. {
  49. int ret = 0;
  50. mlog_entry("(bh->b_blocknr = %llu, inode=%p)\n",
  51. (unsigned long long)bh->b_blocknr, inode);
  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. goto out;
  60. }
  61. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  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(WRITE, bh);
  69. wait_on_buffer(bh);
  70. if (buffer_uptodate(bh)) {
  71. ocfs2_set_buffer_uptodate(inode, 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. put_bh(bh);
  78. }
  79. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  80. out:
  81. mlog_exit(ret);
  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. if (!nr) {
  91. mlog(ML_BH_IO, "No buffers will be read!\n");
  92. goto bail;
  93. }
  94. for (i = 0 ; i < nr ; i++) {
  95. if (bhs[i] == NULL) {
  96. bhs[i] = sb_getblk(osb->sb, block++);
  97. if (bhs[i] == NULL) {
  98. status = -EIO;
  99. mlog_errno(status);
  100. goto bail;
  101. }
  102. }
  103. bh = bhs[i];
  104. if (buffer_jbd(bh)) {
  105. mlog(ML_BH_IO,
  106. "trying to sync read a jbd "
  107. "managed bh (blocknr = %llu), skipping\n",
  108. (unsigned long long)bh->b_blocknr);
  109. continue;
  110. }
  111. if (buffer_dirty(bh)) {
  112. /* This should probably be a BUG, or
  113. * at least return an error. */
  114. mlog(ML_ERROR,
  115. "trying to sync read a dirty "
  116. "buffer! (blocknr = %llu), skipping\n",
  117. (unsigned long long)bh->b_blocknr);
  118. continue;
  119. }
  120. lock_buffer(bh);
  121. if (buffer_jbd(bh)) {
  122. mlog(ML_ERROR,
  123. "block %llu had the JBD bit set "
  124. "while I was in lock_buffer!",
  125. (unsigned long long)bh->b_blocknr);
  126. BUG();
  127. }
  128. clear_buffer_uptodate(bh);
  129. get_bh(bh); /* for end_buffer_read_sync() */
  130. bh->b_end_io = end_buffer_read_sync;
  131. submit_bh(READ, bh);
  132. }
  133. for (i = nr; i > 0; i--) {
  134. bh = bhs[i - 1];
  135. /* No need to wait on the buffer if it's managed by JBD. */
  136. if (!buffer_jbd(bh))
  137. wait_on_buffer(bh);
  138. if (!buffer_uptodate(bh)) {
  139. /* Status won't be cleared from here on out,
  140. * so we can safely record this and loop back
  141. * to cleanup the other buffers. */
  142. status = -EIO;
  143. put_bh(bh);
  144. bhs[i - 1] = NULL;
  145. }
  146. }
  147. bail:
  148. return status;
  149. }
  150. int ocfs2_read_blocks(struct inode *inode, u64 block, int nr,
  151. struct buffer_head *bhs[], int flags,
  152. int (*validate)(struct super_block *sb,
  153. struct buffer_head *bh))
  154. {
  155. int status = 0;
  156. int i, ignore_cache = 0;
  157. struct buffer_head *bh;
  158. mlog_entry("(inode=%p, block=(%llu), nr=(%d), flags=%d)\n",
  159. inode, (unsigned long long)block, nr, flags);
  160. BUG_ON(!inode);
  161. BUG_ON((flags & OCFS2_BH_READAHEAD) &&
  162. (flags & OCFS2_BH_IGNORE_CACHE));
  163. if (bhs == NULL) {
  164. status = -EINVAL;
  165. mlog_errno(status);
  166. goto bail;
  167. }
  168. if (nr < 0) {
  169. mlog(ML_ERROR, "asked to read %d blocks!\n", nr);
  170. status = -EINVAL;
  171. mlog_errno(status);
  172. goto bail;
  173. }
  174. if (nr == 0) {
  175. mlog(ML_BH_IO, "No buffers will be read!\n");
  176. status = 0;
  177. goto bail;
  178. }
  179. mutex_lock(&OCFS2_I(inode)->ip_io_mutex);
  180. for (i = 0 ; i < nr ; i++) {
  181. if (bhs[i] == NULL) {
  182. bhs[i] = sb_getblk(inode->i_sb, block++);
  183. if (bhs[i] == NULL) {
  184. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  185. status = -EIO;
  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(inode, bh)) {
  216. mlog(ML_UPTODATE,
  217. "bh (%llu), inode %llu not uptodate\n",
  218. (unsigned long long)bh->b_blocknr,
  219. (unsigned long long)OCFS2_I(inode)->ip_blkno);
  220. /* We're using ignore_cache here to say
  221. * "go to disk" */
  222. ignore_cache = 1;
  223. }
  224. if (buffer_jbd(bh)) {
  225. if (ignore_cache)
  226. mlog(ML_BH_IO, "trying to sync read a jbd "
  227. "managed bh (blocknr = %llu)\n",
  228. (unsigned long long)bh->b_blocknr);
  229. continue;
  230. }
  231. if (ignore_cache) {
  232. if (buffer_dirty(bh)) {
  233. /* This should probably be a BUG, or
  234. * at least return an error. */
  235. mlog(ML_BH_IO, "asking me to sync read a dirty "
  236. "buffer! (blocknr = %llu)\n",
  237. (unsigned long long)bh->b_blocknr);
  238. continue;
  239. }
  240. /* A read-ahead request was made - if the
  241. * buffer is already under read-ahead from a
  242. * previously submitted request than we are
  243. * done here. */
  244. if ((flags & OCFS2_BH_READAHEAD)
  245. && ocfs2_buffer_read_ahead(inode, bh))
  246. continue;
  247. lock_buffer(bh);
  248. if (buffer_jbd(bh)) {
  249. #ifdef CATCH_BH_JBD_RACES
  250. mlog(ML_ERROR, "block %llu had the JBD bit set "
  251. "while I was in lock_buffer!",
  252. (unsigned long long)bh->b_blocknr);
  253. BUG();
  254. #else
  255. unlock_buffer(bh);
  256. continue;
  257. #endif
  258. }
  259. /* Re-check ocfs2_buffer_uptodate() as a
  260. * previously read-ahead buffer may have
  261. * completed I/O while we were waiting for the
  262. * buffer lock. */
  263. if (!(flags & OCFS2_BH_IGNORE_CACHE)
  264. && !(flags & OCFS2_BH_READAHEAD)
  265. && ocfs2_buffer_uptodate(inode, bh)) {
  266. unlock_buffer(bh);
  267. continue;
  268. }
  269. clear_buffer_uptodate(bh);
  270. get_bh(bh); /* for end_buffer_read_sync() */
  271. if (validate)
  272. set_buffer_needs_validate(bh);
  273. bh->b_end_io = end_buffer_read_sync;
  274. submit_bh(READ, bh);
  275. continue;
  276. }
  277. }
  278. status = 0;
  279. for (i = (nr - 1); i >= 0; i--) {
  280. bh = bhs[i];
  281. if (!(flags & OCFS2_BH_READAHEAD)) {
  282. /* We know this can't have changed as we hold the
  283. * inode 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(inode->i_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(inode, bh);
  317. }
  318. mutex_unlock(&OCFS2_I(inode)->ip_io_mutex);
  319. mlog(ML_BH_IO, "block=(%llu), nr=(%d), cached=%s, flags=0x%x\n",
  320. (unsigned long long)block, nr,
  321. ((flags & OCFS2_BH_IGNORE_CACHE) || ignore_cache) ? "no" : "yes",
  322. flags);
  323. bail:
  324. mlog_exit(status);
  325. return status;
  326. }
  327. /* Check whether the blkno is the super block or one of the backups. */
  328. static void ocfs2_check_super_or_backup(struct super_block *sb,
  329. sector_t blkno)
  330. {
  331. int i;
  332. u64 backup_blkno;
  333. if (blkno == OCFS2_SUPER_BLOCK_BLKNO)
  334. return;
  335. for (i = 0; i < OCFS2_MAX_BACKUP_SUPERBLOCKS; i++) {
  336. backup_blkno = ocfs2_backup_super_blkno(sb, i);
  337. if (backup_blkno == blkno)
  338. return;
  339. }
  340. BUG();
  341. }
  342. /*
  343. * Write super block and backups doesn't need to collaborate with journal,
  344. * so we don't need to lock ip_io_mutex and inode doesn't need to bea passed
  345. * into this function.
  346. */
  347. int ocfs2_write_super_or_backup(struct ocfs2_super *osb,
  348. struct buffer_head *bh)
  349. {
  350. int ret = 0;
  351. mlog_entry_void();
  352. BUG_ON(buffer_jbd(bh));
  353. ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
  354. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
  355. ret = -EROFS;
  356. goto out;
  357. }
  358. lock_buffer(bh);
  359. set_buffer_uptodate(bh);
  360. /* remove from dirty list before I/O. */
  361. clear_buffer_dirty(bh);
  362. get_bh(bh); /* for end_buffer_write_sync() */
  363. bh->b_end_io = end_buffer_write_sync;
  364. submit_bh(WRITE, bh);
  365. wait_on_buffer(bh);
  366. if (!buffer_uptodate(bh)) {
  367. ret = -EIO;
  368. put_bh(bh);
  369. }
  370. out:
  371. mlog_exit(ret);
  372. return ret;
  373. }