trans.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. tr->tr_alloced = 1;
  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. sb_start_intwrite(sdp->sd_vfs);
  52. error = gfs2_log_reserve(sdp, tr->tr_reserved);
  53. if (error)
  54. goto fail;
  55. current->journal_info = tr;
  56. return 0;
  57. fail:
  58. sb_end_intwrite(sdp->sd_vfs);
  59. kfree(tr);
  60. return error;
  61. }
  62. static void gfs2_print_trans(const struct gfs2_trans *tr)
  63. {
  64. pr_warn("Transaction created at: %pSR\n", (void *)tr->tr_ip);
  65. pr_warn("blocks=%u revokes=%u reserved=%u touched=%u\n",
  66. tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, tr->tr_touched);
  67. pr_warn("Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
  68. tr->tr_num_buf_new, tr->tr_num_buf_rm,
  69. tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
  70. tr->tr_num_revoke, tr->tr_num_revoke_rm);
  71. }
  72. void gfs2_trans_end(struct gfs2_sbd *sdp)
  73. {
  74. struct gfs2_trans *tr = current->journal_info;
  75. s64 nbuf;
  76. int alloced = tr->tr_alloced;
  77. BUG_ON(!tr);
  78. current->journal_info = NULL;
  79. if (!tr->tr_touched) {
  80. gfs2_log_release(sdp, tr->tr_reserved);
  81. if (alloced) {
  82. kfree(tr);
  83. sb_end_intwrite(sdp->sd_vfs);
  84. }
  85. return;
  86. }
  87. nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
  88. nbuf -= tr->tr_num_buf_rm;
  89. nbuf -= tr->tr_num_databuf_rm;
  90. if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
  91. (tr->tr_num_revoke <= tr->tr_revokes)))
  92. gfs2_print_trans(tr);
  93. gfs2_log_commit(sdp, tr);
  94. if (alloced && !tr->tr_attached)
  95. kfree(tr);
  96. up_read(&sdp->sd_log_flush_lock);
  97. if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
  98. gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
  99. if (alloced)
  100. sb_end_intwrite(sdp->sd_vfs);
  101. }
  102. static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
  103. struct buffer_head *bh,
  104. const struct gfs2_log_operations *lops)
  105. {
  106. struct gfs2_bufdata *bd;
  107. bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
  108. bd->bd_bh = bh;
  109. bd->bd_gl = gl;
  110. bd->bd_ops = lops;
  111. INIT_LIST_HEAD(&bd->bd_list);
  112. bh->b_private = bd;
  113. return bd;
  114. }
  115. /**
  116. * gfs2_trans_add_data - Add a databuf to the transaction.
  117. * @gl: The inode glock associated with the buffer
  118. * @bh: The buffer to add
  119. *
  120. * This is used in two distinct cases:
  121. * i) In ordered write mode
  122. * We put the data buffer on a list so that we can ensure that its
  123. * synced to disk at the right time
  124. * ii) In journaled data mode
  125. * We need to journal the data block in the same way as metadata in
  126. * the functions above. The difference is that here we have a tag
  127. * which is two __be64's being the block number (as per meta data)
  128. * and a flag which says whether the data block needs escaping or
  129. * not. This means we need a new log entry for each 251 or so data
  130. * blocks, which isn't an enormous overhead but twice as much as
  131. * for normal metadata blocks.
  132. */
  133. void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
  134. {
  135. struct gfs2_trans *tr = current->journal_info;
  136. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  137. struct address_space *mapping = bh->b_page->mapping;
  138. struct gfs2_inode *ip = GFS2_I(mapping->host);
  139. struct gfs2_bufdata *bd;
  140. if (!gfs2_is_jdata(ip)) {
  141. gfs2_ordered_add_inode(ip);
  142. return;
  143. }
  144. lock_buffer(bh);
  145. gfs2_log_lock(sdp);
  146. bd = bh->b_private;
  147. if (bd == NULL) {
  148. gfs2_log_unlock(sdp);
  149. unlock_buffer(bh);
  150. if (bh->b_private == NULL)
  151. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_databuf_lops);
  152. else
  153. bd = bh->b_private;
  154. lock_buffer(bh);
  155. gfs2_log_lock(sdp);
  156. }
  157. gfs2_assert(sdp, bd->bd_gl == gl);
  158. tr->tr_touched = 1;
  159. if (list_empty(&bd->bd_list)) {
  160. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  161. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  162. gfs2_pin(sdp, bd->bd_bh);
  163. tr->tr_num_databuf_new++;
  164. list_add_tail(&bd->bd_list, &tr->tr_databuf);
  165. }
  166. gfs2_log_unlock(sdp);
  167. unlock_buffer(bh);
  168. }
  169. static void meta_lo_add(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  170. {
  171. struct gfs2_meta_header *mh;
  172. struct gfs2_trans *tr;
  173. enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
  174. tr = current->journal_info;
  175. tr->tr_touched = 1;
  176. if (!list_empty(&bd->bd_list))
  177. return;
  178. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  179. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  180. mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
  181. if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
  182. pr_err("Attempting to add uninitialised block to journal (inplace block=%lld)\n",
  183. (unsigned long long)bd->bd_bh->b_blocknr);
  184. BUG();
  185. }
  186. if (unlikely(state == SFS_FROZEN)) {
  187. printk(KERN_INFO "GFS2:adding buf while frozen\n");
  188. gfs2_assert_withdraw(sdp, 0);
  189. }
  190. gfs2_pin(sdp, bd->bd_bh);
  191. mh->__pad0 = cpu_to_be64(0);
  192. mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  193. list_add(&bd->bd_list, &tr->tr_buf);
  194. tr->tr_num_buf_new++;
  195. }
  196. void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
  197. {
  198. struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
  199. struct gfs2_bufdata *bd;
  200. lock_buffer(bh);
  201. gfs2_log_lock(sdp);
  202. bd = bh->b_private;
  203. if (bd == NULL) {
  204. gfs2_log_unlock(sdp);
  205. unlock_buffer(bh);
  206. lock_page(bh->b_page);
  207. if (bh->b_private == NULL)
  208. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_buf_lops);
  209. else
  210. bd = bh->b_private;
  211. unlock_page(bh->b_page);
  212. lock_buffer(bh);
  213. gfs2_log_lock(sdp);
  214. }
  215. gfs2_assert(sdp, bd->bd_gl == gl);
  216. meta_lo_add(sdp, bd);
  217. gfs2_log_unlock(sdp);
  218. unlock_buffer(bh);
  219. }
  220. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  221. {
  222. struct gfs2_trans *tr = current->journal_info;
  223. BUG_ON(!list_empty(&bd->bd_list));
  224. gfs2_add_revoke(sdp, bd);
  225. tr->tr_touched = 1;
  226. tr->tr_num_revoke++;
  227. }
  228. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  229. {
  230. struct gfs2_bufdata *bd, *tmp;
  231. struct gfs2_trans *tr = current->journal_info;
  232. unsigned int n = len;
  233. gfs2_log_lock(sdp);
  234. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_list) {
  235. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  236. list_del_init(&bd->bd_list);
  237. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  238. sdp->sd_log_num_revoke--;
  239. kmem_cache_free(gfs2_bufdata_cachep, bd);
  240. tr->tr_num_revoke_rm++;
  241. if (--n == 0)
  242. break;
  243. }
  244. }
  245. gfs2_log_unlock(sdp);
  246. }