ext4_jbd2.c 7.9 KB

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