trans.c 8.0 KB

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