blob: ffe84050508268c9c852c827d9fb1ad1ad5ee17c [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
Steven Whitehousef55ab262006-02-21 12:51:39 +000040 tr = kzalloc(sizeof(struct gfs2_trans), 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);
55
Jan Kara39263d5e2012-06-12 16:20:41 +020056 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000057
David Teiglandb3b94fa2006-01-16 16:50:04 +000058 error = gfs2_log_reserve(sdp, tr->tr_reserved);
59 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050060 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000061
Steven Whitehouse5c676f62006-02-27 17:23:27 -050062 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000063
64 return 0;
65
Benjamin Marzinski24972552014-05-01 22:26:55 -050066fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020067 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000068 kfree(tr);
69
70 return error;
71}
72
Bob Petersone54c78a2018-10-03 08:47:36 -050073static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
Steven Whitehousec50b91c2012-04-16 16:40:56 +010074{
Bob Petersone54c78a2018-10-03 08:47:36 -050075 fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
76 fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
Bob Peterson9862ca02017-01-25 12:50:47 -050077 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
78 test_bit(TR_TOUCHED, &tr->tr_flags));
Bob Petersona31b4ec2020-01-20 15:49:28 +010079 fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -080080 tr->tr_num_buf_new, tr->tr_num_buf_rm,
81 tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
Bob Petersona31b4ec2020-01-20 15:49:28 +010082 tr->tr_num_revoke, tr->tr_num_revoke_rm);
Steven Whitehousec50b91c2012-04-16 16:40:56 +010083}
84
David Teiglandb3b94fa2006-01-16 16:50:04 +000085void gfs2_trans_end(struct gfs2_sbd *sdp)
86{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040087 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010088 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050089 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060090
Steven Whitehouse5c676f62006-02-27 17:23:27 -050091 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000092
Bob Peterson9862ca02017-01-25 12:50:47 -050093 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000094 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060095 if (alloced) {
Steven Whitehoused8348de2009-02-05 10:12:38 +000096 kfree(tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060097 sb_end_intwrite(sdp->sd_vfs);
98 }
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 return;
100 }
101
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100102 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
103 nbuf -= tr->tr_num_buf_rm;
104 nbuf -= tr->tr_num_databuf_rm;
105
106 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
107 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500108 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
110 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500111 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
112 kfree(tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500113 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800115 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500116 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
117 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600118 if (alloced)
119 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120}
121
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000122static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600123 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000124{
125 struct gfs2_bufdata *bd;
126
127 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
128 bd->bd_bh = bh;
129 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000130 INIT_LIST_HEAD(&bd->bd_list);
131 bh->b_private = bd;
132 return bd;
133}
134
David Teiglandb3b94fa2006-01-16 16:50:04 +0000135/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000136 * gfs2_trans_add_data - Add a databuf to the transaction.
137 * @gl: The inode glock associated with the buffer
138 * @bh: The buffer to add
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500140 * This is used in journaled data mode.
141 * We need to journal the data block in the same way as metadata in
142 * the functions above. The difference is that here we have a tag
143 * which is two __be64's being the block number (as per meta data)
144 * and a flag which says whether the data block needs escaping or
145 * not. This means we need a new log entry for each 251 or so data
146 * blocks, which isn't an enormous overhead but twice as much as
147 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000148 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000149void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
150{
Steven Whitehouse45138992013-01-28 09:30:07 +0000151 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500152 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 struct gfs2_bufdata *bd;
154
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600155 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500156 if (buffer_pinned(bh)) {
157 set_bit(TR_TOUCHED, &tr->tr_flags);
158 goto out;
159 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600160 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500161 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000162 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600163 gfs2_log_unlock(sdp);
164 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000165 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600166 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500167 else
168 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600169 lock_buffer(bh);
170 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000171 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000172 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500173 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000174 if (list_empty(&bd->bd_list)) {
175 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
176 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
177 gfs2_pin(sdp, bd->bd_bh);
178 tr->tr_num_databuf_new++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000179 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000180 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600181 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500182out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600183 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184}
185
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000186void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
187{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000188
Bob Peterson15562c42015-03-16 11:52:05 -0500189 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000190 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500191 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500192 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500193 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000194
195 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500196 if (buffer_pinned(bh)) {
197 set_bit(TR_TOUCHED, &tr->tr_flags);
198 goto out;
199 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000200 gfs2_log_lock(sdp);
201 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000202 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000203 gfs2_log_unlock(sdp);
204 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000205 lock_page(bh->b_page);
206 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600207 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500208 else
209 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000210 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000211 lock_buffer(bh);
212 gfs2_log_lock(sdp);
213 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000214 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500215 set_bit(TR_TOUCHED, &tr->tr_flags);
216 if (!list_empty(&bd->bd_list))
217 goto out_unlock;
218 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
219 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
220 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
221 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500222 fs_err(sdp, "Attempting to add uninitialised block to "
223 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500224 (unsigned long long)bd->bd_bh->b_blocknr);
225 BUG();
226 }
227 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500228 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500229 gfs2_assert_withdraw(sdp, 0);
230 }
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600231 if (unlikely(gfs2_withdrawn(sdp))) {
232 fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
233 (unsigned long long)bd->bd_bh->b_blocknr);
234 }
Bob Peterson192738b2017-01-25 12:57:42 -0500235 gfs2_pin(sdp, bd->bd_bh);
236 mh->__pad0 = cpu_to_be64(0);
237 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
238 list_add(&bd->bd_list, &tr->tr_buf);
239 tr->tr_num_buf_new++;
240out_unlock:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000241 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500242out:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000243 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000244}
245
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100246void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000247{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000248 struct gfs2_trans *tr = current->journal_info;
249
Bob Petersonc0752aa2012-05-01 12:00:34 -0400250 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500251 gfs2_add_revoke(sdp, bd);
Bob Peterson9862ca02017-01-25 12:50:47 -0500252 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000253 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254}
255
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100256void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000257{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000258 struct gfs2_bufdata *bd, *tmp;
259 struct gfs2_trans *tr = current->journal_info;
260 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000261
262 gfs2_log_lock(sdp);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100263 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000264 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400265 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
267 sdp->sd_log_num_revoke--;
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500268 if (bd->bd_gl)
269 gfs2_glock_remove_revoke(bd->bd_gl);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000270 kmem_cache_free(gfs2_bufdata_cachep, bd);
Bob Petersona31b4ec2020-01-20 15:49:28 +0100271 tr->tr_num_revoke_rm++;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000272 if (--n == 0)
273 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 }
275 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000276 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277}
278