xref: /linux/fs/gfs2/trans.c (revision e317ffcb7cc26c5e80cab97160a5e2761a4436ec)
1b3b94faaSDavid Teigland /*
2b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3b3b94faaSDavid Teigland  * Copyright (C) 2004-2005 Red Hat, Inc.  All rights reserved.
4b3b94faaSDavid Teigland  *
5b3b94faaSDavid Teigland  * This copyrighted material is made available to anyone wishing to use,
6b3b94faaSDavid Teigland  * modify, copy, or redistribute it subject to the terms and conditions
7b3b94faaSDavid Teigland  * of the GNU General Public License v.2.
8b3b94faaSDavid Teigland  */
9b3b94faaSDavid Teigland 
10b3b94faaSDavid Teigland #include <linux/sched.h>
11b3b94faaSDavid Teigland #include <linux/slab.h>
12b3b94faaSDavid Teigland #include <linux/spinlock.h>
13b3b94faaSDavid Teigland #include <linux/completion.h>
14b3b94faaSDavid Teigland #include <linux/buffer_head.h>
155c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h>
16b3b94faaSDavid Teigland #include <asm/semaphore.h>
17b3b94faaSDavid Teigland 
18b3b94faaSDavid Teigland #include "gfs2.h"
195c676f6dSSteven Whitehouse #include "lm_interface.h"
205c676f6dSSteven Whitehouse #include "incore.h"
21b3b94faaSDavid Teigland #include "glock.h"
22b3b94faaSDavid Teigland #include "log.h"
23b3b94faaSDavid Teigland #include "lops.h"
24b3b94faaSDavid Teigland #include "meta_io.h"
25b3b94faaSDavid Teigland #include "trans.h"
265c676f6dSSteven Whitehouse #include "util.h"
27b3b94faaSDavid Teigland 
28b3b94faaSDavid Teigland int gfs2_trans_begin_i(struct gfs2_sbd *sdp, unsigned int blocks,
29b3b94faaSDavid Teigland 		       unsigned int revokes, char *file, unsigned int line)
30b3b94faaSDavid Teigland {
31b3b94faaSDavid Teigland 	struct gfs2_trans *tr;
32b3b94faaSDavid Teigland 	int error;
33b3b94faaSDavid Teigland 
345c676f6dSSteven Whitehouse 	if (gfs2_assert_warn(sdp, !current->journal_info) ||
35b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blocks || revokes)) {
36b3b94faaSDavid Teigland 		fs_warn(sdp, "(%s, %u)\n", file, line);
37b3b94faaSDavid Teigland 		return -EINVAL;
38b3b94faaSDavid Teigland 	}
39b3b94faaSDavid Teigland 
40f55ab26aSSteven Whitehouse 	tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS);
41b3b94faaSDavid Teigland 	if (!tr)
42b3b94faaSDavid Teigland 		return -ENOMEM;
43b3b94faaSDavid Teigland 
44b3b94faaSDavid Teigland 	tr->tr_file = file;
45b3b94faaSDavid Teigland 	tr->tr_line = line;
46b3b94faaSDavid Teigland 	tr->tr_blocks = blocks;
47b3b94faaSDavid Teigland 	tr->tr_revokes = revokes;
48b3b94faaSDavid Teigland 	tr->tr_reserved = 1;
49b3b94faaSDavid Teigland 	if (blocks)
50b3b94faaSDavid Teigland 		tr->tr_reserved += 1 + blocks;
51b3b94faaSDavid Teigland 	if (revokes)
52b3b94faaSDavid Teigland 		tr->tr_reserved += gfs2_struct2blk(sdp, revokes,
53b3b94faaSDavid Teigland 						   sizeof(uint64_t));
54b3b94faaSDavid Teigland 	INIT_LIST_HEAD(&tr->tr_list_buf);
55b3b94faaSDavid Teigland 
56*e317ffcbSSteven Whitehouse 	gfs2_holder_init(sdp->sd_trans_gl, LM_ST_SHARED,
57*e317ffcbSSteven Whitehouse 			 GL_NEVER_RECURSE, &tr->tr_t_gh);
58b3b94faaSDavid Teigland 
59*e317ffcbSSteven Whitehouse 	error = gfs2_glock_nq(&tr->tr_t_gh);
60b3b94faaSDavid Teigland 	if (error)
61*e317ffcbSSteven Whitehouse 		goto fail_holder_uninit;
62b3b94faaSDavid Teigland 
63b3b94faaSDavid Teigland 	if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
64*e317ffcbSSteven Whitehouse 		tr->tr_t_gh.gh_flags |= GL_NOCACHE;
65b3b94faaSDavid Teigland 		error = -EROFS;
66b3b94faaSDavid Teigland 		goto fail_gunlock;
67b3b94faaSDavid Teigland 	}
68b3b94faaSDavid Teigland 
69b3b94faaSDavid Teigland 	error = gfs2_log_reserve(sdp, tr->tr_reserved);
70b3b94faaSDavid Teigland 	if (error)
71b3b94faaSDavid Teigland 		goto fail_gunlock;
72b3b94faaSDavid Teigland 
735c676f6dSSteven Whitehouse 	current->journal_info = tr;
74b3b94faaSDavid Teigland 
75b3b94faaSDavid Teigland 	return 0;
76b3b94faaSDavid Teigland 
77b3b94faaSDavid Teigland  fail_gunlock:
78*e317ffcbSSteven Whitehouse 	gfs2_glock_dq(&tr->tr_t_gh);
79b3b94faaSDavid Teigland 
80*e317ffcbSSteven Whitehouse  fail_holder_uninit:
81*e317ffcbSSteven Whitehouse 	gfs2_holder_uninit(&tr->tr_t_gh);
82b3b94faaSDavid Teigland 	kfree(tr);
83b3b94faaSDavid Teigland 
84b3b94faaSDavid Teigland 	return error;
85b3b94faaSDavid Teigland }
86b3b94faaSDavid Teigland 
87b3b94faaSDavid Teigland void gfs2_trans_end(struct gfs2_sbd *sdp)
88b3b94faaSDavid Teigland {
89b3b94faaSDavid Teigland 	struct gfs2_trans *tr;
90b3b94faaSDavid Teigland 	struct gfs2_holder *t_gh;
91b3b94faaSDavid Teigland 
925c676f6dSSteven Whitehouse 	tr = current->journal_info;
935c676f6dSSteven Whitehouse 	current->journal_info = NULL;
94b3b94faaSDavid Teigland 
95b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, tr))
96b3b94faaSDavid Teigland 		return;
97b3b94faaSDavid Teigland 
98*e317ffcbSSteven Whitehouse 	t_gh = &tr->tr_t_gh;
99b3b94faaSDavid Teigland 
100b3b94faaSDavid Teigland 	if (!tr->tr_touched) {
101b3b94faaSDavid Teigland 		gfs2_log_release(sdp, tr->tr_reserved);
102b3b94faaSDavid Teigland 
103b3b94faaSDavid Teigland 		gfs2_glock_dq(t_gh);
104*e317ffcbSSteven Whitehouse 		gfs2_holder_uninit(t_gh);
105b3b94faaSDavid Teigland 
106*e317ffcbSSteven Whitehouse 		kfree(tr);
107b3b94faaSDavid Teigland 		return;
108b3b94faaSDavid Teigland 	}
109b3b94faaSDavid Teigland 
110b3b94faaSDavid Teigland 	if (gfs2_assert_withdraw(sdp, tr->tr_num_buf <= tr->tr_blocks))
111b3b94faaSDavid Teigland 		fs_err(sdp, "tr_num_buf = %u, tr_blocks = %u "
112b3b94faaSDavid Teigland 		       "tr_file = %s, tr_line = %u\n",
113b3b94faaSDavid Teigland 		       tr->tr_num_buf, tr->tr_blocks,
114b3b94faaSDavid Teigland 		       tr->tr_file, tr->tr_line);
115b3b94faaSDavid Teigland 	if (gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
116b3b94faaSDavid Teigland 		fs_err(sdp, "tr_num_revoke = %u, tr_revokes = %u "
117b3b94faaSDavid Teigland 		       "tr_file = %s, tr_line = %u\n",
118b3b94faaSDavid Teigland 		       tr->tr_num_revoke, tr->tr_revokes,
119b3b94faaSDavid Teigland 		       tr->tr_file, tr->tr_line);
120b3b94faaSDavid Teigland 
121b3b94faaSDavid Teigland 	gfs2_log_commit(sdp, tr);
122b3b94faaSDavid Teigland 
123b3b94faaSDavid Teigland 	gfs2_glock_dq(t_gh);
124*e317ffcbSSteven Whitehouse 	gfs2_holder_uninit(t_gh);
125b3b94faaSDavid Teigland 
126b3b94faaSDavid Teigland 	if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
127b3b94faaSDavid Teigland 		gfs2_log_flush(sdp);
128b3b94faaSDavid Teigland }
129b3b94faaSDavid Teigland 
130b3b94faaSDavid Teigland void gfs2_trans_add_gl(struct gfs2_glock *gl)
131b3b94faaSDavid Teigland {
132b3b94faaSDavid Teigland 	lops_add(gl->gl_sbd, &gl->gl_le);
133b3b94faaSDavid Teigland }
134b3b94faaSDavid Teigland 
135b3b94faaSDavid Teigland /**
136b3b94faaSDavid Teigland  * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
137b3b94faaSDavid Teigland  * @gl: the glock the buffer belongs to
138b3b94faaSDavid Teigland  * @bh: The buffer to add
139d4e9c4c3SSteven Whitehouse  * @meta: True in the case of adding metadata
140b3b94faaSDavid Teigland  *
141b3b94faaSDavid Teigland  */
142b3b94faaSDavid Teigland 
143d4e9c4c3SSteven Whitehouse void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
144b3b94faaSDavid Teigland {
145b3b94faaSDavid Teigland 	struct gfs2_sbd *sdp = gl->gl_sbd;
146b3b94faaSDavid Teigland 	struct gfs2_bufdata *bd;
147b3b94faaSDavid Teigland 
1485c676f6dSSteven Whitehouse 	bd = bh->b_private;
149b3b94faaSDavid Teigland 	if (bd)
150b3b94faaSDavid Teigland 		gfs2_assert(sdp, bd->bd_gl == gl);
151b3b94faaSDavid Teigland 	else {
152586dfdaaSSteven Whitehouse 		gfs2_attach_bufdata(gl, bh, meta);
1535c676f6dSSteven Whitehouse 		bd = bh->b_private;
154b3b94faaSDavid Teigland 	}
155b3b94faaSDavid Teigland 	lops_add(sdp, &bd->bd_le);
156b3b94faaSDavid Teigland }
157b3b94faaSDavid Teigland 
158b3b94faaSDavid Teigland void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, uint64_t blkno)
159b3b94faaSDavid Teigland {
160b3b94faaSDavid Teigland 	struct gfs2_revoke *rv = kmalloc(sizeof(struct gfs2_revoke),
16118ec7d5cSSteven Whitehouse 					 GFP_NOFS | __GFP_NOFAIL);
162b3b94faaSDavid Teigland 	lops_init_le(&rv->rv_le, &gfs2_revoke_lops);
163b3b94faaSDavid Teigland 	rv->rv_blkno = blkno;
164b3b94faaSDavid Teigland 	lops_add(sdp, &rv->rv_le);
165b3b94faaSDavid Teigland }
166b3b94faaSDavid Teigland 
167b3b94faaSDavid Teigland void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, uint64_t blkno)
168b3b94faaSDavid Teigland {
169b3b94faaSDavid Teigland 	struct gfs2_revoke *rv;
170b3b94faaSDavid Teigland 	int found = 0;
171b3b94faaSDavid Teigland 
172b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
173b3b94faaSDavid Teigland 
174b3b94faaSDavid Teigland 	list_for_each_entry(rv, &sdp->sd_log_le_revoke, rv_le.le_list) {
175b3b94faaSDavid Teigland 		if (rv->rv_blkno == blkno) {
176b3b94faaSDavid Teigland 			list_del(&rv->rv_le.le_list);
177b3b94faaSDavid Teigland 			gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
178b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke--;
179b3b94faaSDavid Teigland 			found = 1;
180b3b94faaSDavid Teigland 			break;
181b3b94faaSDavid Teigland 		}
182b3b94faaSDavid Teigland 	}
183b3b94faaSDavid Teigland 
184b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
185b3b94faaSDavid Teigland 
186b3b94faaSDavid Teigland 	if (found) {
1875c676f6dSSteven Whitehouse 		struct gfs2_trans *tr = current->journal_info;
188b3b94faaSDavid Teigland 		kfree(rv);
1895c676f6dSSteven Whitehouse 		tr->tr_num_revoke_rm++;
190b3b94faaSDavid Teigland 	}
191b3b94faaSDavid Teigland }
192b3b94faaSDavid Teigland 
193b3b94faaSDavid Teigland void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd)
194b3b94faaSDavid Teigland {
195b3b94faaSDavid Teigland 	lops_add(rgd->rd_sbd, &rgd->rd_le);
196b3b94faaSDavid Teigland }
197b3b94faaSDavid Teigland 
198