blob: 2cce68aec134ff9db97c3d0955fe241766e07684 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050015#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000016#include <asm/semaphore.h>
17
18#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050019#include "lm_interface.h"
20#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000021#include "glock.h"
22#include "log.h"
23#include "lops.h"
24#include "meta_io.h"
25#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050026#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000027
28int gfs2_trans_begin_i(struct gfs2_sbd *sdp, unsigned int blocks,
29 unsigned int revokes, char *file, unsigned int line)
30{
31 struct gfs2_trans *tr;
32 int error;
33
Steven Whitehouse5c676f62006-02-27 17:23:27 -050034 if (gfs2_assert_warn(sdp, !current->journal_info) ||
David Teiglandb3b94fa2006-01-16 16:50:04 +000035 gfs2_assert_warn(sdp, blocks || revokes)) {
36 fs_warn(sdp, "(%s, %u)\n", file, line);
37 return -EINVAL;
38 }
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
44 tr->tr_file = file;
45 tr->tr_line = line;
46 tr->tr_blocks = blocks;
47 tr->tr_revokes = revokes;
48 tr->tr_reserved = 1;
49 if (blocks)
50 tr->tr_reserved += 1 + blocks;
51 if (revokes)
52 tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
53 sizeof(uint64_t));
54 INIT_LIST_HEAD(&tr->tr_list_buf);
55
56 error = -ENOMEM;
57 tr->tr_t_gh = gfs2_holder_get(sdp->sd_trans_gl, LM_ST_SHARED,
Steven Whitehousef55ab262006-02-21 12:51:39 +000058 GL_NEVER_RECURSE, GFP_NOFS);
David Teiglandb3b94fa2006-01-16 16:50:04 +000059 if (!tr->tr_t_gh)
60 goto fail;
61
62 error = gfs2_glock_nq(tr->tr_t_gh);
63 if (error)
64 goto fail_holder_put;
65
66 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
67 tr->tr_t_gh->gh_flags |= GL_NOCACHE;
68 error = -EROFS;
69 goto fail_gunlock;
70 }
71
72 error = gfs2_log_reserve(sdp, tr->tr_reserved);
73 if (error)
74 goto fail_gunlock;
75
Steven Whitehouse5c676f62006-02-27 17:23:27 -050076 current->journal_info = tr;
David Teiglandb3b94fa2006-01-16 16:50:04 +000077
78 return 0;
79
80 fail_gunlock:
81 gfs2_glock_dq(tr->tr_t_gh);
82
83 fail_holder_put:
84 gfs2_holder_put(tr->tr_t_gh);
85
86 fail:
87 kfree(tr);
88
89 return error;
90}
91
92void gfs2_trans_end(struct gfs2_sbd *sdp)
93{
94 struct gfs2_trans *tr;
95 struct gfs2_holder *t_gh;
96
Steven Whitehouse5c676f62006-02-27 17:23:27 -050097 tr = current->journal_info;
98 current->journal_info = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000099
100 if (gfs2_assert_warn(sdp, tr))
101 return;
102
103 t_gh = tr->tr_t_gh;
104 tr->tr_t_gh = NULL;
105
106 if (!tr->tr_touched) {
107 gfs2_log_release(sdp, tr->tr_reserved);
108 kfree(tr);
109
110 gfs2_glock_dq(t_gh);
111 gfs2_holder_put(t_gh);
112
113 return;
114 }
115
116 if (gfs2_assert_withdraw(sdp, tr->tr_num_buf <= tr->tr_blocks))
117 fs_err(sdp, "tr_num_buf = %u, tr_blocks = %u "
118 "tr_file = %s, tr_line = %u\n",
119 tr->tr_num_buf, tr->tr_blocks,
120 tr->tr_file, tr->tr_line);
121 if (gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
122 fs_err(sdp, "tr_num_revoke = %u, tr_revokes = %u "
123 "tr_file = %s, tr_line = %u\n",
124 tr->tr_num_revoke, tr->tr_revokes,
125 tr->tr_file, tr->tr_line);
126
127 gfs2_log_commit(sdp, tr);
128
129 gfs2_glock_dq(t_gh);
130 gfs2_holder_put(t_gh);
131
132 if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
133 gfs2_log_flush(sdp);
134}
135
136void gfs2_trans_add_gl(struct gfs2_glock *gl)
137{
138 lops_add(gl->gl_sbd, &gl->gl_le);
139}
140
141/**
142 * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
143 * @gl: the glock the buffer belongs to
144 * @bh: The buffer to add
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000145 * @meta: True in the case of adding metadata
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146 *
147 */
148
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000149void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150{
151 struct gfs2_sbd *sdp = gl->gl_sbd;
152 struct gfs2_bufdata *bd;
153
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500154 bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000155 if (bd)
156 gfs2_assert(sdp, bd->bd_gl == gl);
157 else {
Steven Whitehouse586dfda2006-01-18 11:32:00 +0000158 gfs2_attach_bufdata(gl, bh, meta);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500159 bd = bh->b_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 lops_add(sdp, &bd->bd_le);
162}
163
164void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, uint64_t blkno)
165{
166 struct gfs2_revoke *rv = kmalloc(sizeof(struct gfs2_revoke),
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000167 GFP_NOFS | __GFP_NOFAIL);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000168 lops_init_le(&rv->rv_le, &gfs2_revoke_lops);
169 rv->rv_blkno = blkno;
170 lops_add(sdp, &rv->rv_le);
171}
172
173void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, uint64_t blkno)
174{
175 struct gfs2_revoke *rv;
176 int found = 0;
177
178 gfs2_log_lock(sdp);
179
180 list_for_each_entry(rv, &sdp->sd_log_le_revoke, rv_le.le_list) {
181 if (rv->rv_blkno == blkno) {
182 list_del(&rv->rv_le.le_list);
183 gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
184 sdp->sd_log_num_revoke--;
185 found = 1;
186 break;
187 }
188 }
189
190 gfs2_log_unlock(sdp);
191
192 if (found) {
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500193 struct gfs2_trans *tr = current->journal_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000194 kfree(rv);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500195 tr->tr_num_revoke_rm++;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000196 }
197}
198
199void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd)
200{
201 lops_add(rgd->rd_sbd, &rgd->rd_le);
202}
203