trans.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #include "trace_gfs2.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. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  33. return -EROFS;
  34. tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
  35. if (!tr)
  36. return -ENOMEM;
  37. tr->tr_ip = (unsigned long)__builtin_return_address(0);
  38. tr->tr_blocks = blocks;
  39. tr->tr_revokes = revokes;
  40. tr->tr_reserved = 1;
  41. if (blocks)
  42. tr->tr_reserved += 6 + blocks;
  43. if (revokes)
  44. tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
  45. sizeof(u64));
  46. sb_start_intwrite(sdp->sd_vfs);
  47. gfs2_holder_init(sdp->sd_trans_gl, LM_ST_SHARED, 0, &tr->tr_t_gh);
  48. error = gfs2_glock_nq(&tr->tr_t_gh);
  49. if (error)
  50. goto fail_holder_uninit;
  51. error = gfs2_log_reserve(sdp, tr->tr_reserved);
  52. if (error)
  53. goto fail_gunlock;
  54. current->journal_info = tr;
  55. return 0;
  56. fail_gunlock:
  57. gfs2_glock_dq(&tr->tr_t_gh);
  58. fail_holder_uninit:
  59. sb_end_intwrite(sdp->sd_vfs);
  60. gfs2_holder_uninit(&tr->tr_t_gh);
  61. kfree(tr);
  62. return error;
  63. }
  64. /**
  65. * gfs2_log_release - Release a given number of log blocks
  66. * @sdp: The GFS2 superblock
  67. * @blks: The number of blocks
  68. *
  69. */
  70. static void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
  71. {
  72. atomic_add(blks, &sdp->sd_log_blks_free);
  73. trace_gfs2_log_blocks(sdp, blks);
  74. gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
  75. sdp->sd_jdesc->jd_blocks);
  76. up_read(&sdp->sd_log_flush_lock);
  77. }
  78. static void gfs2_print_trans(const struct gfs2_trans *tr)
  79. {
  80. print_symbol(KERN_WARNING "GFS2: Transaction created at: %s\n", tr->tr_ip);
  81. printk(KERN_WARNING "GFS2: blocks=%u revokes=%u reserved=%u touched=%d\n",
  82. tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, tr->tr_touched);
  83. printk(KERN_WARNING "GFS2: Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
  84. tr->tr_num_buf_new, tr->tr_num_buf_rm,
  85. tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
  86. tr->tr_num_revoke, tr->tr_num_revoke_rm);
  87. }
  88. void gfs2_trans_end(struct gfs2_sbd *sdp)
  89. {
  90. struct gfs2_trans *tr = current->journal_info;
  91. s64 nbuf;
  92. BUG_ON(!tr);
  93. current->journal_info = NULL;
  94. if (!tr->tr_touched) {
  95. gfs2_log_release(sdp, tr->tr_reserved);
  96. if (tr->tr_t_gh.gh_gl) {
  97. gfs2_glock_dq(&tr->tr_t_gh);
  98. gfs2_holder_uninit(&tr->tr_t_gh);
  99. kfree(tr);
  100. }
  101. sb_end_intwrite(sdp->sd_vfs);
  102. return;
  103. }
  104. nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
  105. nbuf -= tr->tr_num_buf_rm;
  106. nbuf -= tr->tr_num_databuf_rm;
  107. if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
  108. (tr->tr_num_revoke <= tr->tr_revokes)))
  109. gfs2_print_trans(tr);
  110. gfs2_log_commit(sdp, tr);
  111. if (tr->tr_t_gh.gh_gl) {
  112. gfs2_glock_dq(&tr->tr_t_gh);
  113. gfs2_holder_uninit(&tr->tr_t_gh);
  114. kfree(tr);
  115. }
  116. if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
  117. gfs2_log_flush(sdp, NULL);
  118. sb_end_intwrite(sdp->sd_vfs);
  119. }
  120. /**
  121. * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
  122. * @gl: the glock the buffer belongs to
  123. * @bh: The buffer to add
  124. * @meta: True in the case of adding metadata
  125. *
  126. */
  127. void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
  128. {
  129. struct gfs2_sbd *sdp = gl->gl_sbd;
  130. struct gfs2_bufdata *bd;
  131. bd = bh->b_private;
  132. if (bd)
  133. gfs2_assert(sdp, bd->bd_gl == gl);
  134. else {
  135. gfs2_attach_bufdata(gl, bh, meta);
  136. bd = bh->b_private;
  137. }
  138. lops_add(sdp, bd);
  139. }
  140. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  141. {
  142. BUG_ON(!list_empty(&bd->bd_list));
  143. BUG_ON(!list_empty(&bd->bd_ail_st_list));
  144. BUG_ON(!list_empty(&bd->bd_ail_gl_list));
  145. lops_init_le(bd, &gfs2_revoke_lops);
  146. lops_add(sdp, bd);
  147. }
  148. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  149. {
  150. struct gfs2_bufdata *bd, *tmp;
  151. struct gfs2_trans *tr = current->journal_info;
  152. unsigned int n = len;
  153. gfs2_log_lock(sdp);
  154. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_list) {
  155. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  156. list_del_init(&bd->bd_list);
  157. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  158. sdp->sd_log_num_revoke--;
  159. kmem_cache_free(gfs2_bufdata_cachep, bd);
  160. tr->tr_num_revoke_rm++;
  161. if (--n == 0)
  162. break;
  163. }
  164. }
  165. gfs2_log_unlock(sdp);
  166. }