trans.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  4. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/sched.h>
  8. #include <linux/slab.h>
  9. #include <linux/spinlock.h>
  10. #include <linux/completion.h>
  11. #include <linux/buffer_head.h>
  12. #include <linux/kallsyms.h>
  13. #include <linux/gfs2_ondisk.h>
  14. #include "gfs2.h"
  15. #include "incore.h"
  16. #include "glock.h"
  17. #include "inode.h"
  18. #include "log.h"
  19. #include "lops.h"
  20. #include "meta_io.h"
  21. #include "trans.h"
  22. #include "util.h"
  23. #include "trace_gfs2.h"
  24. static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
  25. {
  26. fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
  27. fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
  28. tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
  29. test_bit(TR_TOUCHED, &tr->tr_flags));
  30. fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u\n",
  31. tr->tr_num_buf_new, tr->tr_num_buf_rm,
  32. tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
  33. tr->tr_num_revoke);
  34. }
  35. int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp,
  36. unsigned int blocks, unsigned int revokes,
  37. unsigned long ip)
  38. {
  39. unsigned int extra_revokes;
  40. if (current->journal_info) {
  41. gfs2_print_trans(sdp, current->journal_info);
  42. BUG();
  43. }
  44. BUG_ON(blocks == 0 && revokes == 0);
  45. if (gfs2_withdrawn(sdp))
  46. return -EROFS;
  47. tr->tr_ip = ip;
  48. tr->tr_blocks = blocks;
  49. tr->tr_revokes = revokes;
  50. tr->tr_reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
  51. if (blocks) {
  52. /*
  53. * The reserved blocks are either used for data or metadata.
  54. * We can have mixed data and metadata, each with its own log
  55. * descriptor block; see calc_reserved().
  56. */
  57. tr->tr_reserved += blocks + 1 + DIV_ROUND_UP(blocks - 1, databuf_limit(sdp));
  58. }
  59. INIT_LIST_HEAD(&tr->tr_databuf);
  60. INIT_LIST_HEAD(&tr->tr_buf);
  61. INIT_LIST_HEAD(&tr->tr_list);
  62. INIT_LIST_HEAD(&tr->tr_ail1_list);
  63. INIT_LIST_HEAD(&tr->tr_ail2_list);
  64. if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks))
  65. return -EINVAL;
  66. sb_start_intwrite(sdp->sd_vfs);
  67. /*
  68. * Try the reservations under sd_log_flush_lock to prevent log flushes
  69. * from creating inconsistencies between the number of allocated and
  70. * reserved revokes. If that fails, do a full-block allocation outside
  71. * of the lock to avoid stalling log flushes. Then, allot the
  72. * appropriate number of blocks to revokes, use as many revokes locally
  73. * as needed, and "release" the surplus into the revokes pool.
  74. */
  75. down_read(&sdp->sd_log_flush_lock);
  76. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)))
  77. goto out_not_live;
  78. if (gfs2_log_try_reserve(sdp, tr, &extra_revokes))
  79. goto reserved;
  80. up_read(&sdp->sd_log_flush_lock);
  81. gfs2_log_reserve(sdp, tr, &extra_revokes);
  82. down_read(&sdp->sd_log_flush_lock);
  83. if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
  84. revokes = tr->tr_revokes + extra_revokes;
  85. gfs2_log_release_revokes(sdp, revokes);
  86. gfs2_log_release(sdp, tr->tr_reserved);
  87. goto out_not_live;
  88. }
  89. reserved:
  90. gfs2_log_release_revokes(sdp, extra_revokes);
  91. current->journal_info = tr;
  92. return 0;
  93. out_not_live:
  94. up_read(&sdp->sd_log_flush_lock);
  95. sb_end_intwrite(sdp->sd_vfs);
  96. return -EROFS;
  97. }
  98. int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
  99. unsigned int revokes)
  100. {
  101. struct gfs2_trans *tr;
  102. int error;
  103. tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
  104. if (!tr)
  105. return -ENOMEM;
  106. error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_);
  107. if (error)
  108. kmem_cache_free(gfs2_trans_cachep, tr);
  109. return error;
  110. }
  111. void gfs2_trans_end(struct gfs2_sbd *sdp)
  112. {
  113. struct gfs2_trans *tr = current->journal_info;
  114. s64 nbuf;
  115. current->journal_info = NULL;
  116. if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
  117. gfs2_log_release_revokes(sdp, tr->tr_revokes);
  118. up_read(&sdp->sd_log_flush_lock);
  119. gfs2_log_release(sdp, tr->tr_reserved);
  120. if (!test_bit(TR_ONSTACK, &tr->tr_flags))
  121. gfs2_trans_free(sdp, tr);
  122. sb_end_intwrite(sdp->sd_vfs);
  123. return;
  124. }
  125. gfs2_log_release_revokes(sdp, tr->tr_revokes - tr->tr_num_revoke);
  126. nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
  127. nbuf -= tr->tr_num_buf_rm;
  128. nbuf -= tr->tr_num_databuf_rm;
  129. if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) ||
  130. gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
  131. gfs2_print_trans(sdp, tr);
  132. gfs2_log_commit(sdp, tr);
  133. if (!test_bit(TR_ONSTACK, &tr->tr_flags) &&
  134. !test_bit(TR_ATTACHED, &tr->tr_flags))
  135. gfs2_trans_free(sdp, tr);
  136. up_read(&sdp->sd_log_flush_lock);
  137. if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
  138. gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
  139. GFS2_LFC_TRANS_END);
  140. sb_end_intwrite(sdp->sd_vfs);
  141. }
  142. static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
  143. struct buffer_head *bh)
  144. {
  145. struct gfs2_bufdata *bd;
  146. bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
  147. bd->bd_bh = bh;
  148. bd->bd_gl = gl;
  149. INIT_LIST_HEAD(&bd->bd_list);
  150. INIT_LIST_HEAD(&bd->bd_ail_st_list);
  151. INIT_LIST_HEAD(&bd->bd_ail_gl_list);
  152. bh->b_private = bd;
  153. return bd;
  154. }
  155. /**
  156. * gfs2_trans_add_data - Add a databuf to the transaction.
  157. * @gl: The inode glock associated with the buffer
  158. * @bh: The buffer to add
  159. *
  160. * This is used in journaled data mode.
  161. * We need to journal the data block in the same way as metadata in
  162. * the functions above. The difference is that here we have a tag
  163. * which is two __be64's being the block number (as per meta data)
  164. * and a flag which says whether the data block needs escaping or
  165. * not. This means we need a new log entry for each 251 or so data
  166. * blocks, which isn't an enormous overhead but twice as much as
  167. * for normal metadata blocks.
  168. */
  169. void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
  170. {
  171. struct gfs2_trans *tr = current->journal_info;
  172. struct gfs2_sbd *sdp = glock_sbd(gl);
  173. struct gfs2_bufdata *bd;
  174. lock_buffer(bh);
  175. if (buffer_pinned(bh)) {
  176. set_bit(TR_TOUCHED, &tr->tr_flags);
  177. goto out;
  178. }
  179. gfs2_log_lock(sdp);
  180. bd = bh->b_private;
  181. if (bd == NULL) {
  182. gfs2_log_unlock(sdp);
  183. unlock_buffer(bh);
  184. if (bh->b_private == NULL)
  185. bd = gfs2_alloc_bufdata(gl, bh);
  186. else
  187. bd = bh->b_private;
  188. lock_buffer(bh);
  189. gfs2_log_lock(sdp);
  190. }
  191. gfs2_assert(sdp, bd->bd_gl == gl);
  192. set_bit(TR_TOUCHED, &tr->tr_flags);
  193. if (list_empty(&bd->bd_list)) {
  194. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  195. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  196. gfs2_pin(sdp, bd->bd_bh);
  197. tr->tr_num_databuf_new++;
  198. list_add_tail(&bd->bd_list, &tr->tr_databuf);
  199. }
  200. gfs2_log_unlock(sdp);
  201. out:
  202. unlock_buffer(bh);
  203. }
  204. void gfs2_trans_add_databufs(struct gfs2_glock *gl, struct folio *folio,
  205. size_t from, size_t len)
  206. {
  207. struct buffer_head *head = folio_buffers(folio);
  208. unsigned int bsize = head->b_size;
  209. struct buffer_head *bh;
  210. size_t to = from + len;
  211. size_t start, end;
  212. for (bh = head, start = 0; bh != head || !start;
  213. bh = bh->b_this_page, start = end) {
  214. end = start + bsize;
  215. if (end <= from)
  216. continue;
  217. if (start >= to)
  218. break;
  219. set_buffer_uptodate(bh);
  220. gfs2_trans_add_data(gl, bh);
  221. }
  222. }
  223. void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
  224. {
  225. struct gfs2_sbd *sdp = glock_sbd(gl);
  226. struct super_block *sb = sdp->sd_vfs;
  227. struct gfs2_bufdata *bd;
  228. struct gfs2_meta_header *mh;
  229. struct gfs2_trans *tr = current->journal_info;
  230. lock_buffer(bh);
  231. if (buffer_pinned(bh)) {
  232. set_bit(TR_TOUCHED, &tr->tr_flags);
  233. goto out;
  234. }
  235. gfs2_log_lock(sdp);
  236. bd = bh->b_private;
  237. if (bd == NULL) {
  238. gfs2_log_unlock(sdp);
  239. unlock_buffer(bh);
  240. folio_lock(bh->b_folio);
  241. if (bh->b_private == NULL)
  242. bd = gfs2_alloc_bufdata(gl, bh);
  243. else
  244. bd = bh->b_private;
  245. folio_unlock(bh->b_folio);
  246. lock_buffer(bh);
  247. gfs2_log_lock(sdp);
  248. }
  249. gfs2_assert(sdp, bd->bd_gl == gl);
  250. set_bit(TR_TOUCHED, &tr->tr_flags);
  251. if (!list_empty(&bd->bd_list))
  252. goto out_unlock;
  253. set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
  254. set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
  255. mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
  256. if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
  257. fs_err(sdp, "Attempting to add uninitialised block to "
  258. "journal (inplace block=%lld)\n",
  259. (unsigned long long)bd->bd_bh->b_blocknr);
  260. BUG();
  261. }
  262. if (gfs2_withdrawn(sdp)) {
  263. fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
  264. (unsigned long long)bd->bd_bh->b_blocknr);
  265. goto out_unlock;
  266. }
  267. if (unlikely(sb->s_writers.frozen == SB_FREEZE_COMPLETE)) {
  268. fs_info(sdp, "GFS2:adding buf while frozen\n");
  269. gfs2_withdraw(sdp);
  270. goto out_unlock;
  271. }
  272. gfs2_pin(sdp, bd->bd_bh);
  273. mh->__pad0 = cpu_to_be64(0);
  274. mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
  275. list_add(&bd->bd_list, &tr->tr_buf);
  276. tr->tr_num_buf_new++;
  277. out_unlock:
  278. gfs2_log_unlock(sdp);
  279. out:
  280. unlock_buffer(bh);
  281. }
  282. void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
  283. {
  284. struct gfs2_trans *tr = current->journal_info;
  285. BUG_ON(!list_empty(&bd->bd_list));
  286. gfs2_add_revoke(sdp, bd);
  287. set_bit(TR_TOUCHED, &tr->tr_flags);
  288. tr->tr_num_revoke++;
  289. }
  290. void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
  291. {
  292. struct gfs2_bufdata *bd, *tmp;
  293. unsigned int n = len;
  294. gfs2_log_lock(sdp);
  295. list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
  296. if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
  297. list_del_init(&bd->bd_list);
  298. gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
  299. sdp->sd_log_num_revoke--;
  300. if (bd->bd_gl)
  301. gfs2_glock_remove_revoke(bd->bd_gl);
  302. kmem_cache_free(gfs2_bufdata_cachep, bd);
  303. gfs2_log_release_revokes(sdp, 1);
  304. if (--n == 0)
  305. break;
  306. }
  307. }
  308. gfs2_log_unlock(sdp);
  309. }
  310. void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
  311. {
  312. if (tr == NULL)
  313. return;
  314. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
  315. gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
  316. gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
  317. gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
  318. kmem_cache_free(gfs2_trans_cachep, tr);
  319. }