blob: d328da7cde36d2d460555b8fb9ea54fd2be11030 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Steven Whitehouse3a8a9a12006-05-18 15:09:15 -04003 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00004 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
Steven Whitehousee9fc2aa2006-09-01 11:05:15 -04007 * of the GNU General Public License version 2.
David Teiglandb3b94fa2006-01-16 16:50:04 +00008 */
9
Joe Perchesd77d1b52014-03-06 12:10:45 -080010#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
David Teiglandb3b94fa2006-01-16 16:50:04 +000012#include <linux/sched.h>
13#include <linux/slab.h>
14#include <linux/spinlock.h>
15#include <linux/completion.h>
16#include <linux/buffer_head.h>
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050017#include <linux/kallsyms.h>
Steven Whitehousef057f6c2009-01-12 10:43:39 +000018#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000019
20#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050021#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000022#include "glock.h"
Steven Whitehouse767f4332012-12-14 12:52:14 +000023#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000024#include "log.h"
25#include "lops.h"
26#include "meta_io.h"
27#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050028#include "util.h"
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -050029#include "trace_gfs2.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050031int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
32 unsigned int revokes)
David Teiglandb3b94fa2006-01-16 16:50:04 +000033{
34 struct gfs2_trans *tr;
35 int error;
36
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050037 BUG_ON(current->journal_info);
38 BUG_ON(blocks == 0 && revokes == 0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000039
Steven Whitehousea1c06432009-05-13 10:56:52 +010040 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
41 return -EROFS;
42
Steven Whitehousef55ab262006-02-21 12:51:39 +000043 tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000044 if (!tr)
45 return -ENOMEM;
46
Fabian Frederickd29c0af2014-10-03 20:15:36 +020047 tr->tr_ip = _RET_IP_;
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 tr->tr_blocks = blocks;
49 tr->tr_revokes = revokes;
50 tr->tr_reserved = 1;
Bob Peterson9862ca02017-01-25 12:50:47 -050051 set_bit(TR_ALLOCED, &tr->tr_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +000052 if (blocks)
Steven Whitehousef4154ea2006-04-11 14:49:06 -040053 tr->tr_reserved += 6 + blocks;
David Teiglandb3b94fa2006-01-16 16:50:04 +000054 if (revokes)
55 tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
Steven Whitehousecd915492006-09-04 12:49:07 -040056 sizeof(u64));
Steven Whitehoused69a3c62014-02-21 15:22:35 +000057 INIT_LIST_HEAD(&tr->tr_databuf);
58 INIT_LIST_HEAD(&tr->tr_buf);
59
Jan Kara39263d5e2012-06-12 16:20:41 +020060 sb_start_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000061
David Teiglandb3b94fa2006-01-16 16:50:04 +000062 error = gfs2_log_reserve(sdp, tr->tr_reserved);
63 if (error)
Benjamin Marzinski24972552014-05-01 22:26:55 -050064 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +000065
Steven Whitehouse5c676f62006-02-27 17:23:27 -050066 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000067
68 return 0;
69
Benjamin Marzinski24972552014-05-01 22:26:55 -050070fail:
Jan Kara39263d5e2012-06-12 16:20:41 +020071 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +000072 kfree(tr);
73
74 return error;
75}
76
Bob Petersone54c78a2018-10-03 08:47:36 -050077static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
Steven Whitehousec50b91c2012-04-16 16:40:56 +010078{
Bob Petersone54c78a2018-10-03 08:47:36 -050079 fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
80 fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
Bob Peterson9862ca02017-01-25 12:50:47 -050081 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
82 test_bit(TR_TOUCHED, &tr->tr_flags));
Bob Petersone54c78a2018-10-03 08:47:36 -050083 fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
Joe Perchesd77d1b52014-03-06 12:10:45 -080084 tr->tr_num_buf_new, tr->tr_num_buf_rm,
85 tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
86 tr->tr_num_revoke, tr->tr_num_revoke_rm);
Steven Whitehousec50b91c2012-04-16 16:40:56 +010087}
88
David Teiglandb3b94fa2006-01-16 16:50:04 +000089void gfs2_trans_end(struct gfs2_sbd *sdp)
90{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040091 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010092 s64 nbuf;
Bob Peterson9862ca02017-01-25 12:50:47 -050093 int alloced = test_bit(TR_ALLOCED, &tr->tr_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060094
Steven Whitehouse5c676f62006-02-27 17:23:27 -050095 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000096
Bob Peterson9862ca02017-01-25 12:50:47 -050097 if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +000098 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060099 if (alloced) {
Steven Whitehoused8348de2009-02-05 10:12:38 +0000100 kfree(tr);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600101 sb_end_intwrite(sdp->sd_vfs);
102 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103 return;
104 }
105
Steven Whitehousec50b91c2012-04-16 16:40:56 +0100106 nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
107 nbuf -= tr->tr_num_buf_rm;
108 nbuf -= tr->tr_num_databuf_rm;
109
110 if (gfs2_assert_withdraw(sdp, (nbuf <= tr->tr_blocks) &&
111 (tr->tr_num_revoke <= tr->tr_revokes)))
Bob Petersone54c78a2018-10-03 08:47:36 -0500112 gfs2_print_trans(sdp, tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000113
114 gfs2_log_commit(sdp, tr);
Bob Peterson9862ca02017-01-25 12:50:47 -0500115 if (alloced && !test_bit(TR_ATTACHED, &tr->tr_flags))
116 kfree(tr);
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500117 up_read(&sdp->sd_log_flush_lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118
Linus Torvalds1751e8a2017-11-27 13:05:09 -0800119 if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
Bob Peterson805c09072018-01-08 10:34:17 -0500120 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
121 GFS2_LFC_TRANS_END);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600122 if (alloced)
123 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124}
125
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000126static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600127 struct buffer_head *bh)
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000128{
129 struct gfs2_bufdata *bd;
130
131 bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
132 bd->bd_bh = bh;
133 bd->bd_gl = gl;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000134 INIT_LIST_HEAD(&bd->bd_list);
135 bh->b_private = bd;
136 return bd;
137}
138
David Teiglandb3b94fa2006-01-16 16:50:04 +0000139/**
Steven Whitehouse45138992013-01-28 09:30:07 +0000140 * gfs2_trans_add_data - Add a databuf to the transaction.
141 * @gl: The inode glock associated with the buffer
142 * @bh: The buffer to add
David Teiglandb3b94fa2006-01-16 16:50:04 +0000143 *
Andreas Gruenbacher845802b2018-06-04 07:50:16 -0500144 * This is used in journaled data mode.
145 * We need to journal the data block in the same way as metadata in
146 * the functions above. The difference is that here we have a tag
147 * which is two __be64's being the block number (as per meta data)
148 * and a flag which says whether the data block needs escaping or
149 * not. This means we need a new log entry for each 251 or so data
150 * blocks, which isn't an enormous overhead but twice as much as
151 * for normal metadata blocks.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152 */
Steven Whitehouse767f4332012-12-14 12:52:14 +0000153void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
154{
Steven Whitehouse45138992013-01-28 09:30:07 +0000155 struct gfs2_trans *tr = current->journal_info;
Bob Peterson15562c42015-03-16 11:52:05 -0500156 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 struct gfs2_bufdata *bd;
158
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600159 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500160 if (buffer_pinned(bh)) {
161 set_bit(TR_TOUCHED, &tr->tr_flags);
162 goto out;
163 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600164 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500165 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000166 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600167 gfs2_log_unlock(sdp);
168 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000169 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600170 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500171 else
172 bd = bh->b_private;
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600173 lock_buffer(bh);
174 gfs2_log_lock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000175 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000176 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson9862ca02017-01-25 12:50:47 -0500177 set_bit(TR_TOUCHED, &tr->tr_flags);
Steven Whitehouse45138992013-01-28 09:30:07 +0000178 if (list_empty(&bd->bd_list)) {
179 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
180 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
181 gfs2_pin(sdp, bd->bd_bh);
182 tr->tr_num_databuf_new++;
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000183 list_add_tail(&bd->bd_list, &tr->tr_databuf);
Steven Whitehouse45138992013-01-28 09:30:07 +0000184 }
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600185 gfs2_log_unlock(sdp);
Bob Petersonaacee722017-01-30 11:51:21 -0500186out:
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600187 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188}
189
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000190void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
191{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000192
Bob Peterson15562c42015-03-16 11:52:05 -0500193 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse767f4332012-12-14 12:52:14 +0000194 struct gfs2_bufdata *bd;
Bob Peterson192738b2017-01-25 12:57:42 -0500195 struct gfs2_meta_header *mh;
Bob Petersonaacee722017-01-30 11:51:21 -0500196 struct gfs2_trans *tr = current->journal_info;
Bob Peterson192738b2017-01-25 12:57:42 -0500197 enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000198
199 lock_buffer(bh);
Bob Petersonaacee722017-01-30 11:51:21 -0500200 if (buffer_pinned(bh)) {
201 set_bit(TR_TOUCHED, &tr->tr_flags);
202 goto out;
203 }
Steven Whitehouse767f4332012-12-14 12:52:14 +0000204 gfs2_log_lock(sdp);
205 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000206 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000207 gfs2_log_unlock(sdp);
208 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000209 lock_page(bh->b_page);
210 if (bh->b_private == NULL)
Bob Petersoncbbe76c2018-11-16 14:18:32 -0600211 bd = gfs2_alloc_bufdata(gl, bh);
Bob Peterson491e94f2015-10-01 11:47:31 -0500212 else
213 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000214 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000215 lock_buffer(bh);
216 gfs2_log_lock(sdp);
217 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000218 gfs2_assert(sdp, bd->bd_gl == gl);
Bob Peterson192738b2017-01-25 12:57:42 -0500219 set_bit(TR_TOUCHED, &tr->tr_flags);
220 if (!list_empty(&bd->bd_list))
221 goto out_unlock;
222 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
223 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
224 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
225 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500226 fs_err(sdp, "Attempting to add uninitialised block to "
227 "journal (inplace block=%lld)\n",
Bob Peterson192738b2017-01-25 12:57:42 -0500228 (unsigned long long)bd->bd_bh->b_blocknr);
229 BUG();
230 }
231 if (unlikely(state == SFS_FROZEN)) {
Bob Petersone54c78a2018-10-03 08:47:36 -0500232 fs_info(sdp, "GFS2:adding buf while frozen\n");
Bob Peterson192738b2017-01-25 12:57:42 -0500233 gfs2_assert_withdraw(sdp, 0);
234 }
235 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--;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000268 kmem_cache_free(gfs2_bufdata_cachep, bd);
269 tr->tr_num_revoke_rm++;
270 if (--n == 0)
271 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000272 }
273 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000274 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275}
276