ext4_jbd2.c 11 KB

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