ext4_jbd2.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. static void ext4_journal_abort_handle(const char *caller, unsigned int line,
  102. const char *err_fn,
  103. struct buffer_head *bh,
  104. handle_t *handle, int err)
  105. {
  106. char nbuf[16];
  107. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  108. BUG_ON(!ext4_handle_valid(handle));
  109. if (bh)
  110. BUFFER_TRACE(bh, "abort");
  111. if (!handle->h_err)
  112. handle->h_err = err;
  113. if (is_handle_aborted(handle))
  114. return;
  115. printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
  116. caller, line, errstr, err_fn);
  117. jbd2_journal_abort_handle(handle);
  118. }
  119. int __ext4_journal_get_write_access(const char *where, unsigned int line,
  120. handle_t *handle, struct buffer_head *bh)
  121. {
  122. int err = 0;
  123. might_sleep();
  124. if (ext4_handle_valid(handle)) {
  125. err = jbd2_journal_get_write_access(handle, bh);
  126. if (err)
  127. ext4_journal_abort_handle(where, line, __func__, bh,
  128. handle, err);
  129. }
  130. return err;
  131. }
  132. /*
  133. * The ext4 forget function must perform a revoke if we are freeing data
  134. * which has been journaled. Metadata (eg. indirect blocks) must be
  135. * revoked in all cases.
  136. *
  137. * "bh" may be NULL: a metadata block may have been freed from memory
  138. * but there may still be a record of it in the journal, and that record
  139. * still needs to be revoked.
  140. *
  141. * If the handle isn't valid we're not journaling, but we still need to
  142. * call into ext4_journal_revoke() to put the buffer head.
  143. */
  144. int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
  145. int is_metadata, struct inode *inode,
  146. struct buffer_head *bh, ext4_fsblk_t blocknr)
  147. {
  148. int err;
  149. might_sleep();
  150. trace_ext4_forget(inode, is_metadata, blocknr);
  151. BUFFER_TRACE(bh, "enter");
  152. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  153. "data mode %x\n",
  154. bh, is_metadata, inode->i_mode,
  155. test_opt(inode->i_sb, DATA_FLAGS));
  156. /* In the no journal case, we can just do a bforget and return */
  157. if (!ext4_handle_valid(handle)) {
  158. bforget(bh);
  159. return 0;
  160. }
  161. /* Never use the revoke function if we are doing full data
  162. * journaling: there is no need to, and a V1 superblock won't
  163. * support it. Otherwise, only skip the revoke on un-journaled
  164. * data blocks. */
  165. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  166. (!is_metadata && !ext4_should_journal_data(inode))) {
  167. if (bh) {
  168. BUFFER_TRACE(bh, "call jbd2_journal_forget");
  169. err = jbd2_journal_forget(handle, bh);
  170. if (err)
  171. ext4_journal_abort_handle(where, line, __func__,
  172. bh, handle, err);
  173. return err;
  174. }
  175. return 0;
  176. }
  177. /*
  178. * data!=journal && (is_metadata || should_journal_data(inode))
  179. */
  180. BUFFER_TRACE(bh, "call jbd2_journal_revoke");
  181. err = jbd2_journal_revoke(handle, blocknr, bh);
  182. if (err) {
  183. ext4_journal_abort_handle(where, line, __func__,
  184. bh, handle, err);
  185. __ext4_abort(inode->i_sb, where, line,
  186. "error %d when attempting revoke", err);
  187. }
  188. BUFFER_TRACE(bh, "exit");
  189. return err;
  190. }
  191. int __ext4_journal_get_create_access(const char *where, unsigned int line,
  192. handle_t *handle, struct buffer_head *bh)
  193. {
  194. int err = 0;
  195. if (ext4_handle_valid(handle)) {
  196. err = jbd2_journal_get_create_access(handle, bh);
  197. if (err)
  198. ext4_journal_abort_handle(where, line, __func__,
  199. bh, handle, err);
  200. }
  201. return err;
  202. }
  203. int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
  204. handle_t *handle, struct inode *inode,
  205. struct buffer_head *bh)
  206. {
  207. int err = 0;
  208. might_sleep();
  209. set_buffer_meta(bh);
  210. set_buffer_prio(bh);
  211. if (ext4_handle_valid(handle)) {
  212. err = jbd2_journal_dirty_metadata(handle, bh);
  213. /* Errors can only happen if there is a bug */
  214. if (WARN_ON_ONCE(err)) {
  215. ext4_journal_abort_handle(where, line, __func__, bh,
  216. handle, err);
  217. if (inode == NULL) {
  218. pr_err("EXT4: jbd2_journal_dirty_metadata "
  219. "failed: handle type %u started at "
  220. "line %u, credits %u/%u, errcode %d",
  221. handle->h_type,
  222. handle->h_line_no,
  223. handle->h_requested_credits,
  224. handle->h_buffer_credits, err);
  225. return err;
  226. }
  227. ext4_error_inode(inode, where, line,
  228. bh->b_blocknr,
  229. "journal_dirty_metadata failed: "
  230. "handle type %u started at line %u, "
  231. "credits %u/%u, errcode %d",
  232. handle->h_type,
  233. handle->h_line_no,
  234. handle->h_requested_credits,
  235. handle->h_buffer_credits, err);
  236. }
  237. } else {
  238. if (inode)
  239. mark_buffer_dirty_inode(bh, inode);
  240. else
  241. mark_buffer_dirty(bh);
  242. if (inode && inode_needs_sync(inode)) {
  243. sync_dirty_buffer(bh);
  244. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  245. struct ext4_super_block *es;
  246. es = EXT4_SB(inode->i_sb)->s_es;
  247. es->s_last_error_block =
  248. cpu_to_le64(bh->b_blocknr);
  249. ext4_error_inode(inode, where, line,
  250. bh->b_blocknr,
  251. "IO error syncing itable block");
  252. err = -EIO;
  253. }
  254. }
  255. }
  256. return err;
  257. }
  258. int __ext4_handle_dirty_super(const char *where, unsigned int line,
  259. handle_t *handle, struct super_block *sb)
  260. {
  261. struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
  262. int err = 0;
  263. ext4_superblock_csum_set(sb);
  264. if (ext4_handle_valid(handle)) {
  265. err = jbd2_journal_dirty_metadata(handle, bh);
  266. if (err)
  267. ext4_journal_abort_handle(where, line, __func__,
  268. bh, handle, err);
  269. } else
  270. mark_buffer_dirty(bh);
  271. return err;
  272. }