ext4_jbd2.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Interface between ext4 and JBD
  4. */
  5. #include "ext4_jbd2.h"
  6. #include <trace/events/ext4.h>
  7. /* Just increment the non-pointer handle value */
  8. static handle_t *ext4_get_nojournal(void)
  9. {
  10. handle_t *handle = current->journal_info;
  11. unsigned long ref_cnt = (unsigned long)handle;
  12. BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
  13. ref_cnt++;
  14. handle = (handle_t *)ref_cnt;
  15. current->journal_info = handle;
  16. return handle;
  17. }
  18. /* Decrement the non-pointer handle value */
  19. static void ext4_put_nojournal(handle_t *handle)
  20. {
  21. unsigned long ref_cnt = (unsigned long)handle;
  22. BUG_ON(ref_cnt == 0);
  23. ref_cnt--;
  24. handle = (handle_t *)ref_cnt;
  25. current->journal_info = handle;
  26. }
  27. /*
  28. * Wrappers for jbd2_journal_start/end.
  29. */
  30. static int ext4_journal_check_start(struct super_block *sb)
  31. {
  32. journal_t *journal;
  33. might_sleep();
  34. if (unlikely(ext4_forced_shutdown(EXT4_SB(sb))))
  35. return -EIO;
  36. if (sb_rdonly(sb))
  37. return -EROFS;
  38. WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
  39. journal = EXT4_SB(sb)->s_journal;
  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 (journal && is_journal_aborted(journal)) {
  46. ext4_abort(sb, "Detected aborted journal");
  47. return -EROFS;
  48. }
  49. return 0;
  50. }
  51. handle_t *__ext4_journal_start_sb(struct super_block *sb, unsigned int line,
  52. int type, int blocks, int rsv_blocks)
  53. {
  54. journal_t *journal;
  55. int err;
  56. trace_ext4_journal_start(sb, blocks, rsv_blocks, _RET_IP_);
  57. err = ext4_journal_check_start(sb);
  58. if (err < 0)
  59. return ERR_PTR(err);
  60. journal = EXT4_SB(sb)->s_journal;
  61. if (!journal)
  62. return ext4_get_nojournal();
  63. return jbd2__journal_start(journal, blocks, rsv_blocks, GFP_NOFS,
  64. type, line);
  65. }
  66. int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
  67. {
  68. struct super_block *sb;
  69. int err;
  70. int rc;
  71. if (!ext4_handle_valid(handle)) {
  72. ext4_put_nojournal(handle);
  73. return 0;
  74. }
  75. err = handle->h_err;
  76. if (!handle->h_transaction) {
  77. rc = jbd2_journal_stop(handle);
  78. return err ? err : rc;
  79. }
  80. sb = handle->h_transaction->t_journal->j_private;
  81. rc = jbd2_journal_stop(handle);
  82. if (!err)
  83. err = rc;
  84. if (err)
  85. __ext4_std_error(sb, where, line, err);
  86. return err;
  87. }
  88. handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line,
  89. int type)
  90. {
  91. struct super_block *sb;
  92. int err;
  93. if (!ext4_handle_valid(handle))
  94. return ext4_get_nojournal();
  95. sb = handle->h_journal->j_private;
  96. trace_ext4_journal_start_reserved(sb, handle->h_buffer_credits,
  97. _RET_IP_);
  98. err = ext4_journal_check_start(sb);
  99. if (err < 0) {
  100. jbd2_journal_free_reserved(handle);
  101. return ERR_PTR(err);
  102. }
  103. err = jbd2_journal_start_reserved(handle, type, line);
  104. if (err < 0)
  105. return ERR_PTR(err);
  106. return handle;
  107. }
  108. static void ext4_journal_abort_handle(const char *caller, unsigned int line,
  109. const char *err_fn,
  110. struct buffer_head *bh,
  111. handle_t *handle, int err)
  112. {
  113. char nbuf[16];
  114. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  115. BUG_ON(!ext4_handle_valid(handle));
  116. if (bh)
  117. BUFFER_TRACE(bh, "abort");
  118. if (!handle->h_err)
  119. handle->h_err = err;
  120. if (is_handle_aborted(handle))
  121. return;
  122. printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
  123. caller, line, errstr, err_fn);
  124. jbd2_journal_abort_handle(handle);
  125. }
  126. int __ext4_journal_get_write_access(const char *where, unsigned int line,
  127. handle_t *handle, struct buffer_head *bh)
  128. {
  129. int err = 0;
  130. might_sleep();
  131. if (ext4_handle_valid(handle)) {
  132. err = jbd2_journal_get_write_access(handle, bh);
  133. if (err)
  134. ext4_journal_abort_handle(where, line, __func__, bh,
  135. handle, err);
  136. }
  137. return err;
  138. }
  139. /*
  140. * The ext4 forget function must perform a revoke if we are freeing data
  141. * which has been journaled. Metadata (eg. indirect blocks) must be
  142. * revoked in all cases.
  143. *
  144. * "bh" may be NULL: a metadata block may have been freed from memory
  145. * but there may still be a record of it in the journal, and that record
  146. * still needs to be revoked.
  147. *
  148. * If the handle isn't valid we're not journaling, but we still need to
  149. * call into ext4_journal_revoke() to put the buffer head.
  150. */
  151. int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
  152. int is_metadata, struct inode *inode,
  153. struct buffer_head *bh, ext4_fsblk_t blocknr)
  154. {
  155. int err;
  156. might_sleep();
  157. trace_ext4_forget(inode, is_metadata, blocknr);
  158. BUFFER_TRACE(bh, "enter");
  159. jbd_debug(4, "forgetting bh %p: is_metadata = %d, mode %o, "
  160. "data mode %x\n",
  161. bh, is_metadata, inode->i_mode,
  162. test_opt(inode->i_sb, DATA_FLAGS));
  163. /* In the no journal case, we can just do a bforget and return */
  164. if (!ext4_handle_valid(handle)) {
  165. bforget(bh);
  166. return 0;
  167. }
  168. /* Never use the revoke function if we are doing full data
  169. * journaling: there is no need to, and a V1 superblock won't
  170. * support it. Otherwise, only skip the revoke on un-journaled
  171. * data blocks. */
  172. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  173. (!is_metadata && !ext4_should_journal_data(inode))) {
  174. if (bh) {
  175. BUFFER_TRACE(bh, "call jbd2_journal_forget");
  176. err = jbd2_journal_forget(handle, bh);
  177. if (err)
  178. ext4_journal_abort_handle(where, line, __func__,
  179. bh, handle, err);
  180. return err;
  181. }
  182. return 0;
  183. }
  184. /*
  185. * data!=journal && (is_metadata || should_journal_data(inode))
  186. */
  187. BUFFER_TRACE(bh, "call jbd2_journal_revoke");
  188. err = jbd2_journal_revoke(handle, blocknr, bh);
  189. if (err) {
  190. ext4_journal_abort_handle(where, line, __func__,
  191. bh, handle, err);
  192. __ext4_abort(inode->i_sb, where, line,
  193. "error %d when attempting revoke", err);
  194. }
  195. BUFFER_TRACE(bh, "exit");
  196. return err;
  197. }
  198. int __ext4_journal_get_create_access(const char *where, unsigned int line,
  199. handle_t *handle, struct buffer_head *bh)
  200. {
  201. int err = 0;
  202. if (ext4_handle_valid(handle)) {
  203. err = jbd2_journal_get_create_access(handle, bh);
  204. if (err)
  205. ext4_journal_abort_handle(where, line, __func__,
  206. bh, handle, err);
  207. }
  208. return err;
  209. }
  210. int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
  211. handle_t *handle, struct inode *inode,
  212. struct buffer_head *bh)
  213. {
  214. int err = 0;
  215. might_sleep();
  216. set_buffer_meta(bh);
  217. set_buffer_prio(bh);
  218. if (ext4_handle_valid(handle)) {
  219. err = jbd2_journal_dirty_metadata(handle, bh);
  220. /* Errors can only happen due to aborted journal or a nasty bug */
  221. if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) {
  222. ext4_journal_abort_handle(where, line, __func__, bh,
  223. handle, err);
  224. if (inode == NULL) {
  225. pr_err("EXT4: jbd2_journal_dirty_metadata "
  226. "failed: handle type %u started at "
  227. "line %u, credits %u/%u, errcode %d",
  228. handle->h_type,
  229. handle->h_line_no,
  230. handle->h_requested_credits,
  231. handle->h_buffer_credits, err);
  232. return err;
  233. }
  234. ext4_error_inode(inode, where, line,
  235. bh->b_blocknr,
  236. "journal_dirty_metadata failed: "
  237. "handle type %u started at line %u, "
  238. "credits %u/%u, errcode %d",
  239. handle->h_type,
  240. handle->h_line_no,
  241. handle->h_requested_credits,
  242. handle->h_buffer_credits, err);
  243. }
  244. } else {
  245. if (inode)
  246. mark_buffer_dirty_inode(bh, inode);
  247. else
  248. mark_buffer_dirty(bh);
  249. if (inode && inode_needs_sync(inode)) {
  250. sync_dirty_buffer(bh);
  251. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  252. struct ext4_super_block *es;
  253. es = EXT4_SB(inode->i_sb)->s_es;
  254. es->s_last_error_block =
  255. cpu_to_le64(bh->b_blocknr);
  256. ext4_error_inode(inode, where, line,
  257. bh->b_blocknr,
  258. "IO error syncing itable block");
  259. err = -EIO;
  260. }
  261. }
  262. }
  263. return err;
  264. }
  265. int __ext4_handle_dirty_super(const char *where, unsigned int line,
  266. handle_t *handle, struct super_block *sb)
  267. {
  268. struct buffer_head *bh = EXT4_SB(sb)->s_sbh;
  269. int err = 0;
  270. ext4_superblock_csum_set(sb);
  271. if (ext4_handle_valid(handle)) {
  272. err = jbd2_journal_dirty_metadata(handle, bh);
  273. if (err)
  274. ext4_journal_abort_handle(where, line, __func__,
  275. bh, handle, err);
  276. } else
  277. mark_buffer_dirty(bh);
  278. return err;
  279. }