trans.c 4.2 KB

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