17336d0e6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2b3b94faaSDavid Teigland /* 3b3b94faaSDavid Teigland * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 43a8a9a10SSteven Whitehouse * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. 5b3b94faaSDavid Teigland */ 6b3b94faaSDavid Teigland 7d77d1b58SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8d77d1b58SJoe Perches 9b3b94faaSDavid Teigland #include <linux/sched.h> 10b3b94faaSDavid Teigland #include <linux/slab.h> 11b3b94faaSDavid Teigland #include <linux/spinlock.h> 12b3b94faaSDavid Teigland #include <linux/completion.h> 13b3b94faaSDavid Teigland #include <linux/buffer_head.h> 14d0dc80dbSSteven Whitehouse #include <linux/kallsyms.h> 15f057f6cdSSteven Whitehouse #include <linux/gfs2_ondisk.h> 16b3b94faaSDavid Teigland 17b3b94faaSDavid Teigland #include "gfs2.h" 185c676f6dSSteven Whitehouse #include "incore.h" 19b3b94faaSDavid Teigland #include "glock.h" 20767f433fSSteven Whitehouse #include "inode.h" 21b3b94faaSDavid Teigland #include "log.h" 22b3b94faaSDavid Teigland #include "lops.h" 23b3b94faaSDavid Teigland #include "meta_io.h" 24b3b94faaSDavid Teigland #include "trans.h" 255c676f6dSSteven Whitehouse #include "util.h" 265e687eacSBenjamin Marzinski #include "trace_gfs2.h" 27b3b94faaSDavid Teigland 28b0be23b2SBob Peterson static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr) 29b0be23b2SBob Peterson { 30b0be23b2SBob Peterson fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip); 31b0be23b2SBob Peterson fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n", 32b0be23b2SBob Peterson tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, 33b0be23b2SBob Peterson test_bit(TR_TOUCHED, &tr->tr_flags)); 34b0be23b2SBob Peterson fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u/%u\n", 35b0be23b2SBob Peterson tr->tr_num_buf_new, tr->tr_num_buf_rm, 36b0be23b2SBob Peterson tr->tr_num_databuf_new, tr->tr_num_databuf_rm, 37b0be23b2SBob Peterson tr->tr_num_revoke, tr->tr_num_revoke_rm); 38b0be23b2SBob Peterson } 39b0be23b2SBob Peterson 40c968f578SAndreas Gruenbacher int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, 41c968f578SAndreas Gruenbacher unsigned int blocks, unsigned int revokes, 42c968f578SAndreas Gruenbacher unsigned long ip) 43b3b94faaSDavid Teigland { 44b0be23b2SBob Peterson if (current->journal_info) { 45b0be23b2SBob Peterson gfs2_print_trans(sdp, current->journal_info); 46b0be23b2SBob Peterson BUG(); 47b0be23b2SBob Peterson } 48d0dc80dbSSteven Whitehouse BUG_ON(blocks == 0 && revokes == 0); 49b3b94faaSDavid Teigland 50a1c0643fSSteven Whitehouse if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) 51a1c0643fSSteven Whitehouse return -EROFS; 52a1c0643fSSteven Whitehouse 53c968f578SAndreas Gruenbacher tr->tr_ip = ip; 54b3b94faaSDavid Teigland tr->tr_blocks = blocks; 55b3b94faaSDavid Teigland tr->tr_revokes = revokes; 56*fe3e3976SAndreas Gruenbacher tr->tr_reserved = GFS2_LOG_FLUSH_MIN_BLOCKS; 57297de318SAndreas Gruenbacher if (blocks) { 58297de318SAndreas Gruenbacher /* 59297de318SAndreas Gruenbacher * The reserved blocks are either used for data or metadata. 60297de318SAndreas Gruenbacher * We can have mixed data and metadata, each with its own log 61297de318SAndreas Gruenbacher * descriptor block; see calc_reserved(). 62297de318SAndreas Gruenbacher */ 63297de318SAndreas Gruenbacher tr->tr_reserved += blocks + 1 + DIV_ROUND_UP(blocks - 1, databuf_limit(sdp)); 64297de318SAndreas Gruenbacher } 65b3b94faaSDavid Teigland if (revokes) 66*fe3e3976SAndreas Gruenbacher tr->tr_reserved += gfs2_struct2blk(sdp, revokes) - 1; 67d69a3c65SSteven Whitehouse INIT_LIST_HEAD(&tr->tr_databuf); 68d69a3c65SSteven Whitehouse INIT_LIST_HEAD(&tr->tr_buf); 69462582b9SBob Peterson INIT_LIST_HEAD(&tr->tr_list); 70cbcc89b6SBob Peterson INIT_LIST_HEAD(&tr->tr_ail1_list); 71cbcc89b6SBob Peterson INIT_LIST_HEAD(&tr->tr_ail2_list); 72d69a3c65SSteven Whitehouse 73c1eba1b0SAndreas Gruenbacher if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks)) 74c1eba1b0SAndreas Gruenbacher return -EINVAL; 75c1eba1b0SAndreas Gruenbacher 7639263d5eSJan Kara sb_start_intwrite(sdp->sd_vfs); 77b3b94faaSDavid Teigland 78c1eba1b0SAndreas Gruenbacher gfs2_log_reserve(sdp, tr->tr_reserved); 79c1eba1b0SAndreas Gruenbacher 80c1eba1b0SAndreas Gruenbacher down_read(&sdp->sd_log_flush_lock); 81c1eba1b0SAndreas Gruenbacher if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { 82c1eba1b0SAndreas Gruenbacher up_read(&sdp->sd_log_flush_lock); 835ae8fff8SAndreas Gruenbacher gfs2_log_release(sdp, tr->tr_reserved); 84c968f578SAndreas Gruenbacher sb_end_intwrite(sdp->sd_vfs); 85c1eba1b0SAndreas Gruenbacher return -EROFS; 86c968f578SAndreas Gruenbacher } 87b3b94faaSDavid Teigland 885c676f6dSSteven Whitehouse current->journal_info = tr; 89b3b94faaSDavid Teigland 90b3b94faaSDavid Teigland return 0; 91c968f578SAndreas Gruenbacher } 92b3b94faaSDavid Teigland 93c968f578SAndreas Gruenbacher int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks, 94c968f578SAndreas Gruenbacher unsigned int revokes) 95c968f578SAndreas Gruenbacher { 96c968f578SAndreas Gruenbacher struct gfs2_trans *tr; 97c968f578SAndreas Gruenbacher int error; 98c968f578SAndreas Gruenbacher 99c968f578SAndreas Gruenbacher tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS); 100c968f578SAndreas Gruenbacher if (!tr) 101c968f578SAndreas Gruenbacher return -ENOMEM; 102c968f578SAndreas Gruenbacher error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_); 103c968f578SAndreas Gruenbacher if (error) 104b839dadaSBob Peterson kmem_cache_free(gfs2_trans_cachep, tr); 105b3b94faaSDavid Teigland return error; 106b3b94faaSDavid Teigland } 107b3b94faaSDavid Teigland 108b3b94faaSDavid Teigland void gfs2_trans_end(struct gfs2_sbd *sdp) 109b3b94faaSDavid Teigland { 110f4154ea0SSteven Whitehouse struct gfs2_trans *tr = current->journal_info; 111c50b91c4SSteven Whitehouse s64 nbuf; 1122e60d768SBenjamin Marzinski 1135c676f6dSSteven Whitehouse current->journal_info = NULL; 114b3b94faaSDavid Teigland 1159862ca05SBob Peterson if (!test_bit(TR_TOUCHED, &tr->tr_flags)) { 116b3b94faaSDavid Teigland gfs2_log_release(sdp, tr->tr_reserved); 117c1eba1b0SAndreas Gruenbacher up_read(&sdp->sd_log_flush_lock); 118c968f578SAndreas Gruenbacher if (!test_bit(TR_ONSTACK, &tr->tr_flags)) 119b839dadaSBob Peterson gfs2_trans_free(sdp, tr); 12039263d5eSJan Kara sb_end_intwrite(sdp->sd_vfs); 121b3b94faaSDavid Teigland return; 122b3b94faaSDavid Teigland } 123b3b94faaSDavid Teigland 124c50b91c4SSteven Whitehouse nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new; 125c50b91c4SSteven Whitehouse nbuf -= tr->tr_num_buf_rm; 126c50b91c4SSteven Whitehouse nbuf -= tr->tr_num_databuf_rm; 127c50b91c4SSteven Whitehouse 128625a8eddSAndreas Gruenbacher if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) || 129625a8eddSAndreas Gruenbacher gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes)) 130e54c78a2SBob Peterson gfs2_print_trans(sdp, tr); 131b3b94faaSDavid Teigland 132b3b94faaSDavid Teigland gfs2_log_commit(sdp, tr); 133c968f578SAndreas Gruenbacher if (!test_bit(TR_ONSTACK, &tr->tr_flags) && 134c968f578SAndreas Gruenbacher !test_bit(TR_ATTACHED, &tr->tr_flags)) 135b839dadaSBob Peterson gfs2_trans_free(sdp, tr); 13616ca9412SBenjamin Marzinski up_read(&sdp->sd_log_flush_lock); 137b3b94faaSDavid Teigland 1381751e8a6SLinus Torvalds if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS) 139805c0907SBob Peterson gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | 140805c0907SBob Peterson GFS2_LFC_TRANS_END); 14139263d5eSJan Kara sb_end_intwrite(sdp->sd_vfs); 142b3b94faaSDavid Teigland } 143b3b94faaSDavid Teigland 144c76c4d96SSteven Whitehouse static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl, 145cbbe76c8SBob Peterson struct buffer_head *bh) 146c76c4d96SSteven Whitehouse { 147c76c4d96SSteven Whitehouse struct gfs2_bufdata *bd; 148c76c4d96SSteven Whitehouse 149c76c4d96SSteven Whitehouse bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL); 150c76c4d96SSteven Whitehouse bd->bd_bh = bh; 151c76c4d96SSteven Whitehouse bd->bd_gl = gl; 152c76c4d96SSteven Whitehouse INIT_LIST_HEAD(&bd->bd_list); 153c76c4d96SSteven Whitehouse bh->b_private = bd; 154c76c4d96SSteven Whitehouse return bd; 155c76c4d96SSteven Whitehouse } 156c76c4d96SSteven Whitehouse 157b3b94faaSDavid Teigland /** 15845138990SSteven Whitehouse * gfs2_trans_add_data - Add a databuf to the transaction. 15945138990SSteven Whitehouse * @gl: The inode glock associated with the buffer 16045138990SSteven Whitehouse * @bh: The buffer to add 161b3b94faaSDavid Teigland * 162845802b1SAndreas Gruenbacher * This is used in journaled data mode. 163767f433fSSteven Whitehouse * We need to journal the data block in the same way as metadata in 164767f433fSSteven Whitehouse * the functions above. The difference is that here we have a tag 165767f433fSSteven Whitehouse * which is two __be64's being the block number (as per meta data) 166767f433fSSteven Whitehouse * and a flag which says whether the data block needs escaping or 167767f433fSSteven Whitehouse * not. This means we need a new log entry for each 251 or so data 168767f433fSSteven Whitehouse * blocks, which isn't an enormous overhead but twice as much as 169767f433fSSteven Whitehouse * for normal metadata blocks. 170b3b94faaSDavid Teigland */ 171767f433fSSteven Whitehouse void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh) 172767f433fSSteven Whitehouse { 17345138990SSteven Whitehouse struct gfs2_trans *tr = current->journal_info; 17415562c43SBob Peterson struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 175b3b94faaSDavid Teigland struct gfs2_bufdata *bd; 176b3b94faaSDavid Teigland 17796e5d1d3SBenjamin Marzinski lock_buffer(bh); 178aacee720SBob Peterson if (buffer_pinned(bh)) { 179aacee720SBob Peterson set_bit(TR_TOUCHED, &tr->tr_flags); 180aacee720SBob Peterson goto out; 181aacee720SBob Peterson } 18296e5d1d3SBenjamin Marzinski gfs2_log_lock(sdp); 1835c676f6dSSteven Whitehouse bd = bh->b_private; 184c76c4d96SSteven Whitehouse if (bd == NULL) { 18596e5d1d3SBenjamin Marzinski gfs2_log_unlock(sdp); 18696e5d1d3SBenjamin Marzinski unlock_buffer(bh); 187c76c4d96SSteven Whitehouse if (bh->b_private == NULL) 188cbbe76c8SBob Peterson bd = gfs2_alloc_bufdata(gl, bh); 189491e94f7SBob Peterson else 190491e94f7SBob Peterson bd = bh->b_private; 19196e5d1d3SBenjamin Marzinski lock_buffer(bh); 19296e5d1d3SBenjamin Marzinski gfs2_log_lock(sdp); 193b3b94faaSDavid Teigland } 194c76c4d96SSteven Whitehouse gfs2_assert(sdp, bd->bd_gl == gl); 1959862ca05SBob Peterson set_bit(TR_TOUCHED, &tr->tr_flags); 19645138990SSteven Whitehouse if (list_empty(&bd->bd_list)) { 19745138990SSteven Whitehouse set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); 19845138990SSteven Whitehouse set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); 19945138990SSteven Whitehouse gfs2_pin(sdp, bd->bd_bh); 20045138990SSteven Whitehouse tr->tr_num_databuf_new++; 201d69a3c65SSteven Whitehouse list_add_tail(&bd->bd_list, &tr->tr_databuf); 20245138990SSteven Whitehouse } 20396e5d1d3SBenjamin Marzinski gfs2_log_unlock(sdp); 204aacee720SBob Peterson out: 20596e5d1d3SBenjamin Marzinski unlock_buffer(bh); 206b3b94faaSDavid Teigland } 207b3b94faaSDavid Teigland 208350a9b0aSSteven Whitehouse void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh) 209350a9b0aSSteven Whitehouse { 210767f433fSSteven Whitehouse 21115562c43SBob Peterson struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 212767f433fSSteven Whitehouse struct gfs2_bufdata *bd; 213192738b7SBob Peterson struct gfs2_meta_header *mh; 214aacee720SBob Peterson struct gfs2_trans *tr = current->journal_info; 215192738b7SBob Peterson enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); 216767f433fSSteven Whitehouse 217767f433fSSteven Whitehouse lock_buffer(bh); 218aacee720SBob Peterson if (buffer_pinned(bh)) { 219aacee720SBob Peterson set_bit(TR_TOUCHED, &tr->tr_flags); 220aacee720SBob Peterson goto out; 221aacee720SBob Peterson } 222767f433fSSteven Whitehouse gfs2_log_lock(sdp); 223767f433fSSteven Whitehouse bd = bh->b_private; 224c76c4d96SSteven Whitehouse if (bd == NULL) { 225767f433fSSteven Whitehouse gfs2_log_unlock(sdp); 226767f433fSSteven Whitehouse unlock_buffer(bh); 227c76c4d96SSteven Whitehouse lock_page(bh->b_page); 228c76c4d96SSteven Whitehouse if (bh->b_private == NULL) 229cbbe76c8SBob Peterson bd = gfs2_alloc_bufdata(gl, bh); 230491e94f7SBob Peterson else 231491e94f7SBob Peterson bd = bh->b_private; 232c76c4d96SSteven Whitehouse unlock_page(bh->b_page); 233767f433fSSteven Whitehouse lock_buffer(bh); 234767f433fSSteven Whitehouse gfs2_log_lock(sdp); 235767f433fSSteven Whitehouse } 236c76c4d96SSteven Whitehouse gfs2_assert(sdp, bd->bd_gl == gl); 237192738b7SBob Peterson set_bit(TR_TOUCHED, &tr->tr_flags); 238192738b7SBob Peterson if (!list_empty(&bd->bd_list)) 239192738b7SBob Peterson goto out_unlock; 240192738b7SBob Peterson set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); 241192738b7SBob Peterson set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); 242192738b7SBob Peterson mh = (struct gfs2_meta_header *)bd->bd_bh->b_data; 243192738b7SBob Peterson if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) { 244e54c78a2SBob Peterson fs_err(sdp, "Attempting to add uninitialised block to " 245e54c78a2SBob Peterson "journal (inplace block=%lld)\n", 246192738b7SBob Peterson (unsigned long long)bd->bd_bh->b_blocknr); 247192738b7SBob Peterson BUG(); 248192738b7SBob Peterson } 249192738b7SBob Peterson if (unlikely(state == SFS_FROZEN)) { 250e54c78a2SBob Peterson fs_info(sdp, "GFS2:adding buf while frozen\n"); 251192738b7SBob Peterson gfs2_assert_withdraw(sdp, 0); 252192738b7SBob Peterson } 2532ca0c2fbSBob Peterson if (unlikely(gfs2_withdrawn(sdp))) { 2542ca0c2fbSBob Peterson fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n", 2552ca0c2fbSBob Peterson (unsigned long long)bd->bd_bh->b_blocknr); 2562ca0c2fbSBob Peterson } 257192738b7SBob Peterson gfs2_pin(sdp, bd->bd_bh); 258192738b7SBob Peterson mh->__pad0 = cpu_to_be64(0); 259192738b7SBob Peterson mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); 260192738b7SBob Peterson list_add(&bd->bd_list, &tr->tr_buf); 261192738b7SBob Peterson tr->tr_num_buf_new++; 262192738b7SBob Peterson out_unlock: 263767f433fSSteven Whitehouse gfs2_log_unlock(sdp); 264aacee720SBob Peterson out: 265767f433fSSteven Whitehouse unlock_buffer(bh); 266350a9b0aSSteven Whitehouse } 267350a9b0aSSteven Whitehouse 2681ad38c43SSteven Whitehouse void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) 269b3b94faaSDavid Teigland { 27075f2b879SSteven Whitehouse struct gfs2_trans *tr = current->journal_info; 27175f2b879SSteven Whitehouse 272c0752aa7SBob Peterson BUG_ON(!list_empty(&bd->bd_list)); 2735d054964SBenjamin Marzinski gfs2_add_revoke(sdp, bd); 2749862ca05SBob Peterson set_bit(TR_TOUCHED, &tr->tr_flags); 27575f2b879SSteven Whitehouse tr->tr_num_revoke++; 276b3b94faaSDavid Teigland } 277b3b94faaSDavid Teigland 278fbb27873SAndreas Gruenbacher void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len) 279b3b94faaSDavid Teigland { 2805731be53SSteven Whitehouse struct gfs2_bufdata *bd, *tmp; 2815731be53SSteven Whitehouse struct gfs2_trans *tr = current->journal_info; 2825731be53SSteven Whitehouse unsigned int n = len; 283b3b94faaSDavid Teigland 284b3b94faaSDavid Teigland gfs2_log_lock(sdp); 285a5b1d3fcSAndreas Gruenbacher list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) { 2865731be53SSteven Whitehouse if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) { 287c0752aa7SBob Peterson list_del_init(&bd->bd_list); 288b3b94faaSDavid Teigland gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke); 289b3b94faaSDavid Teigland sdp->sd_log_num_revoke--; 290fe5e7ba1SBob Peterson if (bd->bd_gl) 291fe5e7ba1SBob Peterson gfs2_glock_remove_revoke(bd->bd_gl); 2925731be53SSteven Whitehouse kmem_cache_free(gfs2_bufdata_cachep, bd); 293a31b4ec5SBob Peterson tr->tr_num_revoke_rm++; 2945731be53SSteven Whitehouse if (--n == 0) 295b3b94faaSDavid Teigland break; 296b3b94faaSDavid Teigland } 297b3b94faaSDavid Teigland } 298b3b94faaSDavid Teigland gfs2_log_unlock(sdp); 299b3b94faaSDavid Teigland } 300b3b94faaSDavid Teigland 301b839dadaSBob Peterson void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 302b839dadaSBob Peterson { 303b839dadaSBob Peterson if (tr == NULL) 304b839dadaSBob Peterson return; 305b839dadaSBob Peterson 306b839dadaSBob Peterson gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); 307b839dadaSBob Peterson gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list)); 308b839dadaSBob Peterson gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf)); 309b839dadaSBob Peterson gfs2_assert_warn(sdp, list_empty(&tr->tr_buf)); 310b839dadaSBob Peterson kmem_cache_free(gfs2_trans_cachep, tr); 311b839dadaSBob Peterson } 312