trans.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. if (!tr->tr_attached)
  117. kfree(tr);
  118. }
  119. up_read(&sdp->sd_log_flush_lock);
  120. if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
  121. gfs2_log_flush(sdp, NULL);
  122. sb_end_intwrite(sdp->sd_vfs);
  123. }
  124. static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
  125. struct buffer_head *bh,
  126. const struct gfs2_log_operations *lops)
  127. {
  128. struct gfs2_bufdata *bd;
  129. bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
  130. bd->bd_bh = bh;
  131. bd->bd_gl = gl;
  132. bd->bd_ops = lops;
  133. INIT_LIST_HEAD(&bd->bd_list);
  134. bh->b_private = bd;
  135. return bd;
  136. }
  137. /**
  138. * gfs2_trans_add_data - Add a databuf to the transaction.
  139. * @gl: The inode glock associated with the buffer
  140. * @bh: The buffer to add
  141. *
  142. * This is used in two distinct cases:
  143. * i) In ordered write mode
  144. * We put the data buffer on a list so that we can ensure that its
  145. * synced to disk at the right time
  146. * ii) In journaled data mode
  147. * We need to journal the data block in the same way as metadata in
  148. * the functions above. The difference is that here we have a tag
  149. * which is two __be64's being the block number (as per meta data)
  150. * and a flag which says whether the data block needs escaping or
  151. * not. This means we need a new log entry for each 251 or so data
  152. * blocks, which isn't an enormous overhead but twice as much as
  153. * for normal metadata blocks.
  154. */
  155. void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
  156. {
  157. struct gfs2_trans *tr = current->journal_info;
  158. struct gfs2_sbd *sdp = gl->gl_sbd;
  159. struct address_space *mapping = bh->b_page->mapping;
  160. struct gfs2_inode *ip = GFS2_I(mapping->host);
  161. struct gfs2_bufdata *bd;
  162. if (!gfs2_is_jdata(ip)) {
  163. gfs2_ordered_add_inode(ip);
  164. return;
  165. }
  166. lock_buffer(bh);
  167. gfs2_log_lock(sdp);
  168. bd = bh->b_private;
  169. if (bd == NULL) {
  170. gfs2_log_unlock(sdp);
  171. unlock_buffer(bh);
  172. if (bh->b_private == NULL)
  173. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_databuf_lops);
  174. lock_buffer(bh);
  175. gfs2_log_lock(sdp);
  176. }
  177. gfs2_assert(sdp, bd->bd_gl == gl);
  178. tr->tr_touched = 1;
  179. if (list_empty(&bd->bd_list)) {
  180. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  181. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  182. gfs2_pin(sdp, bd->bd_bh);
  183. tr->tr_num_databuf_new++;
  184. sdp->sd_log_num_databuf++;
  185. list_add_tail(&bd->bd_list, &sdp->sd_log_le_databuf);
  186. }
  187. gfs2_log_unlock(sdp);
  188. unlock_buffer(bh);
  189. }
  190. static void meta_lo_add(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  191. {
  192. struct gfs2_meta_header *mh;
  193. struct gfs2_trans *tr;
  194. tr = current->journal_info;
  195. tr->tr_touched = 1;
  196. if (!list_empty(&bd->bd_list))
  197. return;
  198. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  199. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  200. mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
  201. if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
  202. printk(KERN_ERR
  203. "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. sdp->sd_log_num_buf++;
  211. list_add(&bd->bd_list, &sdp->sd_log_le_buf);
  212. tr->tr_num_buf_new++;
  213. }
  214. void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
  215. {
  216. struct gfs2_sbd *sdp = gl->gl_sbd;
  217. struct gfs2_bufdata *bd;
  218. lock_buffer(bh);
  219. gfs2_log_lock(sdp);
  220. bd = bh->b_private;
  221. if (bd == NULL) {
  222. gfs2_log_unlock(sdp);
  223. unlock_buffer(bh);
  224. lock_page(bh->b_page);
  225. if (bh->b_private == NULL)
  226. bd = gfs2_alloc_bufdata(gl, bh, &gfs2_buf_lops);
  227. unlock_page(bh->b_page);
  228. lock_buffer(bh);
  229. gfs2_log_lock(sdp);
  230. }
  231. gfs2_assert(sdp, bd->bd_gl == gl);
  232. meta_lo_add(sdp, bd);
  233. gfs2_log_unlock(sdp);
  234. unlock_buffer(bh);
  235. }
  236. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  237. {
  238. struct gfs2_trans *tr = current->journal_info;
  239. BUG_ON(!list_empty(&bd->bd_list));
  240. gfs2_add_revoke(sdp, bd);
  241. tr->tr_touched = 1;
  242. tr->tr_num_revoke++;
  243. }
  244. void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  245. {
  246. struct gfs2_bufdata *bd, *tmp;
  247. struct gfs2_trans *tr = current->journal_info;
  248. unsigned int n = len;
  249. gfs2_log_lock(sdp);
  250. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_list) {
  251. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  252. list_del_init(&bd->bd_list);
  253. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  254. sdp->sd_log_num_revoke--;
  255. kmem_cache_free(gfs2_bufdata_cachep, bd);
  256. tr->tr_num_revoke_rm++;
  257. if (--n == 0)
  258. break;
  259. }
  260. }
  261. gfs2_log_unlock(sdp);
  262. }