trans.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/sched.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/completion.h>
  14. #include <linux/buffer_head.h>
  15. #include <linux/kallsyms.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "glock.h"
  20. #include "inode.h"
  21. #include "log.h"
  22. #include "lops.h"
  23. #include "meta_io.h"
  24. #include "trans.h"
  25. #include "util.h"
  26. #include "trace_gfs2.h"
  27. int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
  28. unsigned int revokes)
  29. {
  30. struct gfs2_trans *tr;
  31. int error;
  32. BUG_ON(current->journal_info);
  33. BUG_ON(blocks == 0 && revokes == 0);
  34. if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
  35. return -EROFS;
  36. tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
  37. if (!tr)
  38. return -ENOMEM;
  39. tr->tr_ip = _RET_IP_;
  40. tr->tr_blocks = blocks;
  41. tr->tr_revokes = revokes;
  42. tr->tr_reserved = 1;
  43. set_bit(TR_ALLOCED, &tr->tr_flags);
  44. if (blocks)
  45. tr->tr_reserved += 6 + blocks;
  46. if (revokes)
  47. tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
  48. sizeof(u64));
  49. INIT_LIST_HEAD(&tr->tr_databuf);
  50. INIT_LIST_HEAD(&tr->tr_buf);
  51. INIT_LIST_HEAD(&tr->tr_ail1_list);
  52. INIT_LIST_HEAD(&tr->tr_ail2_list);
  53. sb_start_intwrite(sdp->sd_vfs);
  54. error = gfs2_log_reserve(sdp, tr->tr_reserved);
  55. if (error)
  56. goto fail;
  57. current->journal_info = tr;
  58. return 0;
  59. fail:
  60. sb_end_intwrite(sdp->sd_vfs);
  61. kfree(tr);
  62. return error;
  63. }
  64. static void gfs2_print_trans(const struct gfs2_trans *tr)
  65. {
  66. pr_warn("Transaction created at: %pSR\n", (void *)tr->tr_ip);
  67. pr_warn("blocks=%u revokes=%u reserved=%u touched=%u\n",
  68. tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
  69. test_bit(TR_TOUCHED, &tr->tr_flags));
  70. pr_warn("Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
  71. tr->tr_num_buf_new, tr->tr_num_buf_rm,
  72. tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
  73. tr->tr_num_revoke, tr->tr_num_revoke_rm);
  74. }
  75. void gfs2_trans_end(struct gfs2_sbd *sdp)
  76. {
  77. struct gfs2_trans *tr = current->journal_info;
  78. s64 nbuf;
  79. int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
  80. current->journal_info = NULL;
  81. if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
  82. gfs2_log_release(sdp, tr->tr_reserved);
  83. if (alloced) {
  84. kfree(tr);
  85. sb_end_intwrite(sdp->sd_vfs);
  86. }
  87. return;
  88. }
  89. nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
  90. nbuf -= tr->tr_num_buf_rm;
  91. nbuf -= tr->tr_num_databuf_rm;
  92. if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
  93. (tr->tr_num_revoke <= tr->tr_revokes)))
  94. gfs2_print_trans(tr);
  95. gfs2_log_commit(sdp, tr);
  96. if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
  97. kfree(tr);
  98. up_read(&sdp->sd_log_flush_lock);
  99. if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
  100. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  101. GFS2_LFC_TRANS_END);
  102. if (alloced)
  103. sb_end_intwrite(sdp->sd_vfs);
  104. }
  105. static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
  106. struct buffer_head *bh,
  107. const struct gfs2_log_operations *lops)
  108. {
  109. struct gfs2_bufdata *bd;
  110. bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
  111. bd->bd_bh = bh;
  112. bd->bd_gl = gl;
  113. bd->bd_ops = lops;
  114. INIT_LIST_HEAD(&bd->bd_list);
  115. bh->b_private = bd;
  116. return bd;
  117. }
  118. /**
  119. * gfs2_trans_add_data - Add a databuf to the transaction.
  120. * @gl: The inode glock associated with the buffer
  121. * @bh: The buffer to add
  122. *
  123. * This is used in journaled data mode.
  124. * We need to journal the data block in the same way as metadata in
  125. * the functions above. The difference is that here we have a tag
  126. * which is two __be64's being the block number (as per meta data)
  127. * and a flag which says whether the data block needs escaping or
  128. * not. This means we need a new log entry for each 251 or so data
  129. * blocks, which isn't an enormous overhead but twice as much as
  130. * for normal metadata blocks.
  131. */
  132. void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
  133. {
  134. struct gfs2_trans *tr = current->journal_info;
  135. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  136. struct gfs2_bufdata *bd;
  137. lock_buffer(bh);
  138. if (buffer_pinned(bh)) {
  139. set_bit(TR_TOUCHED, &tr->tr_flags);
  140. goto out;
  141. }
  142. gfs2_log_lock(sdp);
  143. bd = bh->b_private;
  144. if (bd == NULL) {
  145. gfs2_log_unlock(sdp);
  146. unlock_buffer(bh);
  147. if (bh->b_private == NULL)
  148. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_databuf_lops);
  149. else
  150. bd = bh->b_private;
  151. lock_buffer(bh);
  152. gfs2_log_lock(sdp);
  153. }
  154. gfs2_assert(sdp, bd->bd_gl == gl);
  155. set_bit(TR_TOUCHED, &tr->tr_flags);
  156. if (list_empty(&bd->bd_list)) {
  157. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  158. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  159. gfs2_pin(sdp, bd->bd_bh);
  160. tr->tr_num_databuf_new++;
  161. list_add_tail(&bd->bd_list, &tr->tr_databuf);
  162. }
  163. gfs2_log_unlock(sdp);
  164. out:
  165. unlock_buffer(bh);
  166. }
  167. void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
  168. {
  169. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  170. struct gfs2_bufdata *bd;
  171. struct gfs2_meta_header *mh;
  172. struct gfs2_trans *tr = current->journal_info;
  173. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  174. lock_buffer(bh);
  175. if (buffer_pinned(bh)) {
  176. set_bit(TR_TOUCHED, &tr->tr_flags);
  177. goto out;
  178. }
  179. gfs2_log_lock(sdp);
  180. bd = bh->b_private;
  181. if (bd == NULL) {
  182. gfs2_log_unlock(sdp);
  183. unlock_buffer(bh);
  184. lock_page(bh->b_page);
  185. if (bh->b_private == NULL)
  186. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_buf_lops);
  187. else
  188. bd = bh->b_private;
  189. unlock_page(bh->b_page);
  190. lock_buffer(bh);
  191. gfs2_log_lock(sdp);
  192. }
  193. gfs2_assert(sdp, bd->bd_gl == gl);
  194. set_bit(TR_TOUCHED, &tr->tr_flags);
  195. if (!list_empty(&bd->bd_list))
  196. goto out_unlock;
  197. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  198. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  199. mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
  200. if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
  201. pr_err("Attempting to add uninitialised block to journal (inplace block=%lld)\n",
  202. (unsigned long long)bd->bd_bh->b_blocknr);
  203. BUG();
  204. }
  205. if (unlikely(state == SFS_FROZEN)) {
  206. printk(KERN_INFO "GFS2:adding buf while frozen\n");
  207. gfs2_assert_withdraw(sdp, 0);
  208. }
  209. gfs2_pin(sdp, bd->bd_bh);
  210. mh->__pad0 = cpu_to_be64(0);
  211. mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  212. list_add(&bd->bd_list, &tr->tr_buf);
  213. tr->tr_num_buf_new++;
  214. out_unlock:
  215. gfs2_log_unlock(sdp);
  216. out:
  217. unlock_buffer(bh);
  218. }
  219. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  220. {
  221. struct gfs2_trans *tr = current->journal_info;
  222. BUG_ON(!list_empty(&bd->bd_list));
  223. gfs2_add_revoke(sdp, bd);
  224. set_bit(TR_TOUCHED, &tr->tr_flags);
  225. tr->tr_num_revoke++;
  226. }
  227. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  228. {
  229. struct gfs2_bufdata *bd, *tmp;
  230. struct gfs2_trans *tr = current->journal_info;
  231. unsigned int n = len;
  232. gfs2_log_lock(sdp);
  233. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_list) {
  234. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  235. list_del_init(&bd->bd_list);
  236. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  237. sdp->sd_log_num_revoke--;
  238. if (bd->bd_gl)
  239. gfs2_glock_remove_revoke(bd->bd_gl);
  240. kmem_cache_free(gfs2_bufdata_cachep, bd);
  241. tr->tr_num_revoke_rm++;
  242. if (--n == 0)
  243. break;
  244. }
  245. }
  246. gfs2_log_unlock(sdp);
  247. }