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 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 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 17 #include "gfs2.h" 18 #include "incore.h" 19 #include "glock.h" 20 #include "inode.h" 21 #include "log.h" 22 #include "lops.h" 23 #include "meta_io.h" 24 #include "trans.h" 25 #include "util.h" 26 #include "trace_gfs2.h" 27 28 static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr) 29 { 30 fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip); 31 fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n", 32 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, 33 test_bit(TR_TOUCHED, &tr->tr_flags)); 34 fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u/%u\n", 35 tr->tr_num_buf_new, tr->tr_num_buf_rm, 36 tr->tr_num_databuf_new, tr->tr_num_databuf_rm, 37 tr->tr_num_revoke, tr->tr_num_revoke_rm); 38 } 39 40 int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, 41 unsigned int blocks, unsigned int revokes, 42 unsigned long ip) 43 { 44 if (current->journal_info) { 45 gfs2_print_trans(sdp, current->journal_info); 46 BUG(); 47 } 48 BUG_ON(blocks == 0 && revokes == 0); 49 50 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) 51 return -EROFS; 52 53 tr->tr_ip = ip; 54 tr->tr_blocks = blocks; 55 tr->tr_revokes = revokes; 56 tr->tr_reserved = 1; 57 if (blocks) 58 tr->tr_reserved += 6 + blocks; 59 if (revokes) 60 tr->tr_reserved += gfs2_struct2blk(sdp, revokes); 61 INIT_LIST_HEAD(&tr->tr_databuf); 62 INIT_LIST_HEAD(&tr->tr_buf); 63 INIT_LIST_HEAD(&tr->tr_list); 64 INIT_LIST_HEAD(&tr->tr_ail1_list); 65 INIT_LIST_HEAD(&tr->tr_ail2_list); 66 67 if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks)) 68 return -EINVAL; 69 70 sb_start_intwrite(sdp->sd_vfs); 71 72 gfs2_log_reserve(sdp, tr->tr_reserved); 73 74 down_read(&sdp->sd_log_flush_lock); 75 if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { 76 gfs2_log_release(sdp, tr->tr_reserved); 77 up_read(&sdp->sd_log_flush_lock); 78 sb_end_intwrite(sdp->sd_vfs); 79 wake_up(&sdp->sd_log_waitq); 80 return -EROFS; 81 } 82 83 current->journal_info = tr; 84 85 return 0; 86 } 87 88 int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks, 89 unsigned int revokes) 90 { 91 struct gfs2_trans *tr; 92 int error; 93 94 tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS); 95 if (!tr) 96 return -ENOMEM; 97 error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_); 98 if (error) 99 kmem_cache_free(gfs2_trans_cachep, tr); 100 return error; 101 } 102 103 void gfs2_trans_end(struct gfs2_sbd *sdp) 104 { 105 struct gfs2_trans *tr = current->journal_info; 106 s64 nbuf; 107 108 current->journal_info = NULL; 109 110 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) { 111 gfs2_log_release(sdp, tr->tr_reserved); 112 up_read(&sdp->sd_log_flush_lock); 113 if (!test_bit(TR_ONSTACK, &tr->tr_flags)) 114 gfs2_trans_free(sdp, tr); 115 sb_end_intwrite(sdp->sd_vfs); 116 return; 117 } 118 119 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new; 120 nbuf -= tr->tr_num_buf_rm; 121 nbuf -= tr->tr_num_databuf_rm; 122 123 if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) || 124 gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes)) 125 gfs2_print_trans(sdp, tr); 126 127 gfs2_log_commit(sdp, tr); 128 if (!test_bit(TR_ONSTACK, &tr->tr_flags) && 129 !test_bit(TR_ATTACHED, &tr->tr_flags)) 130 gfs2_trans_free(sdp, tr); 131 up_read(&sdp->sd_log_flush_lock); 132 133 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS) 134 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | 135 GFS2_LFC_TRANS_END); 136 sb_end_intwrite(sdp->sd_vfs); 137 } 138 139 static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl, 140 struct buffer_head *bh) 141 { 142 struct gfs2_bufdata *bd; 143 144 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL); 145 bd->bd_bh = bh; 146 bd->bd_gl = gl; 147 INIT_LIST_HEAD(&bd->bd_list); 148 bh->b_private = bd; 149 return bd; 150 } 151 152 /** 153 * gfs2_trans_add_data - Add a databuf to the transaction. 154 * @gl: The inode glock associated with the buffer 155 * @bh: The buffer to add 156 * 157 * This is used in journaled data mode. 158 * We need to journal the data block in the same way as metadata in 159 * the functions above. The difference is that here we have a tag 160 * which is two __be64's being the block number (as per meta data) 161 * and a flag which says whether the data block needs escaping or 162 * not. This means we need a new log entry for each 251 or so data 163 * blocks, which isn't an enormous overhead but twice as much as 164 * for normal metadata blocks. 165 */ 166 void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh) 167 { 168 struct gfs2_trans *tr = current->journal_info; 169 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 170 struct gfs2_bufdata *bd; 171 172 lock_buffer(bh); 173 if (buffer_pinned(bh)) { 174 set_bit(TR_TOUCHED, &tr->tr_flags); 175 goto out; 176 } 177 gfs2_log_lock(sdp); 178 bd = bh->b_private; 179 if (bd == NULL) { 180 gfs2_log_unlock(sdp); 181 unlock_buffer(bh); 182 if (bh->b_private == NULL) 183 bd = gfs2_alloc_bufdata(gl, bh); 184 else 185 bd = bh->b_private; 186 lock_buffer(bh); 187 gfs2_log_lock(sdp); 188 } 189 gfs2_assert(sdp, bd->bd_gl == gl); 190 set_bit(TR_TOUCHED, &tr->tr_flags); 191 if (list_empty(&bd->bd_list)) { 192 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); 193 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); 194 gfs2_pin(sdp, bd->bd_bh); 195 tr->tr_num_databuf_new++; 196 list_add_tail(&bd->bd_list, &tr->tr_databuf); 197 } 198 gfs2_log_unlock(sdp); 199 out: 200 unlock_buffer(bh); 201 } 202 203 void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh) 204 { 205 206 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; 207 struct gfs2_bufdata *bd; 208 struct gfs2_meta_header *mh; 209 struct gfs2_trans *tr = current->journal_info; 210 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); 211 212 lock_buffer(bh); 213 if (buffer_pinned(bh)) { 214 set_bit(TR_TOUCHED, &tr->tr_flags); 215 goto out; 216 } 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); 225 else 226 bd = bh->b_private; 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 set_bit(TR_TOUCHED, &tr->tr_flags); 233 if (!list_empty(&bd->bd_list)) 234 goto out_unlock; 235 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); 236 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); 237 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data; 238 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) { 239 fs_err(sdp, "Attempting to add uninitialised block to " 240 "journal (inplace block=%lld)\n", 241 (unsigned long long)bd->bd_bh->b_blocknr); 242 BUG(); 243 } 244 if (unlikely(state == SFS_FROZEN)) { 245 fs_info(sdp, "GFS2:adding buf while frozen\n"); 246 gfs2_assert_withdraw(sdp, 0); 247 } 248 if (unlikely(gfs2_withdrawn(sdp))) { 249 fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n", 250 (unsigned long long)bd->bd_bh->b_blocknr); 251 } 252 gfs2_pin(sdp, bd->bd_bh); 253 mh->__pad0 = cpu_to_be64(0); 254 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); 255 list_add(&bd->bd_list, &tr->tr_buf); 256 tr->tr_num_buf_new++; 257 out_unlock: 258 gfs2_log_unlock(sdp); 259 out: 260 unlock_buffer(bh); 261 } 262 263 void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) 264 { 265 struct gfs2_trans *tr = current->journal_info; 266 267 BUG_ON(!list_empty(&bd->bd_list)); 268 gfs2_add_revoke(sdp, bd); 269 set_bit(TR_TOUCHED, &tr->tr_flags); 270 tr->tr_num_revoke++; 271 } 272 273 void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len) 274 { 275 struct gfs2_bufdata *bd, *tmp; 276 struct gfs2_trans *tr = current->journal_info; 277 unsigned int n = len; 278 279 gfs2_log_lock(sdp); 280 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) { 281 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) { 282 list_del_init(&bd->bd_list); 283 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke); 284 sdp->sd_log_num_revoke--; 285 if (bd->bd_gl) 286 gfs2_glock_remove_revoke(bd->bd_gl); 287 kmem_cache_free(gfs2_bufdata_cachep, bd); 288 tr->tr_num_revoke_rm++; 289 if (--n == 0) 290 break; 291 } 292 } 293 gfs2_log_unlock(sdp); 294 } 295 296 void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr) 297 { 298 if (tr == NULL) 299 return; 300 301 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); 302 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list)); 303 gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf)); 304 gfs2_assert_warn(sdp, list_empty(&tr->tr_buf)); 305 kmem_cache_free(gfs2_trans_cachep, tr); 306 } 307