trans.c 7.0 KB

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