trans.c 7.8 KB

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