xref: /linux/fs/gfs2/trans.c (revision b4dc72911d149d7d6b7ffb512bd68906f1cbd33a)
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 
56e317ffcbSSteven Whitehouse 	gfs2_holder_init(sdp->sd_trans_gl, LM_ST_SHARED,
57e317ffcbSSteven Whitehouse 			 GL_NEVER_RECURSE, &tr->tr_t_gh);
58b3b94faaSDavid Teigland 
59e317ffcbSSteven Whitehouse 	error = gfs2_glock_nq(&tr->tr_t_gh);
60b3b94faaSDavid Teigland 	if (error)
61e317ffcbSSteven Whitehouse 		goto fail_holder_uninit;
62b3b94faaSDavid Teigland 
63b3b94faaSDavid Teigland 	if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
64e317ffcbSSteven 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:
78e317ffcbSSteven Whitehouse 	gfs2_glock_dq(&tr->tr_t_gh);
79b3b94faaSDavid Teigland 
80e317ffcbSSteven Whitehouse  fail_holder_uninit:
81e317ffcbSSteven 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 
915c676f6dSSteven Whitehouse 	tr = current->journal_info;
925c676f6dSSteven Whitehouse 	current->journal_info = NULL;
93b3b94faaSDavid Teigland 
94b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, tr))
95b3b94faaSDavid Teigland 		return;
96b3b94faaSDavid Teigland 
97b3b94faaSDavid Teigland 	if (!tr->tr_touched) {
98b3b94faaSDavid Teigland 		gfs2_log_release(sdp, tr->tr_reserved);
99b3b94faaSDavid Teigland 
100*b4dc7291SSteven Whitehouse 		gfs2_glock_dq(&tr->tr_t_gh);
101*b4dc7291SSteven Whitehouse 		gfs2_holder_uninit(&tr->tr_t_gh);
102b3b94faaSDavid Teigland 
103e317ffcbSSteven Whitehouse 		kfree(tr);
104b3b94faaSDavid Teigland 		return;
105b3b94faaSDavid Teigland 	}
106b3b94faaSDavid Teigland 
107b3b94faaSDavid Teigland 	if (gfs2_assert_withdraw(sdp, tr->tr_num_buf <= tr->tr_blocks))
108b3b94faaSDavid Teigland 		fs_err(sdp, "tr_num_buf = %u, tr_blocks = %u "
109b3b94faaSDavid Teigland 		       "tr_file = %s, tr_line = %u\n",
110b3b94faaSDavid Teigland 		       tr->tr_num_buf, tr->tr_blocks,
111b3b94faaSDavid Teigland 		       tr->tr_file, tr->tr_line);
112b3b94faaSDavid Teigland 	if (gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
113b3b94faaSDavid Teigland 		fs_err(sdp, "tr_num_revoke = %u, tr_revokes = %u "
114b3b94faaSDavid Teigland 		       "tr_file = %s, tr_line = %u\n",
115b3b94faaSDavid Teigland 		       tr->tr_num_revoke, tr->tr_revokes,
116b3b94faaSDavid Teigland 		       tr->tr_file, tr->tr_line);
117b3b94faaSDavid Teigland 
118b3b94faaSDavid Teigland 	gfs2_log_commit(sdp, tr);
119b3b94faaSDavid Teigland 
120*b4dc7291SSteven Whitehouse         gfs2_glock_dq(&tr->tr_t_gh);
121*b4dc7291SSteven Whitehouse         gfs2_holder_uninit(&tr->tr_t_gh);
122*b4dc7291SSteven Whitehouse 
123*b4dc7291SSteven Whitehouse         kfree(tr);
124b3b94faaSDavid Teigland 
125b3b94faaSDavid Teigland 	if (sdp->sd_vfs->s_flags & MS_SYNCHRONOUS)
126b3b94faaSDavid Teigland 		gfs2_log_flush(sdp);
127b3b94faaSDavid Teigland }
128b3b94faaSDavid Teigland 
129b3b94faaSDavid Teigland void gfs2_trans_add_gl(struct gfs2_glock *gl)
130b3b94faaSDavid Teigland {
131b3b94faaSDavid Teigland 	lops_add(gl->gl_sbd, &gl->gl_le);
132b3b94faaSDavid Teigland }
133b3b94faaSDavid Teigland 
134b3b94faaSDavid Teigland /**
135b3b94faaSDavid Teigland  * gfs2_trans_add_bh - Add a to-be-modified buffer to the current transaction
136b3b94faaSDavid Teigland  * @gl: the glock the buffer belongs to
137b3b94faaSDavid Teigland  * @bh: The buffer to add
138d4e9c4c3SSteven Whitehouse  * @meta: True in the case of adding metadata
139b3b94faaSDavid Teigland  *
140b3b94faaSDavid Teigland  */
141b3b94faaSDavid Teigland 
142d4e9c4c3SSteven Whitehouse void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta)
143b3b94faaSDavid Teigland {
144b3b94faaSDavid Teigland 	struct gfs2_sbd *sdp = gl->gl_sbd;
145b3b94faaSDavid Teigland 	struct gfs2_bufdata *bd;
146b3b94faaSDavid Teigland 
1475c676f6dSSteven Whitehouse 	bd = bh->b_private;
148b3b94faaSDavid Teigland 	if (bd)
149b3b94faaSDavid Teigland 		gfs2_assert(sdp, bd->bd_gl == gl);
150b3b94faaSDavid Teigland 	else {
151586dfdaaSSteven Whitehouse 		gfs2_attach_bufdata(gl, bh, meta);
1525c676f6dSSteven Whitehouse 		bd = bh->b_private;
153b3b94faaSDavid Teigland 	}
154b3b94faaSDavid Teigland 	lops_add(sdp, &bd->bd_le);
155b3b94faaSDavid Teigland }
156b3b94faaSDavid Teigland 
157b3b94faaSDavid Teigland void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, uint64_t blkno)
158b3b94faaSDavid Teigland {
159b3b94faaSDavid Teigland 	struct gfs2_revoke *rv = kmalloc(sizeof(struct gfs2_revoke),
16018ec7d5cSSteven Whitehouse 					 GFP_NOFS | __GFP_NOFAIL);
161b3b94faaSDavid Teigland 	lops_init_le(&rv->rv_le, &gfs2_revoke_lops);
162b3b94faaSDavid Teigland 	rv->rv_blkno = blkno;
163b3b94faaSDavid Teigland 	lops_add(sdp, &rv->rv_le);
164b3b94faaSDavid Teigland }
165b3b94faaSDavid Teigland 
166b3b94faaSDavid Teigland void gfs2_trans_add_unrevoke(struct gfs2_sbd *sdp, uint64_t blkno)
167b3b94faaSDavid Teigland {
168b3b94faaSDavid Teigland 	struct gfs2_revoke *rv;
169b3b94faaSDavid Teigland 	int found = 0;
170b3b94faaSDavid Teigland 
171b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
172b3b94faaSDavid Teigland 
173b3b94faaSDavid Teigland 	list_for_each_entry(rv, &sdp->sd_log_le_revoke, rv_le.le_list) {
174b3b94faaSDavid Teigland 		if (rv->rv_blkno == blkno) {
175b3b94faaSDavid Teigland 			list_del(&rv->rv_le.le_list);
176b3b94faaSDavid Teigland 			gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
177b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke--;
178b3b94faaSDavid Teigland 			found = 1;
179b3b94faaSDavid Teigland 			break;
180b3b94faaSDavid Teigland 		}
181b3b94faaSDavid Teigland 	}
182b3b94faaSDavid Teigland 
183b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
184b3b94faaSDavid Teigland 
185b3b94faaSDavid Teigland 	if (found) {
1865c676f6dSSteven Whitehouse 		struct gfs2_trans *tr = current->journal_info;
187b3b94faaSDavid Teigland 		kfree(rv);
1885c676f6dSSteven Whitehouse 		tr->tr_num_revoke_rm++;
189b3b94faaSDavid Teigland 	}
190b3b94faaSDavid Teigland }
191b3b94faaSDavid Teigland 
192b3b94faaSDavid Teigland void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd)
193b3b94faaSDavid Teigland {
194b3b94faaSDavid Teigland 	lops_add(rgd->rd_sbd, &rgd->rd_le);
195b3b94faaSDavid Teigland }
196b3b94faaSDavid Teigland 
197