blob: e1c7eb6eb00a4018b5e97495b3aa93bdc13a2ab8 [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
Bob Petersonb0be23b2020-07-23 13:14:07 -050028static 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
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050040int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
41 unsigned int revokes)
David Teiglandb3b94fa2006-01-16 16:50:04 +000042{
43 struct gfs2_trans *tr;
44 int error;
45
Bob Petersonb0be23b2020-07-23 13:14:07 -050046 if (current->journal_info) {
47 gfs2_print_trans(sdp, current->journal_info);
48 BUG();
49 }
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050050 BUG_ON(blocks == 0 && revokes == 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000051
Steven Whitehousea1c06432009-05-13 10:56:52 +010052 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
53 return -EROFS;
54
Bob Petersonb839dad2019-04-17 12:04:27 -060055 tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000056 if (!tr)
57 return -ENOMEM;
58
Fabian Frederickd29c0af2014-10-03 20:15:36 +020059 tr->tr_ip = _RET_IP_;
David Teiglandb3b94fa2006-01-16 16:50:04 +000060 tr->tr_blocks = blocks;
61 tr->tr_revokes = revokes;
62 tr->tr_reserved = 1;
Bob Peterson9862ca02017-01-25 12:50:47 -050063 set_bit(TR_ALLOCED, &tr->tr_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000064 if (blocks)
Steven Whitehousef4154ea2006-04-11 14:49:06 -040065 tr->tr_reserved += 6 + blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +000066 if (revokes)
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060067 tr->tr_reserved += gfs2_struct2blk(sdp, revokes);
Steven Whitehoused69a3c62014-02-21 15:22:35 +000068 INIT_LIST_HEAD(&tr->tr_databuf);
69 INIT_LIST_HEAD(&tr->tr_buf);
Bob Petersoncbcc89b2020-06-05 14:12:34 -050070 INIT_LIST_HEAD(&tr->tr_ail1_list);
71 INIT_LIST_HEAD(&tr->tr_ail2_list);
Steven Whitehoused69a3c62014-02-21 15:22:35 +000072
Jan Kara39263d5e2012-06-12 16:20:41 +020073 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000074
David Teiglandb3b94fa2006-01-16 16:50:04 +000075 error = gfs2_log_reserve(sdp, tr->tr_reserved);
76 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050077 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000078
Steven Whitehouse5c676f62006-02-27 17:23:27 -050079 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000080
81 return 0;
82
Benjamin Marzinski24972552014-05-01 22:26:55 -050083fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020084 sb_end_intwrite(sdp->sd_vfs);
Bob Petersonb839dad2019-04-17 12:04:27 -060085 kmem_cache_free(gfs2_trans_cachep, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000086
87 return error;
88}
89
90void gfs2_trans_end(struct gfs2_sbd *sdp)
91{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040092 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010093 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050094 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060095
Steven Whitehouse5c676f62006-02-27 17:23:27 -050096 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000097
Bob Peterson9862ca02017-01-25 12:50:47 -050098 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000099 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600100 if (alloced) {
Bob Petersonb839dad2019-04-17 12:04:27 -0600101 gfs2_trans_free(sdp, tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600102 sb_end_intwrite(sdp->sd_vfs);
103 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104 return;
105 }
106
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100107 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
108 nbuf -= tr->tr_num_buf_rm;
109 nbuf -= tr->tr_num_databuf_rm;
110
111 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
112 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500113 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000114
115 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500116 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
Bob Petersonb839dad2019-04-17 12:04:27 -0600117 gfs2_trans_free(sdp, tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500118 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800120 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500121 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
122 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600123 if (alloced)
124 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000125}
126
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000127static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600128 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000129{
130 struct gfs2_bufdata *bd;
131
132 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
133 bd->bd_bh = bh;
134 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000135 INIT_LIST_HEAD(&bd->bd_list);
136 bh->b_private = bd;
137 return bd;
138}
139
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000141 * gfs2_trans_add_data - Add a databuf to the transaction.
142 * @gl: The inode glock associated with the buffer
143 * @bh: The buffer to add
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500145 * This is used in journaled data mode.
146 * We need to journal the data block in the same way as metadata in
147 * the functions above. The difference is that here we have a tag
148 * which is two __be64's being the block number (as per meta data)
149 * and a flag which says whether the data block needs escaping or
150 * not. This means we need a new log entry for each 251 or so data
151 * blocks, which isn't an enormous overhead but twice as much as
152 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000153 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000154void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
155{
Steven Whitehouse45138992013-01-28 09:30:07 +0000156 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500157 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000158 struct gfs2_bufdata *bd;
159
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600160 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500161 if (buffer_pinned(bh)) {
162 set_bit(TR_TOUCHED, &tr->tr_flags);
163 goto out;
164 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600165 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500166 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000167 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600168 gfs2_log_unlock(sdp);
169 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000170 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600171 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500172 else
173 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600174 lock_buffer(bh);
175 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000177 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500178 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000179 if (list_empty(&bd->bd_list)) {
180 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
181 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
182 gfs2_pin(sdp, bd->bd_bh);
183 tr->tr_num_databuf_new++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000184 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000185 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600186 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500187out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600188 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189}
190
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000191void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
192{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000193
Bob Peterson15562c42015-03-16 11:52:05 -0500194 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000195 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500196 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500197 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500198 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000199
200 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500201 if (buffer_pinned(bh)) {
202 set_bit(TR_TOUCHED, &tr->tr_flags);
203 goto out;
204 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000205 gfs2_log_lock(sdp);
206 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000207 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000208 gfs2_log_unlock(sdp);
209 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000210 lock_page(bh->b_page);
211 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600212 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500213 else
214 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000215 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000216 lock_buffer(bh);
217 gfs2_log_lock(sdp);
218 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000219 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500220 set_bit(TR_TOUCHED, &tr->tr_flags);
221 if (!list_empty(&bd->bd_list))
222 goto out_unlock;
223 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
224 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
225 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
226 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500227 fs_err(sdp, "Attempting to add uninitialised block to "
228 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500229 (unsigned long long)bd->bd_bh->b_blocknr);
230 BUG();
231 }
232 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500233 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500234 gfs2_assert_withdraw(sdp, 0);
235 }
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600236 if (unlikely(gfs2_withdrawn(sdp))) {
237 fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
238 (unsigned long long)bd->bd_bh->b_blocknr);
239 }
Bob Peterson192738b2017-01-25 12:57:42 -0500240 gfs2_pin(sdp, bd->bd_bh);
241 mh->__pad0 = cpu_to_be64(0);
242 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
243 list_add(&bd->bd_list, &tr->tr_buf);
244 tr->tr_num_buf_new++;
245out_unlock:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000246 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500247out:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000248 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000249}
250
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100251void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000252{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000253 struct gfs2_trans *tr = current->journal_info;
254
Bob Petersonc0752aa2012-05-01 12:00:34 -0400255 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500256 gfs2_add_revoke(sdp, bd);
Bob Peterson9862ca02017-01-25 12:50:47 -0500257 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000258 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000259}
260
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100261void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000263 struct gfs2_bufdata *bd, *tmp;
264 struct gfs2_trans *tr = current->journal_info;
265 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266
267 gfs2_log_lock(sdp);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100268 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000269 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400270 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
272 sdp->sd_log_num_revoke--;
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500273 if (bd->bd_gl)
274 gfs2_glock_remove_revoke(bd->bd_gl);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000275 kmem_cache_free(gfs2_bufdata_cachep, bd);
Bob Petersona31b4ec2020-01-20 15:49:28 +0100276 tr->tr_num_revoke_rm++;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000277 if (--n == 0)
278 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 }
280 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000281 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282}
283
Bob Petersonb839dad2019-04-17 12:04:27 -0600284void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
285{
286 if (tr == NULL)
287 return;
288
289 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
290 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
291 gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
292 gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
293 kmem_cache_free(gfs2_trans_cachep, tr);
294}