ext4_jbd2.c 7.1 KB

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