ext4_jbd2.c 8.2 KB

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