ext4_jbd2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * Interface between ext4 and JBD
  3. */
  4. #include "ext4_jbd2.h"
  5. #include <trace/events/ext4.h>
  6. /* Just increment the non-pointer handle value */
  7. static handle_t *ext4_get_nojournal(void)
  8. {
  9. handle_t *handle = current->journal_info;
  10. unsigned long ref_cnt = (unsigned long)handle;
  11. BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
  12. ref_cnt++;
  13. handle = (handle_t *)ref_cnt;
  14. current->journal_info = handle;
  15. return handle;
  16. }
  17. /* Decrement the non-pointer handle value */
  18. static void ext4_put_nojournal(handle_t *handle)
  19. {
  20. unsigned long ref_cnt = (unsigned long)handle;
  21. BUG_ON(ref_cnt == 0);
  22. ref_cnt--;
  23. handle = (handle_t *)ref_cnt;
  24. current->journal_info = handle;
  25. }
  26. /*
  27. * Wrappers for jbd2_journal_start/end.
  28. */
  29. handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
  30. int type, int nblocks)
  31. {
  32. journal_t *journal;
  33. trace_ext4_journal_start(sb, nblocks, _RET_IP_);
  34. if (sb->s_flags & MS_RDONLY)
  35. return ERR_PTR(-EROFS);
  36. WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
  37. journal = EXT4_SB(sb)->s_journal;
  38. if (!journal)
  39. return ext4_get_nojournal();
  40. /*
  41. * Special case here: if the journal has aborted behind our
  42. * backs (eg. EIO in the commit thread), then we still need to
  43. * take the FS itself readonly cleanly.
  44. */
  45. if (is_journal_aborted(journal)) {
  46. ext4_abort(sb, "Detected aborted journal");
  47. return ERR_PTR(-EROFS);
  48. }
  49. return jbd2__journal_start(journal, nblocks, GFP_NOFS, type, line);
  50. }
  51. int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
  52. {
  53. struct super_block *sb;
  54. int err;
  55. int rc;
  56. if (!ext4_handle_valid(handle)) {
  57. ext4_put_nojournal(handle);
  58. return 0;
  59. }
  60. sb = handle->h_transaction->t_journal->j_private;
  61. err = handle->h_err;
  62. rc = jbd2_journal_stop(handle);
  63. if (!err)
  64. err = rc;
  65. if (err)
  66. __ext4_std_error(sb, where, line, err);
  67. return err;
  68. }
  69. void ext4_journal_abort_handle(const char *caller, unsigned int line,
  70. const char *err_fn, struct buffer_head *bh,
  71. handle_t *handle, int err)
  72. {
  73. char nbuf[16];
  74. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  75. BUG_ON(!ext4_handle_valid(handle));
  76. if (bh)
  77. BUFFER_TRACE(bh, "abort");
  78. if (!handle->h_err)
  79. handle->h_err = err;
  80. if (is_handle_aborted(handle))
  81. return;
  82. printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
  83. caller, line, errstr, err_fn);
  84. jbd2_journal_abort_handle(handle);
  85. }
  86. int __ext4_journal_get_write_access(const char *where, unsigned int line,
  87. handle_t *handle, struct buffer_head *bh)
  88. {
  89. int err = 0;
  90. if (ext4_handle_valid(handle)) {
  91. err = jbd2_journal_get_write_access(handle, bh);
  92. if (err)
  93. ext4_journal_abort_handle(where, line, __func__, bh,
  94. handle, err);
  95. }
  96. return err;
  97. }
  98. /*
  99. * The ext4 forget function must perform a revoke if we are freeing data
  100. * which has been journaled. Metadata (eg. indirect blocks) must be
  101. * revoked in all cases.
  102. *
  103. * "bh" may be NULL: a metadata block may have been freed from memory
  104. * but there may still be a record of it in the journal, and that record
  105. * still needs to be revoked.
  106. *
  107. * If the handle isn't valid we're not journaling, but we still need to
  108. * call into ext4_journal_revoke() to put the buffer head.
  109. */
  110. int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
  111. int is_metadata, struct inode *inode,
  112. struct buffer_head *bh, ext4_fsblk_t blocknr)
  113. {
  114. int err;
  115. might_sleep();
  116. trace_ext4_forget(inode, is_metadata, blocknr);
  117. BUFFER_TRACE(bh, "enter");
  118. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  119. "data mode %x\n",
  120. bh, is_metadata, inode->i_mode,
  121. test_opt(inode->i_sb, DATA_FLAGS));
  122. /* In the no journal case, we can just do a bforget and return */
  123. if (!ext4_handle_valid(handle)) {
  124. bforget(bh);
  125. return 0;
  126. }
  127. /* Never use the revoke function if we are doing full data
  128. * journaling: there is no need to, and a V1 superblock won't
  129. * support it. Otherwise, only skip the revoke on un-journaled
  130. * data blocks. */
  131. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  132. (!is_metadata && !ext4_should_journal_data(inode))) {
  133. if (bh) {
  134. BUFFER_TRACE(bh, "call jbd2_journal_forget");
  135. err = jbd2_journal_forget(handle, bh);
  136. if (err)
  137. ext4_journal_abort_handle(where, line, __func__,
  138. bh, handle, err);
  139. return err;
  140. }
  141. return 0;
  142. }
  143. /*
  144. * data!=journal && (is_metadata || should_journal_data(inode))
  145. */
  146. BUFFER_TRACE(bh, "call jbd2_journal_revoke");
  147. err = jbd2_journal_revoke(handle, blocknr, bh);
  148. if (err) {
  149. ext4_journal_abort_handle(where, line, __func__,
  150. bh, handle, err);
  151. __ext4_abort(inode->i_sb, where, line,
  152. "error %d when attempting revoke", err);
  153. }
  154. BUFFER_TRACE(bh, "exit");
  155. return err;
  156. }
  157. int __ext4_journal_get_create_access(const char *where, unsigned int line,
  158. handle_t *handle, struct buffer_head *bh)
  159. {
  160. int err = 0;
  161. if (ext4_handle_valid(handle)) {
  162. err = jbd2_journal_get_create_access(handle, bh);
  163. if (err)
  164. ext4_journal_abort_handle(where, line, __func__,
  165. bh, handle, err);
  166. }
  167. return err;
  168. }
  169. int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
  170. handle_t *handle, struct inode *inode,
  171. struct buffer_head *bh)
  172. {
  173. int err = 0;
  174. if (ext4_handle_valid(handle)) {
  175. err = jbd2_journal_dirty_metadata(handle, bh);
  176. if (err) {
  177. /* Errors can only happen if there is a bug */
  178. handle->h_err = err;
  179. __ext4_journal_stop(where, line, handle);
  180. }
  181. } else {
  182. if (inode)
  183. mark_buffer_dirty_inode(bh, inode);
  184. else
  185. mark_buffer_dirty(bh);
  186. if (inode && inode_needs_sync(inode)) {
  187. sync_dirty_buffer(bh);
  188. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  189. struct ext4_super_block *es;
  190. es = EXT4_SB(inode->i_sb)->s_es;
  191. es->s_last_error_block =
  192. cpu_to_le64(bh->b_blocknr);
  193. ext4_error_inode(inode, where, line,
  194. bh->b_blocknr,
  195. "IO error syncing itable block");
  196. err = -EIO;
  197. }
  198. }
  199. }
  200. return err;
  201. }
  202. int __ext4_handle_dirty_super(const char *where, unsigned int line,
  203. handle_t *handle, struct super_block *sb)
  204. {
  205. struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
  206. int err = 0;
  207. ext4_superblock_csum_set(sb);
  208. if (ext4_handle_valid(handle)) {
  209. err = jbd2_journal_dirty_metadata(handle, bh);
  210. if (err)
  211. ext4_journal_abort_handle(where, line, __func__,
  212. bh, handle, err);
  213. } else
  214. mark_buffer_dirty(bh);
  215. return err;
  216. }