trans.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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 v.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 <asm/semaphore.h>
  16. #include "gfs2.h"
  17. #include "lm_interface.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_i(struct gfs2_sbd *sdp, unsigned int blocks,
  26. unsigned int revokes, char *file, unsigned int line)
  27. {
  28. struct gfs2_trans *tr;
  29. int error;
  30. if (gfs2_assert_warn(sdp, !current->journal_info) ||
  31. gfs2_assert_warn(sdp, blocks || revokes)) {
  32. fs_warn(sdp, "(%s, %u)\n", file, line);
  33. return -EINVAL;
  34. }
  35. tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
  36. if (!tr)
  37. return -ENOMEM;
  38. tr->tr_file = file;
  39. tr->tr_line = line;
  40. tr->tr_blocks = blocks;
  41. tr->tr_revokes = revokes;
  42. tr->tr_reserved = 1;
  43. if (blocks)
  44. tr->tr_reserved += 1 + blocks;
  45. if (revokes)
  46. tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
  47. sizeof(uint64_t));
  48. INIT_LIST_HEAD(&tr->tr_list_buf);
  49. gfs2_holder_init(sdp->sd_trans_gl, LM_ST_SHARED,
  50. GL_NEVER_RECURSE, &tr->tr_t_gh);
  51. error = gfs2_glock_nq(&tr->tr_t_gh);
  52. if (error)
  53. goto fail_holder_uninit;
  54. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
  55. tr->tr_t_gh.gh_flags |= GL_NOCACHE;
  56. error = -EROFS;
  57. goto fail_gunlock;
  58. }
  59. error = gfs2_log_reserve(sdp, tr->tr_reserved);
  60. if (error)
  61. goto fail_gunlock;
  62. current->journal_info = tr;
  63. return 0;
  64. fail_gunlock:
  65. gfs2_glock_dq(&tr->tr_t_gh);
  66. fail_holder_uninit:
  67. gfs2_holder_uninit(&tr->tr_t_gh);
  68. kfree(tr);
  69. return error;
  70. }
  71. void gfs2_trans_end(struct gfs2_sbd *sdp)
  72. {
  73. struct gfs2_trans *tr;
  74. tr = current->journal_info;
  75. current->journal_info = NULL;
  76. if (gfs2_assert_warn(sdp, tr))
  77. return;
  78. if (!tr->tr_touched) {
  79. gfs2_log_release(sdp, tr->tr_reserved);
  80. gfs2_glock_dq(&tr->tr_t_gh);
  81. gfs2_holder_uninit(&tr->tr_t_gh);
  82. kfree(tr);
  83. return;
  84. }
  85. if (gfs2_assert_withdraw(sdp, tr->tr_num_buf <= tr->tr_blocks))
  86. fs_err(sdp, "tr_num_buf = %u, tr_blocks = %u "
  87. "tr_file = %s, tr_line = %u\n",
  88. tr->tr_num_buf, tr->tr_blocks,
  89. tr->tr_file, tr->tr_line);
  90. if (gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
  91. fs_err(sdp, "tr_num_revoke = %u, tr_revokes = %u "
  92. "tr_file = %s, tr_line = %u\n",
  93. tr->tr_num_revoke, tr->tr_revokes,
  94. tr->tr_file, tr->tr_line);
  95. gfs2_log_commit(sdp, tr);
  96. gfs2_glock_dq(&tr->tr_t_gh);
  97. gfs2_holder_uninit(&tr->tr_t_gh);
  98. kfree(tr);
  99. if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
  100. gfs2_log_flush(sdp);
  101. }
  102. void gfs2_trans_add_gl(struct gfs2_glock *gl)
  103. {
  104. lops_add(gl->gl_sbd, &gl->gl_le);
  105. }
  106. /**
  107. * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
  108. * @gl: the glock the buffer belongs to
  109. * @bh: The buffer to add
  110. * @meta: True in the case of adding metadata
  111. *
  112. */
  113. void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
  114. {
  115. struct gfs2_sbd *sdp = gl->gl_sbd;
  116. struct gfs2_bufdata *bd;
  117. bd = bh->b_private;
  118. if (bd)
  119. gfs2_assert(sdp, bd->bd_gl == gl);
  120. else {
  121. gfs2_attach_bufdata(gl, bh, meta);
  122. bd = bh->b_private;
  123. }
  124. lops_add(sdp, &bd->bd_le);
  125. }
  126. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, uint64_t blkno)
  127. {
  128. struct gfs2_revoke *rv = kmalloc(sizeof(struct gfs2_revoke),
  129. GFP_NOFS | __GFP_NOFAIL);
  130. lops_init_le(&rv->rv_le, &gfs2_revoke_lops);
  131. rv->rv_blkno = blkno;
  132. lops_add(sdp, &rv->rv_le);
  133. }
  134. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, uint64_t blkno)
  135. {
  136. struct gfs2_revoke *rv;
  137. int found = 0;
  138. gfs2_log_lock(sdp);
  139. list_for_each_entry(rv, &sdp->sd_log_le_revoke, rv_le.le_list) {
  140. if (rv->rv_blkno == blkno) {
  141. list_del(&rv->rv_le.le_list);
  142. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  143. sdp->sd_log_num_revoke--;
  144. found = 1;
  145. break;
  146. }
  147. }
  148. gfs2_log_unlock(sdp);
  149. if (found) {
  150. struct gfs2_trans *tr = current->journal_info;
  151. kfree(rv);
  152. tr->tr_num_revoke_rm++;
  153. }
  154. }
  155. void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd)
  156. {
  157. lops_add(rgd->rd_sbd, &rgd->rd_le);
  158. }