blob: 9d4227330de470b9f98966b867fa2689c88f17b8 [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)
52 tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
Steven Whitehousecd915492006-09-04 12:49:07 -040053 sizeof(u64));
Steven Whitehoused69a3c62014-02-21 15:22:35 +000054 INIT_LIST_HEAD(&tr->tr_databuf);
55 INIT_LIST_HEAD(&tr->tr_buf);
56
Jan Kara39263d5e2012-06-12 16:20:41 +020057 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000058
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 error = gfs2_log_reserve(sdp, tr->tr_reserved);
60 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050061 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000062
Steven Whitehouse5c676f62006-02-27 17:23:27 -050063 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000064
65 return 0;
66
Benjamin Marzinski24972552014-05-01 22:26:55 -050067fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020068 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000069 kfree(tr);
70
71 return error;
72}
73
Bob Petersone54c78a2018-10-03 08:47:36 -050074static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
Steven Whitehousec50b91c2012-04-16 16:40:56 +010075{
Bob Petersone54c78a2018-10-03 08:47:36 -050076 fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
77 fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
Bob Peterson9862ca02017-01-25 12:50:47 -050078 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
79 test_bit(TR_TOUCHED, &tr->tr_flags));
Bob Petersone9555372019-03-26 13:51:48 -060080 fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -080081 tr->tr_num_buf_new, tr->tr_num_buf_rm,
82 tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
Bob Petersone9555372019-03-26 13:51:48 -060083 tr->tr_num_revoke);
Steven Whitehousec50b91c2012-04-16 16:40:56 +010084}
85
David Teiglandb3b94fa2006-01-16 16:50:04 +000086void gfs2_trans_end(struct gfs2_sbd *sdp)
87{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040088 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010089 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050090 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060091
Steven Whitehouse5c676f62006-02-27 17:23:27 -050092 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000093
Bob Peterson9862ca02017-01-25 12:50:47 -050094 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060096 if (alloced) {
Steven Whitehoused8348de2009-02-05 10:12:38 +000097 kfree(tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060098 sb_end_intwrite(sdp->sd_vfs);
99 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000100 return;
101 }
102
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100103 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
104 nbuf -= tr->tr_num_buf_rm;
105 nbuf -= tr->tr_num_databuf_rm;
106
107 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
108 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500109 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
111 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500112 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
113 kfree(tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500114 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800116 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500117 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
118 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600119 if (alloced)
120 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121}
122
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000123static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600124 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000125{
126 struct gfs2_bufdata *bd;
127
128 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
129 bd->bd_bh = bh;
130 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000131 INIT_LIST_HEAD(&bd->bd_list);
132 bh->b_private = bd;
133 return bd;
134}
135
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000137 * gfs2_trans_add_data - Add a databuf to the transaction.
138 * @gl: The inode glock associated with the buffer
139 * @bh: The buffer to add
David Teiglandb3b94fa2006-01-16 16:50:04 +0000140 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500141 * This is used in journaled data mode.
142 * We need to journal the data block in the same way as metadata in
143 * the functions above. The difference is that here we have a tag
144 * which is two __be64's being the block number (as per meta data)
145 * and a flag which says whether the data block needs escaping or
146 * not. This means we need a new log entry for each 251 or so data
147 * blocks, which isn't an enormous overhead but twice as much as
148 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000149 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000150void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
151{
Steven Whitehouse45138992013-01-28 09:30:07 +0000152 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500153 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 struct gfs2_bufdata *bd;
155
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600156 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500157 if (buffer_pinned(bh)) {
158 set_bit(TR_TOUCHED, &tr->tr_flags);
159 goto out;
160 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600161 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500162 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000163 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600164 gfs2_log_unlock(sdp);
165 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000166 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600167 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500168 else
169 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600170 lock_buffer(bh);
171 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000173 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500174 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000175 if (list_empty(&bd->bd_list)) {
176 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
177 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
178 gfs2_pin(sdp, bd->bd_bh);
179 tr->tr_num_databuf_new++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000180 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000181 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600182 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500183out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600184 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185}
186
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000187void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
188{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000189
Bob Peterson15562c42015-03-16 11:52:05 -0500190 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000191 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500192 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500193 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500194 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000195
196 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500197 if (buffer_pinned(bh)) {
198 set_bit(TR_TOUCHED, &tr->tr_flags);
199 goto out;
200 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000201 gfs2_log_lock(sdp);
202 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000203 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000204 gfs2_log_unlock(sdp);
205 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000206 lock_page(bh->b_page);
207 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600208 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500209 else
210 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000211 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000212 lock_buffer(bh);
213 gfs2_log_lock(sdp);
214 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000215 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500216 set_bit(TR_TOUCHED, &tr->tr_flags);
217 if (!list_empty(&bd->bd_list))
218 goto out_unlock;
219 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
220 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
221 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
222 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500223 fs_err(sdp, "Attempting to add uninitialised block to "
224 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500225 (unsigned long long)bd->bd_bh->b_blocknr);
226 BUG();
227 }
228 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500229 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500230 gfs2_assert_withdraw(sdp, 0);
231 }
232 gfs2_pin(sdp, bd->bd_bh);
233 mh->__pad0 = cpu_to_be64(0);
234 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
235 list_add(&bd->bd_list, &tr->tr_buf);
236 tr->tr_num_buf_new++;
237out_unlock:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000238 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500239out:
Steven Whitehouse767f4332012-12-14 12:52:14 +0000240 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000241}
242
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100243void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000244{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000245 struct gfs2_trans *tr = current->journal_info;
246
Bob Petersonc0752aa2012-05-01 12:00:34 -0400247 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500248 gfs2_add_revoke(sdp, bd);
Bob Peterson9862ca02017-01-25 12:50:47 -0500249 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000250 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251}
252
Andreas Gruenbacherfbb27872019-04-05 12:18:23 +0100253void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000254{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000255 struct gfs2_bufdata *bd, *tmp;
256 struct gfs2_trans *tr = current->journal_info;
257 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258
259 gfs2_log_lock(sdp);
Andreas Gruenbachera5b1d3f2019-04-05 12:16:14 +0100260 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000261 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400262 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
264 sdp->sd_log_num_revoke--;
Bob Petersonfe5e7ba2019-11-14 09:49:11 -0500265 if (bd->bd_gl)
266 gfs2_glock_remove_revoke(bd->bd_gl);
Steven Whitehouse5731be52008-02-01 13:16:55 +0000267 kmem_cache_free(gfs2_bufdata_cachep, bd);
Bob Petersone9555372019-03-26 13:51:48 -0600268 tr->tr_num_revoke--;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000269 if (--n == 0)
270 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000271 }
272 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000273 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274}
275