ext4_jbd2.c 8.2 KB

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