ext4_jbd2.c 6.3 KB

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