trans.c 8.0 KB

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