]>
Commit | Line | Data |
---|---|---|
b3b94faa DT |
1 | /* |
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> | |
5c676f6d | 15 | #include <linux/gfs2_ondisk.h> |
b3b94faa DT |
16 | #include <asm/semaphore.h> |
17 | ||
18 | #include "gfs2.h" | |
5c676f6d SW |
19 | #include "lm_interface.h" |
20 | #include "incore.h" | |
b3b94faa DT |
21 | #include "glock.h" |
22 | #include "log.h" | |
23 | #include "lops.h" | |
24 | #include "meta_io.h" | |
25 | #include "trans.h" | |
5c676f6d | 26 | #include "util.h" |
b3b94faa DT |
27 | |
28 | int 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 | ||
5c676f6d | 34 | if (gfs2_assert_warn(sdp, !current->journal_info) || |
b3b94faa DT |
35 | gfs2_assert_warn(sdp, blocks || revokes)) { |
36 | fs_warn(sdp, "(%s, %u)\n", file, line); | |
37 | return -EINVAL; | |
38 | } | |
39 | ||
f55ab26a | 40 | tr = kzalloc(sizeof(struct gfs2_trans), GFP_NOFS); |
b3b94faa DT |
41 | 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, | |
f55ab26a | 58 | GL_NEVER_RECURSE, GFP_NOFS); |
b3b94faa DT |
59 | 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 | ||
5c676f6d | 76 | current->journal_info = tr; |
b3b94faa DT |
77 | |
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 | ||
92 | void gfs2_trans_end(struct gfs2_sbd *sdp) | |
93 | { | |
94 | struct gfs2_trans *tr; | |
95 | struct gfs2_holder *t_gh; | |
96 | ||
5c676f6d SW |
97 | tr = current->journal_info; |
98 | current->journal_info = NULL; | |
b3b94faa DT |
99 | |
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 | ||
136 | void 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 | |
d4e9c4c3 | 145 | * @meta: True in the case of adding metadata |
b3b94faa DT |
146 | * |
147 | */ | |
148 | ||
d4e9c4c3 | 149 | void gfs2_trans_add_bh(struct gfs2_glock *gl, struct buffer_head *bh, int meta) |
b3b94faa DT |
150 | { |
151 | struct gfs2_sbd *sdp = gl->gl_sbd; | |
152 | struct gfs2_bufdata *bd; | |
153 | ||
5c676f6d | 154 | bd = bh->b_private; |
b3b94faa DT |
155 | if (bd) |
156 | gfs2_assert(sdp, bd->bd_gl == gl); | |
157 | else { | |
586dfdaa | 158 | gfs2_attach_bufdata(gl, bh, meta); |
5c676f6d | 159 | bd = bh->b_private; |
b3b94faa | 160 | } |
b3b94faa DT |
161 | lops_add(sdp, &bd->bd_le); |
162 | } | |
163 | ||
164 | void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, uint64_t blkno) | |
165 | { | |
166 | struct gfs2_revoke *rv = kmalloc(sizeof(struct gfs2_revoke), | |
18ec7d5c | 167 | GFP_NOFS | __GFP_NOFAIL); |
b3b94faa DT |
168 | lops_init_le(&rv->rv_le, &gfs2_revoke_lops); |
169 | rv->rv_blkno = blkno; | |
170 | lops_add(sdp, &rv->rv_le); | |
171 | } | |
172 | ||
173 | void 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) { | |
5c676f6d | 193 | struct gfs2_trans *tr = current->journal_info; |
b3b94faa | 194 | kfree(rv); |
5c676f6d | 195 | tr->tr_num_revoke_rm++; |
b3b94faa DT |
196 | } |
197 | } | |
198 | ||
199 | void gfs2_trans_add_rg(struct gfs2_rgrpd *rgd) | |
200 | { | |
201 | lops_add(rgd->rd_sbd, &rgd->rd_le); | |
202 | } | |
203 |