xref: /linux/fs/gfs2/trans.c (revision 1b0975ee3bdd3eb19a47371c26fd7ef8f7f6b599)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5  */
6 
7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8 
9 #include <linux/sched.h>
10 #include <linux/slab.h>
11 #include <linux/spinlock.h>
12 #include <linux/completion.h>
13 #include <linux/buffer_head.h>
14 #include <linux/kallsyms.h>
15 #include <linux/gfs2_ondisk.h>
16 
17 #include "gfs2.h"
18 #include "incore.h"
19 #include "glock.h"
20 #include "inode.h"
21 #include "log.h"
22 #include "lops.h"
23 #include "meta_io.h"
24 #include "trans.h"
25 #include "util.h"
26 #include "trace_gfs2.h"
27 
28 static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr)
29 {
30 	fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip);
31 	fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n",
32 		tr->tr_blocks, tr->tr_revokes, tr->tr_reserved,
33 		test_bit(TR_TOUCHED, &tr->tr_flags));
34 	fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u\n",
35 		tr->tr_num_buf_new, tr->tr_num_buf_rm,
36 		tr->tr_num_databuf_new, tr->tr_num_databuf_rm,
37 		tr->tr_num_revoke);
38 }
39 
40 int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp,
41 		       unsigned int blocks, unsigned int revokes,
42 		       unsigned long ip)
43 {
44 	unsigned int extra_revokes;
45 
46 	if (current->journal_info) {
47 		gfs2_print_trans(sdp, current->journal_info);
48 		BUG();
49 	}
50 	BUG_ON(blocks == 0 && revokes == 0);
51 
52 	if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
53 		return -EROFS;
54 
55 	tr->tr_ip = ip;
56 	tr->tr_blocks = blocks;
57 	tr->tr_revokes = revokes;
58 	tr->tr_reserved = GFS2_LOG_FLUSH_MIN_BLOCKS;
59 	if (blocks) {
60 		/*
61 		 * The reserved blocks are either used for data or metadata.
62 		 * We can have mixed data and metadata, each with its own log
63 		 * descriptor block; see calc_reserved().
64 		 */
65 		tr->tr_reserved += blocks + 1 + DIV_ROUND_UP(blocks - 1, databuf_limit(sdp));
66 	}
67 	INIT_LIST_HEAD(&tr->tr_databuf);
68 	INIT_LIST_HEAD(&tr->tr_buf);
69 	INIT_LIST_HEAD(&tr->tr_list);
70 	INIT_LIST_HEAD(&tr->tr_ail1_list);
71 	INIT_LIST_HEAD(&tr->tr_ail2_list);
72 
73 	if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks))
74 		return -EINVAL;
75 
76 	sb_start_intwrite(sdp->sd_vfs);
77 
78 	/*
79 	 * Try the reservations under sd_log_flush_lock to prevent log flushes
80 	 * from creating inconsistencies between the number of allocated and
81 	 * reserved revokes.  If that fails, do a full-block allocation outside
82 	 * of the lock to avoid stalling log flushes.  Then, allot the
83 	 * appropriate number of blocks to revokes, use as many revokes locally
84 	 * as needed, and "release" the surplus into the revokes pool.
85 	 */
86 
87 	down_read(&sdp->sd_log_flush_lock);
88 	if (gfs2_log_try_reserve(sdp, tr, &extra_revokes))
89 		goto reserved;
90 	up_read(&sdp->sd_log_flush_lock);
91 	gfs2_log_reserve(sdp, tr, &extra_revokes);
92 	down_read(&sdp->sd_log_flush_lock);
93 
94 reserved:
95 	gfs2_log_release_revokes(sdp, extra_revokes);
96 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
97 		gfs2_log_release_revokes(sdp, tr->tr_revokes);
98 		up_read(&sdp->sd_log_flush_lock);
99 		gfs2_log_release(sdp, tr->tr_reserved);
100 		sb_end_intwrite(sdp->sd_vfs);
101 		return -EROFS;
102 	}
103 
104 	current->journal_info = tr;
105 
106 	return 0;
107 }
108 
109 int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks,
110 		     unsigned int revokes)
111 {
112 	struct gfs2_trans *tr;
113 	int error;
114 
115 	tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS);
116 	if (!tr)
117 		return -ENOMEM;
118 	error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_);
119 	if (error)
120 		kmem_cache_free(gfs2_trans_cachep, tr);
121 	return error;
122 }
123 
124 void gfs2_trans_end(struct gfs2_sbd *sdp)
125 {
126 	struct gfs2_trans *tr = current->journal_info;
127 	s64 nbuf;
128 
129 	current->journal_info = NULL;
130 
131 	if (!test_bit(TR_TOUCHED, &tr->tr_flags)) {
132 		gfs2_log_release_revokes(sdp, tr->tr_revokes);
133 		up_read(&sdp->sd_log_flush_lock);
134 		gfs2_log_release(sdp, tr->tr_reserved);
135 		if (!test_bit(TR_ONSTACK, &tr->tr_flags))
136 			gfs2_trans_free(sdp, tr);
137 		sb_end_intwrite(sdp->sd_vfs);
138 		return;
139 	}
140 
141 	gfs2_log_release_revokes(sdp, tr->tr_revokes - tr->tr_num_revoke);
142 
143 	nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new;
144 	nbuf -= tr->tr_num_buf_rm;
145 	nbuf -= tr->tr_num_databuf_rm;
146 
147 	if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) ||
148 	    gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes))
149 		gfs2_print_trans(sdp, tr);
150 
151 	gfs2_log_commit(sdp, tr);
152 	if (!test_bit(TR_ONSTACK, &tr->tr_flags) &&
153 	    !test_bit(TR_ATTACHED, &tr->tr_flags))
154 		gfs2_trans_free(sdp, tr);
155 	up_read(&sdp->sd_log_flush_lock);
156 
157 	if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS)
158 		gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
159 			       GFS2_LFC_TRANS_END);
160 	sb_end_intwrite(sdp->sd_vfs);
161 }
162 
163 static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl,
164 					       struct buffer_head *bh)
165 {
166 	struct gfs2_bufdata *bd;
167 
168 	bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL);
169 	bd->bd_bh = bh;
170 	bd->bd_gl = gl;
171 	INIT_LIST_HEAD(&bd->bd_list);
172 	INIT_LIST_HEAD(&bd->bd_ail_st_list);
173 	INIT_LIST_HEAD(&bd->bd_ail_gl_list);
174 	bh->b_private = bd;
175 	return bd;
176 }
177 
178 /**
179  * gfs2_trans_add_data - Add a databuf to the transaction.
180  * @gl: The inode glock associated with the buffer
181  * @bh: The buffer to add
182  *
183  * This is used in journaled data mode.
184  * We need to journal the data block in the same way as metadata in
185  * the functions above. The difference is that here we have a tag
186  * which is two __be64's being the block number (as per meta data)
187  * and a flag which says whether the data block needs escaping or
188  * not. This means we need a new log entry for each 251 or so data
189  * blocks, which isn't an enormous overhead but twice as much as
190  * for normal metadata blocks.
191  */
192 void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh)
193 {
194 	struct gfs2_trans *tr = current->journal_info;
195 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
196 	struct gfs2_bufdata *bd;
197 
198 	lock_buffer(bh);
199 	if (buffer_pinned(bh)) {
200 		set_bit(TR_TOUCHED, &tr->tr_flags);
201 		goto out;
202 	}
203 	gfs2_log_lock(sdp);
204 	bd = bh->b_private;
205 	if (bd == NULL) {
206 		gfs2_log_unlock(sdp);
207 		unlock_buffer(bh);
208 		if (bh->b_private == NULL)
209 			bd = gfs2_alloc_bufdata(gl, bh);
210 		else
211 			bd = bh->b_private;
212 		lock_buffer(bh);
213 		gfs2_log_lock(sdp);
214 	}
215 	gfs2_assert(sdp, bd->bd_gl == gl);
216 	set_bit(TR_TOUCHED, &tr->tr_flags);
217 	if (list_empty(&bd->bd_list)) {
218 		set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
219 		set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
220 		gfs2_pin(sdp, bd->bd_bh);
221 		tr->tr_num_databuf_new++;
222 		list_add_tail(&bd->bd_list, &tr->tr_databuf);
223 	}
224 	gfs2_log_unlock(sdp);
225 out:
226 	unlock_buffer(bh);
227 }
228 
229 void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh)
230 {
231 
232 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
233 	struct gfs2_bufdata *bd;
234 	struct gfs2_meta_header *mh;
235 	struct gfs2_trans *tr = current->journal_info;
236 
237 	lock_buffer(bh);
238 	if (buffer_pinned(bh)) {
239 		set_bit(TR_TOUCHED, &tr->tr_flags);
240 		goto out;
241 	}
242 	gfs2_log_lock(sdp);
243 	bd = bh->b_private;
244 	if (bd == NULL) {
245 		gfs2_log_unlock(sdp);
246 		unlock_buffer(bh);
247 		lock_page(bh->b_page);
248 		if (bh->b_private == NULL)
249 			bd = gfs2_alloc_bufdata(gl, bh);
250 		else
251 			bd = bh->b_private;
252 		unlock_page(bh->b_page);
253 		lock_buffer(bh);
254 		gfs2_log_lock(sdp);
255 	}
256 	gfs2_assert(sdp, bd->bd_gl == gl);
257 	set_bit(TR_TOUCHED, &tr->tr_flags);
258 	if (!list_empty(&bd->bd_list))
259 		goto out_unlock;
260 	set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags);
261 	set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags);
262 	mh = (struct gfs2_meta_header *)bd->bd_bh->b_data;
263 	if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) {
264 		fs_err(sdp, "Attempting to add uninitialised block to "
265 		       "journal (inplace block=%lld)\n",
266 		       (unsigned long long)bd->bd_bh->b_blocknr);
267 		BUG();
268 	}
269 	if (unlikely(test_bit(SDF_FROZEN, &sdp->sd_flags))) {
270 		fs_info(sdp, "GFS2:adding buf while frozen\n");
271 		gfs2_assert_withdraw(sdp, 0);
272 	}
273 	if (unlikely(gfs2_withdrawn(sdp))) {
274 		fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n",
275 			(unsigned long long)bd->bd_bh->b_blocknr);
276 	}
277 	gfs2_pin(sdp, bd->bd_bh);
278 	mh->__pad0 = cpu_to_be64(0);
279 	mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
280 	list_add(&bd->bd_list, &tr->tr_buf);
281 	tr->tr_num_buf_new++;
282 out_unlock:
283 	gfs2_log_unlock(sdp);
284 out:
285 	unlock_buffer(bh);
286 }
287 
288 void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
289 {
290 	struct gfs2_trans *tr = current->journal_info;
291 
292 	BUG_ON(!list_empty(&bd->bd_list));
293 	gfs2_add_revoke(sdp, bd);
294 	set_bit(TR_TOUCHED, &tr->tr_flags);
295 	tr->tr_num_revoke++;
296 }
297 
298 void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len)
299 {
300 	struct gfs2_bufdata *bd, *tmp;
301 	unsigned int n = len;
302 
303 	gfs2_log_lock(sdp);
304 	list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) {
305 		if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) {
306 			list_del_init(&bd->bd_list);
307 			gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke);
308 			sdp->sd_log_num_revoke--;
309 			if (bd->bd_gl)
310 				gfs2_glock_remove_revoke(bd->bd_gl);
311 			kmem_cache_free(gfs2_bufdata_cachep, bd);
312 			gfs2_log_release_revokes(sdp, 1);
313 			if (--n == 0)
314 				break;
315 		}
316 	}
317 	gfs2_log_unlock(sdp);
318 }
319 
320 void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
321 {
322 	if (tr == NULL)
323 		return;
324 
325 	gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
326 	gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
327 	gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf));
328 	gfs2_assert_warn(sdp, list_empty(&tr->tr_buf));
329 	kmem_cache_free(gfs2_trans_cachep, tr);
330 }
331