ext4_jbd2.c 6.2 KB

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