blob: 6d4bf7ea7b3be66d3fcdcb51dd0722d5d35826b3 [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 Peterson462582b2020-08-21 08:50:34 -050070 INIT_LIST_HEAD(&tr->tr_list);
Bob Petersoncbcc89b2020-06-05 14:12:34 -050071 INIT_LIST_HEAD(&tr->tr_ail1_list);
72 INIT_LIST_HEAD(&tr->tr_ail2_list);
Steven Whitehoused69a3c62014-02-21 15:22:35 +000073
Jan Kara39263d5e2012-06-12 16:20:41 +020074 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000075
David Teiglandb3b94fa2006-01-16 16:50:04 +000076 error = gfs2_log_reserve(sdp, tr->tr_reserved);
77 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050078 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000079
Steven Whitehouse5c676f62006-02-27 17:23:27 -050080 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000081
82 return 0;
83
Benjamin Marzinski24972552014-05-01 22:26:55 -050084fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020085 sb_end_intwrite(sdp->sd_vfs);
Bob Petersonb839dad2019-04-17 12:04:27 -060086 kmem_cache_free(gfs2_trans_cachep, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +000087
88 return error;
89}
90
91void gfs2_trans_end(struct gfs2_sbd *sdp)
92{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040093 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010094 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050095 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060096
Steven Whitehouse5c676f62006-02-27 17:23:27 -050097 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000098
Bob Peterson9862ca02017-01-25 12:50:47 -050099 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600101 if (alloced) {
Bob Petersonb839dad2019-04-17 12:04:27 -0600102 gfs2_trans_free(sdp, tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600103 sb_end_intwrite(sdp->sd_vfs);
104 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000105 return;
106 }
107
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100108 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
109 nbuf -= tr->tr_num_buf_rm;
110 nbuf -= tr->tr_num_databuf_rm;
111
112 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
113 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500114 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
116 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500117 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
Bob Petersonb839dad2019-04-17 12:04:27 -0600118 gfs2_trans_free(sdp, tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500119 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800121 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500122 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
123 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600124 if (alloced)
125 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000126}
127
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000128static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600129 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000130{
131 struct gfs2_bufdata *bd;
132
133 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
134 bd->bd_bh = bh;
135 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000136 INIT_LIST_HEAD(&bd->bd_list);
137 bh->b_private = bd;
138 return bd;
139}
140
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000142 * gfs2_trans_add_data - Add a databuf to the transaction.
143 * @gl: The inode glock associated with the buffer
144 * @bh: The buffer to add
David Teiglandb3b94fa2006-01-16 16:50:04 +0000145 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500146 * This is used in journaled data mode.
147 * We need to journal the data block in the same way as metadata in
148 * the functions above. The difference is that here we have a tag
149 * which is two __be64's being the block number (as per meta data)
150 * and a flag which says whether the data block needs escaping or
151 * not. This means we need a new log entry for each 251 or so data
152 * blocks, which isn't an enormous overhead but twice as much as
153 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000155void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
156{
Steven Whitehouse45138992013-01-28 09:30:07 +0000157 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500158 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000159 struct gfs2_bufdata *bd;
160
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600161 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500162 if (buffer_pinned(bh)) {
163 set_bit(TR_TOUCHED, &tr->tr_flags);
164 goto out;
165 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600166 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500167 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000168 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600169 gfs2_log_unlock(sdp);
170 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000171 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600172 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500173 else
174 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600175 lock_buffer(bh);
176 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000178 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500179 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000180 if (list_empty(&bd->bd_list)) {
181 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
182 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
183 gfs2_pin(sdp, bd->bd_bh);
184 tr->tr_num_databuf_new++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000185 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000186 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600187 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500188out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600189 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190}
191
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000192void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
193{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000194
Bob Peterson15562c42015-03-16 11:52:05 -0500195 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000196 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500197 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500198 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500199 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000200
201 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500202 if (buffer_pinned(bh)) {
203 set_bit(TR_TOUCHED, &tr->tr_flags);
204 goto out;
205 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000206 gfs2_log_lock(sdp);
207 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000208 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000209 gfs2_log_unlock(sdp);
210 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000211 lock_page(bh->b_page);
212 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600213 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500214 else
215 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000216 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000217 lock_buffer(bh);
218 gfs2_log_lock(sdp);
219 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000220 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500221 set_bit(TR_TOUCHED, &tr->tr_flags);
222 if (!list_empty(&bd->bd_list))
223 goto out_unlock;
224 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
225 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
226 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
227 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500228 fs_err(sdp, "Attempting to add uninitialised block to "
229 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500230 (unsigned long long)bd->bd_bh->b_blocknr);
231 BUG();
232 }
233 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500234 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500235 gfs2_assert_withdraw(sdp, 0);
236 }
Bob Peterson2ca0c2f2019-11-13 13:58:30 -0600237 if (unlikely(gfs2_withdrawn(sdp))) {
238 fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
239 (unsigned long long)bd->bd_bh->b_blocknr);
240 }
Bob Peterson192738b2017-01-25 12:57:42 -0500241 gfs2_pin(sdp, bd->bd_bh);
242 mh->__pad0 = cpu_to_be64(0);
243 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
244 list_add(&bd->bd_list, &tr->tr_buf);
245 tr->tr_num_buf_new++;
246out_unlock:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000247 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500248out:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000249 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000250}
251
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100252void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000253{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000254 struct gfs2_trans *tr = current->journal_info;
255
Bob Petersonc0752aa2012-05-01 12:00:34 -0400256 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500257 gfs2_add_revoke(sdp, bd);
Bob Peterson9862ca02017-01-25 12:50:47 -0500258 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000259 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260}
261
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100262void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000264 struct gfs2_bufdata *bd, *tmp;
265 struct gfs2_trans *tr = current->journal_info;
266 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000267
268 gfs2_log_lock(sdp);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100269 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000270 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400271 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
273 sdp->sd_log_num_revoke--;
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500274 if (bd->bd_gl)
275 gfs2_glock_remove_revoke(bd->bd_gl);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000276 kmem_cache_free(gfs2_bufdata_cachep, bd);
Bob Petersona31b4ec2020-01-20 15:49:28 +0100277 tr->tr_num_revoke_rm++;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000278 if (--n == 0)
279 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 }
281 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283}
284
Bob Petersonb839dad2019-04-17 12:04:27 -0600285void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
286{
287 if (tr == NULL)
288 return;
289
290 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
291 gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
292 gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
293 gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
294 kmem_cache_free(gfs2_trans_cachep, tr);
295}