PageRenderTime 25ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/ocfs2/buffer_head_io.c

https://github.com/Dabary/linux_gt-i9000
C | 442 lines | 275 code | 67 blank | 100 comment | 53 complexity | 0cf82cefa4dce2d4cb631d2101d02560 MD5 | raw file
  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. /*
  36. * Bits on bh->b_state used by ocfs2.
  37. *
  38. * These MUST be after the JBD2 bits. Hence, we use BH_JBDPrivateStart.
  39. */
  40. enum ocfs2_state_bits {
  41. BH_NeedsValidate = BH_JBDPrivateStart,
  42. };
  43. /* Expand the magic b_state functions */
  44. BUFFER_FNS(NeedsValidate, needs_validate);
  45. int ocfs2_write_block(struct ocfs2_super *osb, struct buffer_head *bh,
  46. struct ocfs2_caching_info *ci)
  47. {
  48. int ret = 0;
  49. mlog_entry("(bh->b_blocknr = %llu, ci=%p)\n",
  50. (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. goto out;
  59. }
  60. ocfs2_metadata_cache_io_lock(ci);
  61. lock_buffer(bh);
  62. set_buffer_uptodate(bh);
  63. /* remove from dirty list before I/O. */
  64. clear_buffer_dirty(bh);
  65. get_bh(bh); /* for end_buffer_write_sync() */
  66. bh->b_end_io = end_buffer_write_sync;
  67. submit_bh(WRITE, bh);
  68. wait_on_buffer(bh);
  69. if (buffer_uptodate(bh)) {
  70. ocfs2_set_buffer_uptodate(ci, bh);
  71. } else {
  72. /* We don't need to remove the clustered uptodate
  73. * information for this bh as it's not marked locally
  74. * uptodate. */
  75. ret = -EIO;
  76. put_bh(bh);
  77. }
  78. ocfs2_metadata_cache_io_unlock(ci);
  79. out:
  80. mlog_exit(ret);
  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. if (!nr) {
  90. mlog(ML_BH_IO, "No buffers will be read!\n");
  91. goto bail;
  92. }
  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 = -EIO;
  98. mlog_errno(status);
  99. goto bail;
  100. }
  101. }
  102. bh = bhs[i];
  103. if (buffer_jbd(bh)) {
  104. mlog(ML_BH_IO,
  105. "trying to sync read a jbd "
  106. "managed bh (blocknr = %llu), skipping\n",
  107. (unsigned long long)bh->b_blocknr);
  108. continue;
  109. }
  110. if (buffer_dirty(bh)) {
  111. /* This should probably be a BUG, or
  112. * at least return an error. */
  113. mlog(ML_ERROR,
  114. "trying to sync read a dirty "
  115. "buffer! (blocknr = %llu), skipping\n",
  116. (unsigned long long)bh->b_blocknr);
  117. continue;
  118. }
  119. lock_buffer(bh);
  120. if (buffer_jbd(bh)) {
  121. mlog(ML_ERROR,
  122. "block %llu had the JBD bit set "
  123. "while I was in lock_buffer!",
  124. (unsigned long long)bh->b_blocknr);
  125. BUG();
  126. }
  127. clear_buffer_uptodate(bh);
  128. get_bh(bh); /* for end_buffer_read_sync() */
  129. bh->b_end_io = end_buffer_read_sync;
  130. submit_bh(READ, bh);
  131. }
  132. for (i = nr; i > 0; i--) {
  133. bh = bhs[i - 1];
  134. /* No need to wait on the buffer if it's managed by JBD. */
  135. if (!buffer_jbd(bh))
  136. wait_on_buffer(bh);
  137. if (!buffer_uptodate(bh)) {
  138. /* Status won't be cleared from here on out,
  139. * so we can safely record this and loop back
  140. * to cleanup the other buffers. */
  141. status = -EIO;
  142. put_bh(bh);
  143. bhs[i - 1] = NULL;
  144. }
  145. }
  146. bail:
  147. return status;
  148. }
  149. int ocfs2_read_blocks(struct ocfs2_caching_info *ci, u64 block, int nr,
  150. struct buffer_head *bhs[], int flags,
  151. int (*validate)(struct super_block *sb,
  152. struct buffer_head *bh))
  153. {
  154. int status = 0;
  155. int i, ignore_cache = 0;
  156. struct buffer_head *bh;
  157. struct super_block *sb = ocfs2_metadata_cache_get_super(ci);
  158. mlog_entry("(ci=%p, block=(%llu), nr=(%d), flags=%d)\n",
  159. ci, (unsigned long long)block, nr, flags);
  160. BUG_ON(!ci);
  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. 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 = -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(ci, bh)) {
  216. mlog(ML_UPTODATE,
  217. "bh (%llu), owner %llu not uptodate\n",
  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. 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(ci, 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(ci, 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. * 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. 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 ci 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. struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data;
  352. mlog_entry_void();
  353. BUG_ON(buffer_jbd(bh));
  354. ocfs2_check_super_or_backup(osb->sb, bh->b_blocknr);
  355. if (ocfs2_is_hard_readonly(osb) || ocfs2_is_soft_readonly(osb)) {
  356. ret = -EROFS;
  357. goto out;
  358. }
  359. lock_buffer(bh);
  360. set_buffer_uptodate(bh);
  361. /* remove from dirty list before I/O. */
  362. clear_buffer_dirty(bh);
  363. get_bh(bh); /* for end_buffer_write_sync() */
  364. bh->b_end_io = end_buffer_write_sync;
  365. ocfs2_compute_meta_ecc(osb->sb, bh->b_data, &di->i_check);
  366. submit_bh(WRITE, bh);
  367. wait_on_buffer(bh);
  368. if (!buffer_uptodate(bh)) {
  369. ret = -EIO;
  370. put_bh(bh);
  371. }
  372. out:
  373. mlog_exit(ret);
  374. return ret;
  375. }