ext4_jbd2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. int ext4_inode_journal_mode(struct inode *inode)
  8. {
  9. if (EXT4_JOURNAL(inode) == NULL)
  10. return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */
  11. /* We do not support data journalling with delayed allocation */
  12. if (!S_ISREG(inode->i_mode) ||
  13. ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE) ||
  14. test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  15. (ext4_test_inode_flag(inode, EXT4_INODE_JOURNAL_DATA) &&
  16. !test_opt(inode->i_sb, DELALLOC))) {
  17. /* We do not support data journalling for encrypted data */
  18. if (S_ISREG(inode->i_mode) && IS_ENCRYPTED(inode))
  19. return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */
  20. return EXT4_INODE_JOURNAL_DATA_MODE; /* journal data */
  21. }
  22. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_ORDERED_DATA)
  23. return EXT4_INODE_ORDERED_DATA_MODE; /* ordered */
  24. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_WRITEBACK_DATA)
  25. return EXT4_INODE_WRITEBACK_DATA_MODE; /* writeback */
  26. BUG();
  27. }
  28. /* Just increment the non-pointer handle value */
  29. static handle_t *ext4_get_nojournal(void)
  30. {
  31. handle_t *handle = current->journal_info;
  32. unsigned long ref_cnt = (unsigned long)handle;
  33. BUG_ON(ref_cnt >= EXT4_NOJOURNAL_MAX_REF_COUNT);
  34. ref_cnt++;
  35. handle = (handle_t *)ref_cnt;
  36. current->journal_info = handle;
  37. return handle;
  38. }
  39. /* Decrement the non-pointer handle value */
  40. static void ext4_put_nojournal(handle_t *handle)
  41. {
  42. unsigned long ref_cnt = (unsigned long)handle;
  43. BUG_ON(ref_cnt == 0);
  44. ref_cnt--;
  45. handle = (handle_t *)ref_cnt;
  46. current->journal_info = handle;
  47. }
  48. /*
  49. * Wrappers for jbd2_journal_start/end.
  50. */
  51. static int ext4_journal_check_start(struct super_block *sb)
  52. {
  53. journal_t *journal;
  54. might_sleep();
  55. if (unlikely(ext4_forced_shutdown(sb)))
  56. return -EIO;
  57. if (WARN_ON_ONCE(sb_rdonly(sb)))
  58. return -EROFS;
  59. WARN_ON(sb->s_writers.frozen == SB_FREEZE_COMPLETE);
  60. journal = EXT4_SB(sb)->s_journal;
  61. /*
  62. * Special case here: if the journal has aborted behind our
  63. * backs (eg. EIO in the commit thread), then we still need to
  64. * take the FS itself readonly cleanly.
  65. */
  66. if (journal && is_journal_aborted(journal)) {
  67. ext4_abort(sb, -journal->j_errno, "Detected aborted journal");
  68. return -EROFS;
  69. }
  70. return 0;
  71. }
  72. handle_t *__ext4_journal_start_sb(struct inode *inode,
  73. struct super_block *sb, unsigned int line,
  74. int type, int blocks, int rsv_blocks,
  75. int revoke_creds)
  76. {
  77. journal_t *journal;
  78. int err;
  79. if (inode)
  80. trace_ext4_journal_start_inode(inode, blocks, rsv_blocks,
  81. revoke_creds, type,
  82. _RET_IP_);
  83. else
  84. trace_ext4_journal_start_sb(sb, blocks, rsv_blocks,
  85. revoke_creds, type,
  86. _RET_IP_);
  87. err = ext4_journal_check_start(sb);
  88. if (err < 0)
  89. return ERR_PTR(err);
  90. journal = EXT4_SB(sb)->s_journal;
  91. if (!journal || (EXT4_SB(sb)->s_mount_state & EXT4_FC_REPLAY))
  92. return ext4_get_nojournal();
  93. return jbd2__journal_start(journal, blocks, rsv_blocks, revoke_creds,
  94. GFP_NOFS, type, line);
  95. }
  96. int __ext4_journal_stop(const char *where, unsigned int line, handle_t *handle)
  97. {
  98. struct super_block *sb;
  99. int err;
  100. int rc;
  101. if (!ext4_handle_valid(handle)) {
  102. ext4_put_nojournal(handle);
  103. return 0;
  104. }
  105. err = handle->h_err;
  106. if (!handle->h_transaction) {
  107. rc = jbd2_journal_stop(handle);
  108. return err ? err : rc;
  109. }
  110. sb = handle->h_transaction->t_journal->j_private;
  111. rc = jbd2_journal_stop(handle);
  112. if (!err)
  113. err = rc;
  114. if (err)
  115. __ext4_std_error(sb, where, line, err);
  116. return err;
  117. }
  118. handle_t *__ext4_journal_start_reserved(handle_t *handle, unsigned int line,
  119. int type)
  120. {
  121. struct super_block *sb;
  122. int err;
  123. if (!ext4_handle_valid(handle))
  124. return ext4_get_nojournal();
  125. sb = handle->h_journal->j_private;
  126. trace_ext4_journal_start_reserved(sb,
  127. jbd2_handle_buffer_credits(handle), _RET_IP_);
  128. err = ext4_journal_check_start(sb);
  129. if (err < 0) {
  130. jbd2_journal_free_reserved(handle);
  131. return ERR_PTR(err);
  132. }
  133. err = jbd2_journal_start_reserved(handle, type, line);
  134. if (err < 0)
  135. return ERR_PTR(err);
  136. return handle;
  137. }
  138. int __ext4_journal_ensure_credits(handle_t *handle, int check_cred,
  139. int extend_cred, int revoke_cred)
  140. {
  141. if (!ext4_handle_valid(handle))
  142. return 0;
  143. if (is_handle_aborted(handle))
  144. return -EROFS;
  145. if (jbd2_handle_buffer_credits(handle) >= check_cred &&
  146. handle->h_revoke_credits >= revoke_cred)
  147. return 0;
  148. extend_cred = max(0, extend_cred - jbd2_handle_buffer_credits(handle));
  149. revoke_cred = max(0, revoke_cred - handle->h_revoke_credits);
  150. return ext4_journal_extend(handle, extend_cred, revoke_cred);
  151. }
  152. static void ext4_journal_abort_handle(const char *caller, unsigned int line,
  153. const char *err_fn,
  154. struct buffer_head *bh,
  155. handle_t *handle, int err)
  156. {
  157. char nbuf[16];
  158. const char *errstr = ext4_decode_error(NULL, err, nbuf);
  159. BUG_ON(!ext4_handle_valid(handle));
  160. if (bh)
  161. BUFFER_TRACE(bh, "abort");
  162. if (!handle->h_err)
  163. handle->h_err = err;
  164. if (is_handle_aborted(handle))
  165. return;
  166. printk(KERN_ERR "EXT4-fs: %s:%d: aborting transaction: %s in %s\n",
  167. caller, line, errstr, err_fn);
  168. jbd2_journal_abort_handle(handle);
  169. }
  170. static void ext4_check_bdev_write_error(struct super_block *sb)
  171. {
  172. struct address_space *mapping = sb->s_bdev->bd_mapping;
  173. struct ext4_sb_info *sbi = EXT4_SB(sb);
  174. int err;
  175. /*
  176. * If the block device has write error flag, it may have failed to
  177. * async write out metadata buffers in the background. In this case,
  178. * we could read old data from disk and write it out again, which
  179. * may lead to on-disk filesystem inconsistency.
  180. */
  181. if (errseq_check(&mapping->wb_err, READ_ONCE(sbi->s_bdev_wb_err))) {
  182. spin_lock(&sbi->s_bdev_wb_lock);
  183. err = errseq_check_and_advance(&mapping->wb_err, &sbi->s_bdev_wb_err);
  184. spin_unlock(&sbi->s_bdev_wb_lock);
  185. if (err)
  186. ext4_error_err(sb, -err,
  187. "Error while async write back metadata");
  188. }
  189. }
  190. int __ext4_journal_get_write_access(const char *where, unsigned int line,
  191. handle_t *handle, struct super_block *sb,
  192. struct buffer_head *bh,
  193. enum ext4_journal_trigger_type trigger_type)
  194. {
  195. int err;
  196. might_sleep();
  197. if (ext4_handle_valid(handle)) {
  198. err = jbd2_journal_get_write_access(handle, bh);
  199. if (err) {
  200. ext4_journal_abort_handle(where, line, __func__, bh,
  201. handle, err);
  202. return err;
  203. }
  204. } else
  205. ext4_check_bdev_write_error(sb);
  206. if (trigger_type == EXT4_JTR_NONE || !ext4_has_metadata_csum(sb))
  207. return 0;
  208. BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT);
  209. jbd2_journal_set_triggers(bh,
  210. &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers);
  211. return 0;
  212. }
  213. /*
  214. * The ext4 forget function must perform a revoke if we are freeing data
  215. * which has been journaled. Metadata (eg. indirect blocks) must be
  216. * revoked in all cases.
  217. *
  218. * "bh" may be NULL: a metadata block may have been freed from memory
  219. * but there may still be a record of it in the journal, and that record
  220. * still needs to be revoked.
  221. */
  222. int __ext4_forget(const char *where, unsigned int line, handle_t *handle,
  223. int is_metadata, struct inode *inode,
  224. struct buffer_head *bh, ext4_fsblk_t blocknr)
  225. {
  226. int err;
  227. might_sleep();
  228. trace_ext4_forget(inode, is_metadata, blocknr);
  229. BUFFER_TRACE(bh, "enter");
  230. ext4_debug("forgetting bh %p: is_metadata=%d, mode %o, data mode %x\n",
  231. bh, is_metadata, inode->i_mode,
  232. test_opt(inode->i_sb, DATA_FLAGS));
  233. /*
  234. * In the no journal case, we should wait for the ongoing buffer
  235. * to complete and do a forget.
  236. */
  237. if (!ext4_handle_valid(handle)) {
  238. if (bh) {
  239. clear_buffer_dirty(bh);
  240. wait_on_buffer(bh);
  241. __bforget(bh);
  242. }
  243. return 0;
  244. }
  245. /* Never use the revoke function if we are doing full data
  246. * journaling: there is no need to, and a V1 superblock won't
  247. * support it. Otherwise, only skip the revoke on un-journaled
  248. * data blocks. */
  249. if (test_opt(inode->i_sb, DATA_FLAGS) == EXT4_MOUNT_JOURNAL_DATA ||
  250. (!is_metadata && !ext4_should_journal_data(inode))) {
  251. if (bh) {
  252. BUFFER_TRACE(bh, "call jbd2_journal_forget");
  253. err = jbd2_journal_forget(handle, bh);
  254. if (err)
  255. ext4_journal_abort_handle(where, line, __func__,
  256. bh, handle, err);
  257. return err;
  258. }
  259. return 0;
  260. }
  261. /*
  262. * data!=journal && (is_metadata || should_journal_data(inode))
  263. */
  264. BUFFER_TRACE(bh, "call jbd2_journal_revoke");
  265. err = jbd2_journal_revoke(handle, blocknr, bh);
  266. if (err) {
  267. ext4_journal_abort_handle(where, line, __func__,
  268. bh, handle, err);
  269. __ext4_error(inode->i_sb, where, line, true, -err, 0,
  270. "error %d when attempting revoke", err);
  271. }
  272. BUFFER_TRACE(bh, "exit");
  273. return err;
  274. }
  275. int __ext4_journal_get_create_access(const char *where, unsigned int line,
  276. handle_t *handle, struct super_block *sb,
  277. struct buffer_head *bh,
  278. enum ext4_journal_trigger_type trigger_type)
  279. {
  280. int err;
  281. if (!ext4_handle_valid(handle))
  282. return 0;
  283. err = jbd2_journal_get_create_access(handle, bh);
  284. if (err) {
  285. ext4_journal_abort_handle(where, line, __func__, bh, handle,
  286. err);
  287. return err;
  288. }
  289. if (trigger_type == EXT4_JTR_NONE || !ext4_has_metadata_csum(sb))
  290. return 0;
  291. BUG_ON(trigger_type >= EXT4_JOURNAL_TRIGGER_COUNT);
  292. jbd2_journal_set_triggers(bh,
  293. &EXT4_SB(sb)->s_journal_triggers[trigger_type].tr_triggers);
  294. return 0;
  295. }
  296. int __ext4_handle_dirty_metadata(const char *where, unsigned int line,
  297. handle_t *handle, struct inode *inode,
  298. struct buffer_head *bh)
  299. {
  300. int err = 0;
  301. might_sleep();
  302. set_buffer_meta(bh);
  303. set_buffer_prio(bh);
  304. set_buffer_uptodate(bh);
  305. if (ext4_handle_valid(handle)) {
  306. err = jbd2_journal_dirty_metadata(handle, bh);
  307. /* Errors can only happen due to aborted journal or a nasty bug */
  308. if (!is_handle_aborted(handle) && WARN_ON_ONCE(err)) {
  309. ext4_journal_abort_handle(where, line, __func__, bh,
  310. handle, err);
  311. if (inode == NULL) {
  312. pr_err("EXT4: jbd2_journal_dirty_metadata "
  313. "failed: handle type %u started at "
  314. "line %u, credits %u/%u, errcode %d",
  315. handle->h_type,
  316. handle->h_line_no,
  317. handle->h_requested_credits,
  318. jbd2_handle_buffer_credits(handle), err);
  319. return err;
  320. }
  321. ext4_error_inode(inode, where, line,
  322. bh->b_blocknr,
  323. "journal_dirty_metadata failed: "
  324. "handle type %u started at line %u, "
  325. "credits %u/%u, errcode %d",
  326. handle->h_type,
  327. handle->h_line_no,
  328. handle->h_requested_credits,
  329. jbd2_handle_buffer_credits(handle),
  330. err);
  331. }
  332. } else {
  333. if (inode)
  334. mark_buffer_dirty_inode(bh, inode);
  335. else
  336. mark_buffer_dirty(bh);
  337. if (inode && inode_needs_sync(inode)) {
  338. sync_dirty_buffer(bh);
  339. if (buffer_req(bh) && !buffer_uptodate(bh)) {
  340. ext4_error_inode_err(inode, where, line,
  341. bh->b_blocknr, EIO,
  342. "IO error syncing itable block");
  343. err = -EIO;
  344. }
  345. }
  346. }
  347. return err;
  348. }