trans.c 4.2 KB

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