blob: a3dfa3aa87ad920147363de4d2f92e2e2c54f9ae [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04004 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
Joe Perchesd77d1b52014-03-06 12:10:45 -08007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
David Teiglandb3b94fa2006-01-16 16:50:04 +00009#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>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050014#include <linux/kallsyms.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000015#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016
17#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050018#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000019#include "glock.h"
Steven Whitehouse767f4332012-12-14 12:52:14 +000020#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "log.h"
22#include "lops.h"
23#include "meta_io.h"
24#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "util.h"
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -050026#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050028int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
29 unsigned int revokes)
David Teiglandb3b94fa2006-01-16 16:50:04 +000030{
31 struct gfs2_trans *tr;
32 int error;
33
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050034 BUG_ON(current->journal_info);
35 BUG_ON(blocks == 0 && revokes == 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000036
Steven Whitehousea1c06432009-05-13 10:56:52 +010037 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
38 return -EROFS;
39
Bob Petersonb839dad2019-04-17 12:04:27 -060040 tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000041 if (!tr)
42 return -ENOMEM;
43
Fabian Frederickd29c0af2014-10-03 20:15:36 +020044 tr->tr_ip = _RET_IP_;
David Teiglandb3b94fa2006-01-16 16:50:04 +000045 tr->tr_blocks = blocks;
46 tr->tr_revokes = revokes;
47 tr->tr_reserved = 1;
Bob Peterson9862ca02017-01-25 12:50:47 -050048 set_bit(TR_ALLOCED, &tr->tr_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000049 if (blocks)
Steven Whitehousef4154ea2006-04-11 14:49:06 -040050 tr->tr_reserved += 6 + blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +000051 if (revokes)
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060052 tr->tr_reserved += gfs2_struct2blk(sdp, revokes);
Steven Whitehoused69a3c62014-02-21 15:22:35 +000053 INIT_LIST_HEAD(&tr->tr_databuf);
54 INIT_LIST_HEAD(&tr->tr_buf);
Bob Petersoncbcc89b2020-06-05 14:12:34 -050055 INIT_LIST_HEAD(&tr->tr_ail1_list);
56 INIT_LIST_HEAD(&tr->tr_ail2_list);
Steven Whitehoused69a3c62014-02-21 15:22:35 +000057
Jan Kara39263d5e2012-06-12 16:20:41 +020058 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000059
David Teiglandb3b94fa2006-01-16 16:50:04 +000060 error = gfs2_log_reserve(sdp, tr->tr_reserved);
61 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050062 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
Steven Whitehouse5c676f62006-02-27 17:23:27 -050064 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
66 return 0;
67
Benjamin Marzinski24972552014-05-01 22:26:55 -050068fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020069 sb_end_intwrite(sdp->sd_vfs);
Bob Petersonb839dad2019-04-17 12:04:27 -060070 kmem_cache_free(gfs2_trans_cachep, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000071
72 return error;
73}
74
Bob Petersone54c78a2018-10-03 08:47:36 -050075static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
Steven Whitehousec50b91c2012-04-16 16:40:56 +010076{
Bob Petersone54c78a2018-10-03 08:47:36 -050077 fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
78 fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
Bob Peterson9862ca02017-01-25 12:50:47 -050079 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
80 test_bit(TR_TOUCHED, &tr->tr_flags));
Bob Petersona31b4ec2020-01-20 15:49:28 +010081 fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -080082 tr->tr_num_buf_new, tr->tr_num_buf_rm,
83 tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
Bob Petersona31b4ec2020-01-20 15:49:28 +010084 tr->tr_num_revoke, tr->tr_num_revoke_rm);
Steven Whitehousec50b91c2012-04-16 16:40:56 +010085}
86
David Teiglandb3b94fa2006-01-16 16:50:04 +000087void gfs2_trans_end(struct gfs2_sbd *sdp)
88{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040089 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010090 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050091 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060092
Steven Whitehouse5c676f62006-02-27 17:23:27 -050093 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000094
Bob Peterson9862ca02017-01-25 12:50:47 -050095 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000096 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060097 if (alloced) {
Bob Petersonb839dad2019-04-17 12:04:27 -060098 gfs2_trans_free(sdp, tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060099 sb_end_intwrite(sdp->sd_vfs);
100 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000101 return;
102 }
103
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100104 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
105 nbuf -= tr->tr_num_buf_rm;
106 nbuf -= tr->tr_num_databuf_rm;
107
108 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
109 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500110 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111
112 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500113 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
Bob Petersonb839dad2019-04-17 12:04:27 -0600114 gfs2_trans_free(sdp, tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500115 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800117 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500118 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
119 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600120 if (alloced)
121 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122}
123
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000124static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600125 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000126{
127 struct gfs2_bufdata *bd;
128
129 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
130 bd->bd_bh = bh;
131 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000132 INIT_LIST_HEAD(&bd->bd_list);
133 bh->b_private = bd;
134 return bd;
135}
136
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000138 * 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
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500142 * This is used in journaled data mode.
143 * We need to journal the data block in the same way as metadata in
144 * the functions above. The difference is that here we have a tag
145 * which is two __be64's being the block number (as per meta data)
146 * and a flag which says whether the data block needs escaping or
147 * not. This means we need a new log entry for each 251 or so data
148 * blocks, which isn't an enormous overhead but twice as much as
149 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000151void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
152{
Steven Whitehouse45138992013-01-28 09:30:07 +0000153 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500154 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 struct gfs2_bufdata *bd;
156
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600157 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500158 if (buffer_pinned(bh)) {
159 set_bit(TR_TOUCHED, &tr->tr_flags);
160 goto out;
161 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600162 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500163 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000164 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600165 gfs2_log_unlock(sdp);
166 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000167 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600168 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500169 else
170 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600171 lock_buffer(bh);
172 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000174 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500175 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000176 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++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000181 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000182 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600183 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500184out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600185 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000186}
187
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000188void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
189{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000190
Bob Peterson15562c42015-03-16 11:52:05 -0500191 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000192 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500193 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500194 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500195 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000196
197 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500198 if (buffer_pinned(bh)) {
199 set_bit(TR_TOUCHED, &tr->tr_flags);
200 goto out;
201 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000202 gfs2_log_lock(sdp);
203 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000204 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000205 gfs2_log_unlock(sdp);
206 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000207 lock_page(bh->b_page);
208 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600209 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500210 else
211 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000212 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000213 lock_buffer(bh);
214 gfs2_log_lock(sdp);
215 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000216 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500217 set_bit(TR_TOUCHED, &tr->tr_flags);
218 if (!list_empty(&bd->bd_list))
219 goto out_unlock;
220 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
221 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
222 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
223 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500224 fs_err(sdp, "Attempting to add uninitialised block to "
225 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500226 (unsigned long long)bd->bd_bh->b_blocknr);
227 BUG();
228 }
229 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500230 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500231 gfs2_assert_withdraw(sdp, 0);
232 }
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600233 if (unlikely(gfs2_withdrawn(sdp))) {
234 fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
235 (unsigned long long)bd->bd_bh->b_blocknr);
236 }
Bob Peterson192738b2017-01-25 12:57:42 -0500237 gfs2_pin(sdp, bd->bd_bh);
238 mh->__pad0 = cpu_to_be64(0);
239 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
240 list_add(&bd->bd_list, &tr->tr_buf);
241 tr->tr_num_buf_new++;
242out_unlock:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000243 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500244out:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000245 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000246}
247
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100248void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000249{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000250 struct gfs2_trans *tr = current->journal_info;
251
Bob Petersonc0752aa2012-05-01 12:00:34 -0400252 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500253 gfs2_add_revoke(sdp, bd);
Bob Peterson9862ca02017-01-25 12:50:47 -0500254 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000255 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000256}
257
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100258void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000260 struct gfs2_bufdata *bd, *tmp;
261 struct gfs2_trans *tr = current->journal_info;
262 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263
264 gfs2_log_lock(sdp);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100265 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000266 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400267 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
269 sdp->sd_log_num_revoke--;
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500270 if (bd->bd_gl)
271 gfs2_glock_remove_revoke(bd->bd_gl);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000272 kmem_cache_free(gfs2_bufdata_cachep, bd);
Bob Petersona31b4ec2020-01-20 15:49:28 +0100273 tr->tr_num_revoke_rm++;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000274 if (--n == 0)
275 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276 }
277 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279}
280
Bob Petersonb839dad2019-04-17 12:04:27 -0600281void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
282{
283 if (tr == NULL)
284 return;
285
286 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
287 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
288 gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
289 gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
290 kmem_cache_free(gfs2_trans_cachep, tr);
291}