trans.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/kallsyms.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include "gfs2.h"
  17. #include "incore.h"
  18. #include "glock.h"
  19. #include "log.h"
  20. #include "lops.h"
  21. #include "meta_io.h"
  22. #include "trans.h"
  23. #include "util.h"
  24. int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
  25. unsigned int revokes)
  26. {
  27. struct gfs2_trans *tr;
  28. int error;
  29. BUG_ON(current->journal_info);
  30. BUG_ON(blocks == 0 && revokes == 0);
  31. tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
  32. if (!tr)
  33. return -ENOMEM;
  34. tr->tr_ip = (unsigned long)__builtin_return_address(0);
  35. tr->tr_blocks = blocks;
  36. tr->tr_revokes = revokes;
  37. tr->tr_reserved = 1;
  38. if (blocks)
  39. tr->tr_reserved += 6 + blocks;
  40. if (revokes)
  41. tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
  42. sizeof(u64));
  43. INIT_LIST_HEAD(&tr->tr_list_buf);
  44. gfs2_holder_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &tr->tr_t_gh);
  45. error = gfs2_glock_nq(&tr->tr_t_gh);
  46. if (error)
  47. goto fail_holder_uninit;
  48. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  49. tr->tr_t_gh.gh_flags |= GL_NOCACHE;
  50. error = -EROFS;
  51. goto fail_gunlock;
  52. }
  53. error = gfs2_log_reserve(sdp, tr->tr_reserved);
  54. if (error)
  55. goto fail_gunlock;
  56. current->journal_info = tr;
  57. return 0;
  58. fail_gunlock:
  59. gfs2_glock_dq(&tr->tr_t_gh);
  60. fail_holder_uninit:
  61. gfs2_holder_uninit(&tr->tr_t_gh);
  62. kfree(tr);
  63. return error;
  64. }
  65. void gfs2_trans_end(struct gfs2_sbd *sdp)
  66. {
  67. struct gfs2_trans *tr = current->journal_info;
  68. BUG_ON(!tr);
  69. current->journal_info = NULL;
  70. if (!tr->tr_touched) {
  71. gfs2_log_release(sdp, tr->tr_reserved);
  72. gfs2_glock_dq(&tr->tr_t_gh);
  73. gfs2_holder_uninit(&tr->tr_t_gh);
  74. kfree(tr);
  75. return;
  76. }
  77. if (gfs2_assert_withdraw(sdp, tr->tr_num_buf <= tr->tr_blocks)) {
  78. fs_err(sdp, "tr_num_buf = %u, tr_blocks = %u ",
  79. tr->tr_num_buf, tr->tr_blocks);
  80. print_symbol(KERN_WARNING "GFS2: Transaction created at: %s\n", tr->tr_ip);
  81. }
  82. if (gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes)) {
  83. fs_err(sdp, "tr_num_revoke = %u, tr_revokes = %u ",
  84. tr->tr_num_revoke, tr->tr_revokes);
  85. print_symbol(KERN_WARNING "GFS2: Transaction created at: %s\n", tr->tr_ip);
  86. }
  87. gfs2_log_commit(sdp, tr);
  88. gfs2_glock_dq(&tr->tr_t_gh);
  89. gfs2_holder_uninit(&tr->tr_t_gh);
  90. kfree(tr);
  91. if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
  92. gfs2_log_flush(sdp, NULL);
  93. }
  94. /**
  95. * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
  96. * @gl: the glock the buffer belongs to
  97. * @bh: The buffer to add
  98. * @meta: True in the case of adding metadata
  99. *
  100. */
  101. void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
  102. {
  103. struct gfs2_sbd *sdp = gl->gl_sbd;
  104. struct gfs2_bufdata *bd;
  105. bd = bh->b_private;
  106. if (bd)
  107. gfs2_assert(sdp, bd->bd_gl == gl);
  108. else {
  109. gfs2_attach_bufdata(gl, bh, meta);
  110. bd = bh->b_private;
  111. }
  112. lops_add(sdp, &bd->bd_le);
  113. }
  114. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  115. {
  116. BUG_ON(!list_empty(&bd->bd_le.le_list));
  117. BUG_ON(!list_empty(&bd->bd_ail_st_list));
  118. BUG_ON(!list_empty(&bd->bd_ail_gl_list));
  119. lops_init_le(&bd->bd_le, &gfs2_revoke_lops);
  120. lops_add(sdp, &bd->bd_le);
  121. }
  122. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  123. {
  124. struct gfs2_bufdata *bd, *tmp;
  125. struct gfs2_trans *tr = current->journal_info;
  126. unsigned int n = len;
  127. gfs2_log_lock(sdp);
  128. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_le.le_list) {
  129. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  130. list_del_init(&bd->bd_le.le_list);
  131. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  132. sdp->sd_log_num_revoke--;
  133. kmem_cache_free(gfs2_bufdata_cachep, bd);
  134. tr->tr_num_revoke_rm++;
  135. if (--n == 0)
  136. break;
  137. }
  138. }
  139. gfs2_log_unlock(sdp);
  140. }
  141. void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd)
  142. {
  143. lops_add(rgd->rd_sbd, &rgd->rd_le);
  144. }