blob: 0546ab4e28e8a3dcbcef555f8574e068b531531c [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
Steven Whitehoused0dc80d2006-03-29 14:36:49 -050047 tr->tr_ip = (unsigned long)__builtin_return_address(0);
David Teiglandb3b94fa2006-01-16 16:50:04 +000048 tr->tr_blocks = blocks;
49 tr->tr_revokes = revokes;
50 tr->tr_reserved = 1;
Benjamin Marzinski24972552014-05-01 22:26:55 -050051 tr->tr_alloced = 1;
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
Steven Whitehousec50b91c2012-04-16 16:40:56 +010077static void gfs2_print_trans(const struct gfs2_trans *tr)
78{
Joe Perchesd77d1b52014-03-06 12:10:45 -080079 pr_warn("Transaction created at: %pSR\n", (void *)tr->tr_ip);
80 pr_warn("blocks=%u revokes=%u reserved=%u touched=%u\n",
81 tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, tr->tr_touched);
82 pr_warn("Buf %u/%u Databuf %u/%u Revoke %u/%u\n",
83 tr->tr_num_buf_new, tr->tr_num_buf_rm,
84 tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
85 tr->tr_num_revoke, tr->tr_num_revoke_rm);
Steven Whitehousec50b91c2012-04-16 16:40:56 +010086}
87
David Teiglandb3b94fa2006-01-16 16:50:04 +000088void gfs2_trans_end(struct gfs2_sbd *sdp)
89{
Steven Whitehousef4154ea2006-04-11 14:49:06 -040090 struct gfs2_trans *tr = current->journal_info;
Steven Whitehousec50b91c2012-04-16 16:40:56 +010091 s64 nbuf;
Steven Whitehousef4154ea2006-04-11 14:49:06 -040092 BUG_ON(!tr);
Steven Whitehouse5c676f62006-02-27 17:23:27 -050093 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000094
David Teiglandb3b94fa2006-01-16 16:50:04 +000095 if (!tr->tr_touched) {
96 gfs2_log_release(sdp, tr->tr_reserved);
Benjamin Marzinski24972552014-05-01 22:26:55 -050097 if (tr->tr_alloced)
Steven Whitehoused8348de2009-02-05 10:12:38 +000098 kfree(tr);
Jan Kara39263d5e2012-06-12 16:20:41 +020099 sb_end_intwrite(sdp->sd_vfs);
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)))
109 gfs2_print_trans(tr);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110
111 gfs2_log_commit(sdp, tr);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500112 if (tr->tr_alloced && !tr->tr_attached)
Benjamin Marzinski16ca9412013-04-05 20:31:46 -0500113 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
116 if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500117 gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
Jan Kara39263d5e2012-06-12 16:20:41 +0200118 sb_end_intwrite(sdp->sd_vfs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000119}
120
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000121static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
122 struct buffer_head *bh,
123 const struct gfs2_log_operations *lops)
124{
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;
130 bd->bd_ops = lops;
131 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 *
Steven Whitehouse767f4332012-12-14 12:52:14 +0000141 * This is used in two distinct cases:
142 * i) In ordered write mode
143 * We put the data buffer on a list so that we can ensure that its
144 * synced to disk at the right time
145 * ii) 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;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000157 struct gfs2_sbd *sdp = gl->gl_sbd;
Steven Whitehouse45138992013-01-28 09:30:07 +0000158 struct address_space *mapping = bh->b_page->mapping;
159 struct gfs2_inode *ip = GFS2_I(mapping->host);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 struct gfs2_bufdata *bd;
161
Steven Whitehouse45138992013-01-28 09:30:07 +0000162 if (!gfs2_is_jdata(ip)) {
163 gfs2_ordered_add_inode(ip);
164 return;
165 }
166
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600167 lock_buffer(bh);
168 gfs2_log_lock(sdp);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500169 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000170 if (bd == NULL) {
Benjamin Marzinski96e5d1d2012-11-07 00:38:06 -0600171 gfs2_log_unlock(sdp);
172 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000173 if (bh->b_private == NULL)
174 bd = gfs2_alloc_bufdata(gl, bh, &gfs2_databuf_lops);
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);
Steven Whitehouse45138992013-01-28 09:30:07 +0000179 tr->tr_touched = 1;
180 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);
188 unlock_buffer(bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000189}
190
Steven Whitehouse767f4332012-12-14 12:52:14 +0000191static void meta_lo_add(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000192{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000193 struct gfs2_meta_header *mh;
194 struct gfs2_trans *tr;
195
196 tr = current->journal_info;
197 tr->tr_touched = 1;
198 if (!list_empty(&bd->bd_list))
199 return;
200 set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
201 set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
202 mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
203 if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
Joe Perchesd77d1b52014-03-06 12:10:45 -0800204 pr_err("Attempting to add uninitialised block to journal (inplace block=%lld)\n",
Steven Whitehouse767f4332012-12-14 12:52:14 +0000205 (unsigned long long)bd->bd_bh->b_blocknr);
206 BUG();
207 }
208 gfs2_pin(sdp, bd->bd_bh);
209 mh->__pad0 = cpu_to_be64(0);
210 mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
Steven Whitehoused69a3c62014-02-21 15:22:35 +0000211 list_add(&bd->bd_list, &tr->tr_buf);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000212 tr->tr_num_buf_new++;
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000213}
214
215void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
216{
Steven Whitehouse767f4332012-12-14 12:52:14 +0000217
218 struct gfs2_sbd *sdp = gl->gl_sbd;
219 struct gfs2_bufdata *bd;
220
221 lock_buffer(bh);
222 gfs2_log_lock(sdp);
223 bd = bh->b_private;
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000224 if (bd == NULL) {
Steven Whitehouse767f4332012-12-14 12:52:14 +0000225 gfs2_log_unlock(sdp);
226 unlock_buffer(bh);
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000227 lock_page(bh->b_page);
228 if (bh->b_private == NULL)
229 bd = gfs2_alloc_bufdata(gl, bh, &gfs2_buf_lops);
230 unlock_page(bh->b_page);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000231 lock_buffer(bh);
232 gfs2_log_lock(sdp);
233 }
Steven Whitehousec76c4d92012-12-14 17:54:21 +0000234 gfs2_assert(sdp, bd->bd_gl == gl);
Steven Whitehouse767f4332012-12-14 12:52:14 +0000235 meta_lo_add(sdp, bd);
236 gfs2_log_unlock(sdp);
237 unlock_buffer(bh);
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000238}
239
Steven Whitehouse1ad38c42007-09-03 11:01:33 +0100240void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241{
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000242 struct gfs2_trans *tr = current->journal_info;
243
Bob Petersonc0752aa2012-05-01 12:00:34 -0400244 BUG_ON(!list_empty(&bd->bd_list));
Benjamin Marzinski5d054962013-06-14 11:38:29 -0500245 gfs2_add_revoke(sdp, bd);
Steven Whitehouse75f2b872012-12-14 12:29:56 +0000246 tr->tr_touched = 1;
247 tr->tr_num_revoke++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000248}
249
Steven Whitehouse5731be52008-02-01 13:16:55 +0000250void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000251{
Steven Whitehouse5731be52008-02-01 13:16:55 +0000252 struct gfs2_bufdata *bd, *tmp;
253 struct gfs2_trans *tr = current->journal_info;
254 unsigned int n = len;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000255
256 gfs2_log_lock(sdp);
Bob Petersonc0752aa2012-05-01 12:00:34 -0400257 list_for_each_entry_safe(bd, tmp, &sdp->sd_log_le_revoke, bd_list) {
Steven Whitehouse5731be52008-02-01 13:16:55 +0000258 if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
Bob Petersonc0752aa2012-05-01 12:00:34 -0400259 list_del_init(&bd->bd_list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000260 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
261 sdp->sd_log_num_revoke--;
Steven Whitehouse5731be52008-02-01 13:16:55 +0000262 kmem_cache_free(gfs2_bufdata_cachep, bd);
263 tr->tr_num_revoke_rm++;
264 if (--n == 0)
265 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000266 }
267 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000268 gfs2_log_unlock(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000269}
270