xref: /openbmc/linux/fs/gfs2/log.c (revision ca399c96)
17336d0e6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b3b94faaSDavid Teigland /*
3b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4da6dd40dSBob Peterson  * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
5b3b94faaSDavid Teigland  */
6b3b94faaSDavid Teigland 
7b3b94faaSDavid Teigland #include <linux/sched.h>
8b3b94faaSDavid Teigland #include <linux/slab.h>
9b3b94faaSDavid Teigland #include <linux/spinlock.h>
10b3b94faaSDavid Teigland #include <linux/completion.h>
11b3b94faaSDavid Teigland #include <linux/buffer_head.h>
125c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h>
1371b86f56SSteven Whitehouse #include <linux/crc32.h>
14c1696fb8SBob Peterson #include <linux/crc32c.h>
15a25311c8SSteven Whitehouse #include <linux/delay.h>
16ec69b188SSteven Whitehouse #include <linux/kthread.h>
17ec69b188SSteven Whitehouse #include <linux/freezer.h>
18254db57fSSteven Whitehouse #include <linux/bio.h>
19885bcecaSSteven Whitehouse #include <linux/blkdev.h>
204667a0ecSSteven Whitehouse #include <linux/writeback.h>
214a36d08dSBob Peterson #include <linux/list_sort.h>
22b3b94faaSDavid Teigland 
23b3b94faaSDavid Teigland #include "gfs2.h"
245c676f6dSSteven Whitehouse #include "incore.h"
25b3b94faaSDavid Teigland #include "bmap.h"
26b3b94faaSDavid Teigland #include "glock.h"
27b3b94faaSDavid Teigland #include "log.h"
28b3b94faaSDavid Teigland #include "lops.h"
29b3b94faaSDavid Teigland #include "meta_io.h"
305c676f6dSSteven Whitehouse #include "util.h"
3171b86f56SSteven Whitehouse #include "dir.h"
3263997775SSteven Whitehouse #include "trace_gfs2.h"
33b3b94faaSDavid Teigland 
34feed98a8SBob Peterson static void gfs2_log_shutdown(struct gfs2_sbd *sdp);
35feed98a8SBob Peterson 
36b3b94faaSDavid Teigland /**
37b3b94faaSDavid Teigland  * gfs2_struct2blk - compute stuff
38b3b94faaSDavid Teigland  * @sdp: the filesystem
39b3b94faaSDavid Teigland  * @nstruct: the number of structures
40b3b94faaSDavid Teigland  *
41b3b94faaSDavid Teigland  * Compute the number of log descriptor blocks needed to hold a certain number
42b3b94faaSDavid Teigland  * of structures of a certain size.
43b3b94faaSDavid Teigland  *
44b3b94faaSDavid Teigland  * Returns: the number of blocks needed (minimum is always 1)
45b3b94faaSDavid Teigland  */
46b3b94faaSDavid Teigland 
472e9eeaa1SBob Peterson unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct)
48b3b94faaSDavid Teigland {
49b3b94faaSDavid Teigland 	unsigned int blks;
50b3b94faaSDavid Teigland 	unsigned int first, second;
51b3b94faaSDavid Teigland 
52b3b94faaSDavid Teigland 	blks = 1;
532e9eeaa1SBob Peterson 	first = sdp->sd_ldptrs;
54b3b94faaSDavid Teigland 
55b3b94faaSDavid Teigland 	if (nstruct > first) {
562e9eeaa1SBob Peterson 		second = sdp->sd_inptrs;
575c676f6dSSteven Whitehouse 		blks += DIV_ROUND_UP(nstruct - first, second);
58b3b94faaSDavid Teigland 	}
59b3b94faaSDavid Teigland 
60b3b94faaSDavid Teigland 	return blks;
61b3b94faaSDavid Teigland }
62b3b94faaSDavid Teigland 
63ddacfaf7SSteven Whitehouse /**
641e1a3d03SSteven Whitehouse  * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
651e1a3d03SSteven Whitehouse  * @mapping: The associated mapping (maybe NULL)
661e1a3d03SSteven Whitehouse  * @bd: The gfs2_bufdata to remove
671e1a3d03SSteven Whitehouse  *
68c618e87aSSteven Whitehouse  * The ail lock _must_ be held when calling this function
691e1a3d03SSteven Whitehouse  *
701e1a3d03SSteven Whitehouse  */
711e1a3d03SSteven Whitehouse 
729bc980cdSBob Peterson static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
731e1a3d03SSteven Whitehouse {
7416ca9412SBenjamin Marzinski 	bd->bd_tr = NULL;
751ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_st_list);
761ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_gl_list);
771e1a3d03SSteven Whitehouse 	atomic_dec(&bd->bd_gl->gl_ail_count);
781e1a3d03SSteven Whitehouse 	brelse(bd->bd_bh);
791e1a3d03SSteven Whitehouse }
801e1a3d03SSteven Whitehouse 
811e1a3d03SSteven Whitehouse /**
82ddacfaf7SSteven Whitehouse  * gfs2_ail1_start_one - Start I/O on a part of the AIL
83ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
844667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
854667a0ecSSteven Whitehouse  * @ai: The ail structure
86ddacfaf7SSteven Whitehouse  *
87ddacfaf7SSteven Whitehouse  */
88ddacfaf7SSteven Whitehouse 
894f1de018SSteven Whitehouse static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
904667a0ecSSteven Whitehouse 			       struct writeback_control *wbc,
9169511080SBob Peterson 			       struct gfs2_trans *tr)
92d6a079e8SDave Chinner __releases(&sdp->sd_ail_lock)
93d6a079e8SDave Chinner __acquires(&sdp->sd_ail_lock)
94ddacfaf7SSteven Whitehouse {
955ac048bbSSteven Whitehouse 	struct gfs2_glock *gl = NULL;
964667a0ecSSteven Whitehouse 	struct address_space *mapping;
97ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
98ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
99b1676cbbSBob Peterson 	int ret = 0;
100ddacfaf7SSteven Whitehouse 
10116ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
102ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
103ddacfaf7SSteven Whitehouse 
10416ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
105ddacfaf7SSteven Whitehouse 
106ddacfaf7SSteven Whitehouse 		if (!buffer_busy(bh)) {
10730fe70a8SBob Peterson 			if (buffer_uptodate(bh)) {
10830fe70a8SBob Peterson 				list_move(&bd->bd_ail_st_list,
10930fe70a8SBob Peterson 					  &tr->tr_ail2_list);
11030fe70a8SBob Peterson 				continue;
11130fe70a8SBob Peterson 			}
112036330c9SBob Peterson 			if (!cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
113ddacfaf7SSteven Whitehouse 				gfs2_io_error_bh(sdp, bh);
11469511080SBob Peterson 				gfs2_withdraw_delayed(sdp);
1159e1a9ecdSAndreas Gruenbacher 			}
116ddacfaf7SSteven Whitehouse 		}
117ddacfaf7SSteven Whitehouse 
11830fe70a8SBob Peterson 		if (gfs2_withdrawn(sdp)) {
11930fe70a8SBob Peterson 			gfs2_remove_from_ail(bd);
12030fe70a8SBob Peterson 			continue;
12130fe70a8SBob Peterson 		}
122ddacfaf7SSteven Whitehouse 		if (!buffer_dirty(bh))
123ddacfaf7SSteven Whitehouse 			continue;
1245ac048bbSSteven Whitehouse 		if (gl == bd->bd_gl)
1255ac048bbSSteven Whitehouse 			continue;
1265ac048bbSSteven Whitehouse 		gl = bd->bd_gl;
12716ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
1284667a0ecSSteven Whitehouse 		mapping = bh->b_page->mapping;
1294f1de018SSteven Whitehouse 		if (!mapping)
1304f1de018SSteven Whitehouse 			continue;
131d6a079e8SDave Chinner 		spin_unlock(&sdp->sd_ail_lock);
132b1676cbbSBob Peterson 		ret = generic_writepages(mapping, wbc);
133d6a079e8SDave Chinner 		spin_lock(&sdp->sd_ail_lock);
134b1676cbbSBob Peterson 		if (ret || wbc->nr_to_write <= 0)
135ddacfaf7SSteven Whitehouse 			break;
136b1676cbbSBob Peterson 		return -EBUSY;
137ddacfaf7SSteven Whitehouse 	}
1384f1de018SSteven Whitehouse 
139b1676cbbSBob Peterson 	return ret;
1404667a0ecSSteven Whitehouse }
1414667a0ecSSteven Whitehouse 
1424667a0ecSSteven Whitehouse 
1434667a0ecSSteven Whitehouse /**
1444667a0ecSSteven Whitehouse  * gfs2_ail1_flush - start writeback of some ail1 entries
1454667a0ecSSteven Whitehouse  * @sdp: The super block
1464667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
1474667a0ecSSteven Whitehouse  *
1484667a0ecSSteven Whitehouse  * Writes back some ail1 entries, according to the limits in the
1494667a0ecSSteven Whitehouse  * writeback control structure
1504667a0ecSSteven Whitehouse  */
1514667a0ecSSteven Whitehouse 
1524667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
1534667a0ecSSteven Whitehouse {
1544667a0ecSSteven Whitehouse 	struct list_head *head = &sdp->sd_ail1_list;
15516ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
156885bcecaSSteven Whitehouse 	struct blk_plug plug;
157b1676cbbSBob Peterson 	int ret = 0;
1584667a0ecSSteven Whitehouse 
159c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 1);
160885bcecaSSteven Whitehouse 	blk_start_plug(&plug);
1614667a0ecSSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
1624f1de018SSteven Whitehouse restart:
16316ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, head, tr_list) {
1644667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
1654667a0ecSSteven Whitehouse 			break;
166b1676cbbSBob Peterson 		ret = gfs2_ail1_start_one(sdp, wbc, tr);
167b1676cbbSBob Peterson 		if (ret) {
168b1676cbbSBob Peterson 			if (ret == -EBUSY)
1694f1de018SSteven Whitehouse 				goto restart;
170b1676cbbSBob Peterson 			break;
171b1676cbbSBob Peterson 		}
1724667a0ecSSteven Whitehouse 	}
1734667a0ecSSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
174885bcecaSSteven Whitehouse 	blk_finish_plug(&plug);
175b1676cbbSBob Peterson 	if (ret)
176badb55ecSAndreas Gruenbacher 		gfs2_withdraw(sdp);
177c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 0);
1784667a0ecSSteven Whitehouse }
1794667a0ecSSteven Whitehouse 
1804667a0ecSSteven Whitehouse /**
1814667a0ecSSteven Whitehouse  * gfs2_ail1_start - start writeback of all ail1 entries
1824667a0ecSSteven Whitehouse  * @sdp: The superblock
1834667a0ecSSteven Whitehouse  */
1844667a0ecSSteven Whitehouse 
1854667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp)
1864667a0ecSSteven Whitehouse {
1874667a0ecSSteven Whitehouse 	struct writeback_control wbc = {
1884667a0ecSSteven Whitehouse 		.sync_mode = WB_SYNC_NONE,
1894667a0ecSSteven Whitehouse 		.nr_to_write = LONG_MAX,
1904667a0ecSSteven Whitehouse 		.range_start = 0,
1914667a0ecSSteven Whitehouse 		.range_end = LLONG_MAX,
1924667a0ecSSteven Whitehouse 	};
1934667a0ecSSteven Whitehouse 
1944667a0ecSSteven Whitehouse 	return gfs2_ail1_flush(sdp, &wbc);
195ddacfaf7SSteven Whitehouse }
196ddacfaf7SSteven Whitehouse 
197ddacfaf7SSteven Whitehouse /**
198ddacfaf7SSteven Whitehouse  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
199ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
2005e4c7632SBob Peterson  * @tr: the transaction
2015e4c7632SBob Peterson  * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
202ddacfaf7SSteven Whitehouse  *
203ddacfaf7SSteven Whitehouse  */
204ddacfaf7SSteven Whitehouse 
2055e4c7632SBob Peterson static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
2065e4c7632SBob Peterson 				int *max_revokes)
207ddacfaf7SSteven Whitehouse {
208ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
209ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
210ddacfaf7SSteven Whitehouse 
21116ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
212ddacfaf7SSteven Whitehouse 					 bd_ail_st_list) {
213ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
21416ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
215036330c9SBob Peterson 		/*
216036330c9SBob Peterson 		 * If another process flagged an io error, e.g. writing to the
217036330c9SBob Peterson 		 * journal, error all other bhs and move them off the ail1 to
218036330c9SBob Peterson 		 * prevent a tight loop when unmount tries to flush ail1,
219036330c9SBob Peterson 		 * regardless of whether they're still busy. If no outside
220036330c9SBob Peterson 		 * errors were found and the buffer is busy, move to the next.
221036330c9SBob Peterson 		 * If the ail buffer is not busy and caught an error, flag it
222036330c9SBob Peterson 		 * for others.
223036330c9SBob Peterson 		 */
224036330c9SBob Peterson 		if (!sdp->sd_log_error && buffer_busy(bh))
225ddacfaf7SSteven Whitehouse 			continue;
226b524abccSBob Peterson 		if (!buffer_uptodate(bh) &&
227036330c9SBob Peterson 		    !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
228ddacfaf7SSteven Whitehouse 			gfs2_io_error_bh(sdp, bh);
22969511080SBob Peterson 			gfs2_withdraw_delayed(sdp);
2309e1a9ecdSAndreas Gruenbacher 		}
2315e4c7632SBob Peterson 		/*
2325e4c7632SBob Peterson 		 * If we have space for revokes and the bd is no longer on any
2335e4c7632SBob Peterson 		 * buf list, we can just add a revoke for it immediately and
2345e4c7632SBob Peterson 		 * avoid having to put it on the ail2 list, where it would need
2355e4c7632SBob Peterson 		 * to be revoked later.
2365e4c7632SBob Peterson 		 */
2375e4c7632SBob Peterson 		if (*max_revokes && list_empty(&bd->bd_list)) {
2385e4c7632SBob Peterson 			gfs2_add_revoke(sdp, bd);
2395e4c7632SBob Peterson 			(*max_revokes)--;
2405e4c7632SBob Peterson 			continue;
2415e4c7632SBob Peterson 		}
24216ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
243ddacfaf7SSteven Whitehouse 	}
244ddacfaf7SSteven Whitehouse }
245ddacfaf7SSteven Whitehouse 
2464667a0ecSSteven Whitehouse /**
2474667a0ecSSteven Whitehouse  * gfs2_ail1_empty - Try to empty the ail1 lists
2484667a0ecSSteven Whitehouse  * @sdp: The superblock
2495e4c7632SBob Peterson  * @max_revokes: If non-zero, add revokes where appropriate
2504667a0ecSSteven Whitehouse  *
2514667a0ecSSteven Whitehouse  * Tries to empty the ail1 lists, starting with the oldest first
2524667a0ecSSteven Whitehouse  */
253b3b94faaSDavid Teigland 
2545e4c7632SBob Peterson static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
255b3b94faaSDavid Teigland {
25616ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *s;
2575d054964SBenjamin Marzinski 	int oldest_tr = 1;
258b3b94faaSDavid Teigland 	int ret;
259b3b94faaSDavid Teigland 
260d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
26116ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
2625e4c7632SBob Peterson 		gfs2_ail1_empty_one(sdp, tr, &max_revokes);
2635d054964SBenjamin Marzinski 		if (list_empty(&tr->tr_ail1_list) && oldest_tr)
26416ca9412SBenjamin Marzinski 			list_move(&tr->tr_list, &sdp->sd_ail2_list);
2654667a0ecSSteven Whitehouse 		else
2665d054964SBenjamin Marzinski 			oldest_tr = 0;
267b3b94faaSDavid Teigland 	}
268b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
269d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
270b3b94faaSDavid Teigland 
27169511080SBob Peterson 	if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) {
272badb55ecSAndreas Gruenbacher 		gfs2_lm(sdp, "fatal: I/O error(s)\n");
273badb55ecSAndreas Gruenbacher 		gfs2_withdraw(sdp);
274badb55ecSAndreas Gruenbacher 	}
2759e1a9ecdSAndreas Gruenbacher 
276b3b94faaSDavid Teigland 	return ret;
277b3b94faaSDavid Teigland }
278b3b94faaSDavid Teigland 
27926b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
28026b06a69SSteven Whitehouse {
28116ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
28226b06a69SSteven Whitehouse 	struct gfs2_bufdata *bd;
28326b06a69SSteven Whitehouse 	struct buffer_head *bh;
28426b06a69SSteven Whitehouse 
28526b06a69SSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
28616ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
28716ca9412SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
28826b06a69SSteven Whitehouse 			bh = bd->bd_bh;
28926b06a69SSteven Whitehouse 			if (!buffer_locked(bh))
29026b06a69SSteven Whitehouse 				continue;
29126b06a69SSteven Whitehouse 			get_bh(bh);
29226b06a69SSteven Whitehouse 			spin_unlock(&sdp->sd_ail_lock);
29326b06a69SSteven Whitehouse 			wait_on_buffer(bh);
29426b06a69SSteven Whitehouse 			brelse(bh);
29526b06a69SSteven Whitehouse 			return;
29626b06a69SSteven Whitehouse 		}
29726b06a69SSteven Whitehouse 	}
29826b06a69SSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
29926b06a69SSteven Whitehouse }
300ddacfaf7SSteven Whitehouse 
301ddacfaf7SSteven Whitehouse /**
3022ca0c2fbSBob Peterson  * gfs2_ail_empty_tr - empty one of the ail lists for a transaction
303ddacfaf7SSteven Whitehouse  */
304ddacfaf7SSteven Whitehouse 
3052ca0c2fbSBob Peterson static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
3062ca0c2fbSBob Peterson 			      struct list_head *head)
307ddacfaf7SSteven Whitehouse {
308ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd;
309ddacfaf7SSteven Whitehouse 
310ddacfaf7SSteven Whitehouse 	while (!list_empty(head)) {
3112ca0c2fbSBob Peterson 		bd = list_first_entry(head, struct gfs2_bufdata,
312ddacfaf7SSteven Whitehouse 				      bd_ail_st_list);
31316ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
314f91a0d3eSSteven Whitehouse 		gfs2_remove_from_ail(bd);
315ddacfaf7SSteven Whitehouse 	}
316ddacfaf7SSteven Whitehouse }
317ddacfaf7SSteven Whitehouse 
318b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
319b3b94faaSDavid Teigland {
32016ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *safe;
321b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
322b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
323b3b94faaSDavid Teigland 	int a, b, rm;
324b3b94faaSDavid Teigland 
325d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
326b3b94faaSDavid Teigland 
32716ca9412SBenjamin Marzinski 	list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
32816ca9412SBenjamin Marzinski 		a = (old_tail <= tr->tr_first);
32916ca9412SBenjamin Marzinski 		b = (tr->tr_first < new_tail);
330b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
331b3b94faaSDavid Teigland 		if (!rm)
332b3b94faaSDavid Teigland 			continue;
333b3b94faaSDavid Teigland 
3342ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
33516ca9412SBenjamin Marzinski 		list_del(&tr->tr_list);
33616ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
33716ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
33816ca9412SBenjamin Marzinski 		kfree(tr);
339b3b94faaSDavid Teigland 	}
340b3b94faaSDavid Teigland 
341d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
342b3b94faaSDavid Teigland }
343b3b94faaSDavid Teigland 
344b3b94faaSDavid Teigland /**
34524972557SBenjamin Marzinski  * gfs2_log_release - Release a given number of log blocks
34624972557SBenjamin Marzinski  * @sdp: The GFS2 superblock
34724972557SBenjamin Marzinski  * @blks: The number of blocks
34824972557SBenjamin Marzinski  *
34924972557SBenjamin Marzinski  */
35024972557SBenjamin Marzinski 
35124972557SBenjamin Marzinski void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
35224972557SBenjamin Marzinski {
35324972557SBenjamin Marzinski 
35424972557SBenjamin Marzinski 	atomic_add(blks, &sdp->sd_log_blks_free);
35524972557SBenjamin Marzinski 	trace_gfs2_log_blocks(sdp, blks);
35624972557SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
35724972557SBenjamin Marzinski 				  sdp->sd_jdesc->jd_blocks);
35824972557SBenjamin Marzinski 	up_read(&sdp->sd_log_flush_lock);
35924972557SBenjamin Marzinski }
36024972557SBenjamin Marzinski 
36124972557SBenjamin Marzinski /**
362b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
363b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
364b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
365b3b94faaSDavid Teigland  *
36689918647SSteven Whitehouse  * Note that we never give out the last few blocks of the journal. Thats
3672332c443SRobert Peterson  * due to the fact that there is a small number of header blocks
368b004157aSSteven Whitehouse  * associated with each log flush. The exact number can't be known until
369b004157aSSteven Whitehouse  * flush time, so we ensure that we have just enough free blocks at all
370b004157aSSteven Whitehouse  * times to avoid running out during a log flush.
371b004157aSSteven Whitehouse  *
3725e687eacSBenjamin Marzinski  * We no longer flush the log here, instead we wake up logd to do that
3735e687eacSBenjamin Marzinski  * for us. To avoid the thundering herd and to ensure that we deal fairly
3745e687eacSBenjamin Marzinski  * with queued waiters, we use an exclusive wait. This means that when we
3755e687eacSBenjamin Marzinski  * get woken with enough journal space to get our reservation, we need to
3765e687eacSBenjamin Marzinski  * wake the next waiter on the list.
3775e687eacSBenjamin Marzinski  *
378b3b94faaSDavid Teigland  * Returns: errno
379b3b94faaSDavid Teigland  */
380b3b94faaSDavid Teigland 
381b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
382b3b94faaSDavid Teigland {
3832e60d768SBenjamin Marzinski 	int ret = 0;
3845d054964SBenjamin Marzinski 	unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
3855e687eacSBenjamin Marzinski 	unsigned wanted = blks + reserved_blks;
3865e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
3875e687eacSBenjamin Marzinski 	int did_wait = 0;
3885e687eacSBenjamin Marzinski 	unsigned int free_blocks;
389b3b94faaSDavid Teigland 
390b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
391b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
392b3b94faaSDavid Teigland 		return -EINVAL;
393f07b3520SBob Peterson 	atomic_add(blks, &sdp->sd_log_blks_needed);
3945e687eacSBenjamin Marzinski retry:
3955e687eacSBenjamin Marzinski 	free_blocks = atomic_read(&sdp->sd_log_blks_free);
3965e687eacSBenjamin Marzinski 	if (unlikely(free_blocks <= wanted)) {
3975e687eacSBenjamin Marzinski 		do {
3985e687eacSBenjamin Marzinski 			prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
3995e687eacSBenjamin Marzinski 					TASK_UNINTERRUPTIBLE);
4005e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_logd_waitq);
4015e687eacSBenjamin Marzinski 			did_wait = 1;
4025e687eacSBenjamin Marzinski 			if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
4035e687eacSBenjamin Marzinski 				io_schedule();
4045e687eacSBenjamin Marzinski 			free_blocks = atomic_read(&sdp->sd_log_blks_free);
4055e687eacSBenjamin Marzinski 		} while(free_blocks <= wanted);
4065e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_log_waitq, &wait);
407b3b94faaSDavid Teigland 	}
4082e60d768SBenjamin Marzinski 	atomic_inc(&sdp->sd_reserving_log);
4095e687eacSBenjamin Marzinski 	if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
4102e60d768SBenjamin Marzinski 				free_blocks - blks) != free_blocks) {
4112e60d768SBenjamin Marzinski 		if (atomic_dec_and_test(&sdp->sd_reserving_log))
4122e60d768SBenjamin Marzinski 			wake_up(&sdp->sd_reserving_log_wait);
4135e687eacSBenjamin Marzinski 		goto retry;
4142e60d768SBenjamin Marzinski 	}
415f07b3520SBob Peterson 	atomic_sub(blks, &sdp->sd_log_blks_needed);
41663997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, -blks);
4175e687eacSBenjamin Marzinski 
4185e687eacSBenjamin Marzinski 	/*
4195e687eacSBenjamin Marzinski 	 * If we waited, then so might others, wake them up _after_ we get
4205e687eacSBenjamin Marzinski 	 * our share of the log.
4215e687eacSBenjamin Marzinski 	 */
4225e687eacSBenjamin Marzinski 	if (unlikely(did_wait))
4235e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_log_waitq);
424484adff8SSteven Whitehouse 
425484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
42624972557SBenjamin Marzinski 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
42724972557SBenjamin Marzinski 		gfs2_log_release(sdp, blks);
4282e60d768SBenjamin Marzinski 		ret = -EROFS;
42924972557SBenjamin Marzinski 	}
4302e60d768SBenjamin Marzinski 	if (atomic_dec_and_test(&sdp->sd_reserving_log))
4312e60d768SBenjamin Marzinski 		wake_up(&sdp->sd_reserving_log_wait);
4322e60d768SBenjamin Marzinski 	return ret;
433b3b94faaSDavid Teigland }
434b3b94faaSDavid Teigland 
435b3b94faaSDavid Teigland /**
436b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
437b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
438b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
439b3b94faaSDavid Teigland  * @older: The older journal block of the pair
440b3b94faaSDavid Teigland  *
441b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
442b3b94faaSDavid Teigland  *   blocks in the journal
443b3b94faaSDavid Teigland  *
444b3b94faaSDavid Teigland  * Returns: the distance in blocks
445b3b94faaSDavid Teigland  */
446b3b94faaSDavid Teigland 
447faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
448b3b94faaSDavid Teigland 					unsigned int older)
449b3b94faaSDavid Teigland {
450b3b94faaSDavid Teigland 	int dist;
451b3b94faaSDavid Teigland 
452b3b94faaSDavid Teigland 	dist = newer - older;
453b3b94faaSDavid Teigland 	if (dist < 0)
454b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
455b3b94faaSDavid Teigland 
456b3b94faaSDavid Teigland 	return dist;
457b3b94faaSDavid Teigland }
458b3b94faaSDavid Teigland 
4592332c443SRobert Peterson /**
4602332c443SRobert Peterson  * calc_reserved - Calculate the number of blocks to reserve when
4612332c443SRobert Peterson  *                 refunding a transaction's unused buffers.
4622332c443SRobert Peterson  * @sdp: The GFS2 superblock
4632332c443SRobert Peterson  *
4642332c443SRobert Peterson  * This is complex.  We need to reserve room for all our currently used
4652332c443SRobert Peterson  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
4662332c443SRobert Peterson  * all our journaled data buffers for journaled files (e.g. files in the
4672332c443SRobert Peterson  * meta_fs like rindex, or files for which chattr +j was done.)
4682332c443SRobert Peterson  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
4692332c443SRobert Peterson  * will count it as free space (sd_log_blks_free) and corruption will follow.
4702332c443SRobert Peterson  *
4712332c443SRobert Peterson  * We can have metadata bufs and jdata bufs in the same journal.  So each
4722332c443SRobert Peterson  * type gets its own log header, for which we need to reserve a block.
4732332c443SRobert Peterson  * In fact, each type has the potential for needing more than one header
4742332c443SRobert Peterson  * in cases where we have more buffers than will fit on a journal page.
4752332c443SRobert Peterson  * Metadata journal entries take up half the space of journaled buffer entries.
4762332c443SRobert Peterson  * Thus, metadata entries have buf_limit (502) and journaled buffers have
4772332c443SRobert Peterson  * databuf_limit (251) before they cause a wrap around.
4782332c443SRobert Peterson  *
4792332c443SRobert Peterson  * Also, we need to reserve blocks for revoke journal entries and one for an
4802332c443SRobert Peterson  * overall header for the lot.
4812332c443SRobert Peterson  *
4822332c443SRobert Peterson  * Returns: the number of blocks reserved
4832332c443SRobert Peterson  */
4842332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp)
4852332c443SRobert Peterson {
4862332c443SRobert Peterson 	unsigned int reserved = 0;
487022ef4feSSteven Whitehouse 	unsigned int mbuf;
488022ef4feSSteven Whitehouse 	unsigned int dbuf;
489022ef4feSSteven Whitehouse 	struct gfs2_trans *tr = sdp->sd_log_tr;
4902332c443SRobert Peterson 
491022ef4feSSteven Whitehouse 	if (tr) {
492022ef4feSSteven Whitehouse 		mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
493022ef4feSSteven Whitehouse 		dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
494022ef4feSSteven Whitehouse 		reserved = mbuf + dbuf;
495022ef4feSSteven Whitehouse 		/* Account for header blocks */
496022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
497022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
498022ef4feSSteven Whitehouse 	}
4992332c443SRobert Peterson 
5005d439758SAndreas Gruenbacher 	if (sdp->sd_log_committed_revoke > 0)
5015d439758SAndreas Gruenbacher 		reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke);
5022332c443SRobert Peterson 	/* One for the overall header */
5032332c443SRobert Peterson 	if (reserved)
5042332c443SRobert Peterson 		reserved++;
5052332c443SRobert Peterson 	return reserved;
5062332c443SRobert Peterson }
5072332c443SRobert Peterson 
508b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
509b3b94faaSDavid Teigland {
51016ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
511b3b94faaSDavid Teigland 	unsigned int tail;
512b3b94faaSDavid Teigland 
513d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
514b3b94faaSDavid Teigland 
515faa31ce8SSteven Whitehouse 	if (list_empty(&sdp->sd_ail1_list)) {
516b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
517faa31ce8SSteven Whitehouse 	} else {
51816ca9412SBenjamin Marzinski 		tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
51916ca9412SBenjamin Marzinski 				tr_list);
52016ca9412SBenjamin Marzinski 		tail = tr->tr_first;
521b3b94faaSDavid Teigland 	}
522b3b94faaSDavid Teigland 
523d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
524b3b94faaSDavid Teigland 
525b3b94faaSDavid Teigland 	return tail;
526b3b94faaSDavid Teigland }
527b3b94faaSDavid Teigland 
5282332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
529b3b94faaSDavid Teigland {
530b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
531b3b94faaSDavid Teigland 
532b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
533b3b94faaSDavid Teigland 
534fd041f0bSSteven Whitehouse 	atomic_add(dist, &sdp->sd_log_blks_free);
53563997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, dist);
5365e687eacSBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
5375e687eacSBenjamin Marzinski 			     sdp->sd_jdesc->jd_blocks);
538b3b94faaSDavid Teigland 
539b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
540b3b94faaSDavid Teigland }
541b3b94faaSDavid Teigland 
542b3b94faaSDavid Teigland 
5439ff78289SBob Peterson void log_flush_wait(struct gfs2_sbd *sdp)
544b3b94faaSDavid Teigland {
54516615be1SSteven Whitehouse 	DEFINE_WAIT(wait);
546b3b94faaSDavid Teigland 
54716615be1SSteven Whitehouse 	if (atomic_read(&sdp->sd_log_in_flight)) {
54816615be1SSteven Whitehouse 		do {
54916615be1SSteven Whitehouse 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
55016615be1SSteven Whitehouse 					TASK_UNINTERRUPTIBLE);
55116615be1SSteven Whitehouse 			if (atomic_read(&sdp->sd_log_in_flight))
55216615be1SSteven Whitehouse 				io_schedule();
55316615be1SSteven Whitehouse 		} while(atomic_read(&sdp->sd_log_in_flight));
55416615be1SSteven Whitehouse 		finish_wait(&sdp->sd_log_flush_wait, &wait);
555b3b94faaSDavid Teigland 	}
556b3b94faaSDavid Teigland }
557b3b94faaSDavid Teigland 
55845138990SSteven Whitehouse static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
5594a36d08dSBob Peterson {
56045138990SSteven Whitehouse 	struct gfs2_inode *ipa, *ipb;
5614a36d08dSBob Peterson 
56245138990SSteven Whitehouse 	ipa = list_entry(a, struct gfs2_inode, i_ordered);
56345138990SSteven Whitehouse 	ipb = list_entry(b, struct gfs2_inode, i_ordered);
5644a36d08dSBob Peterson 
56545138990SSteven Whitehouse 	if (ipa->i_no_addr < ipb->i_no_addr)
5664a36d08dSBob Peterson 		return -1;
56745138990SSteven Whitehouse 	if (ipa->i_no_addr > ipb->i_no_addr)
5684a36d08dSBob Peterson 		return 1;
5694a36d08dSBob Peterson 	return 0;
5704a36d08dSBob Peterson }
5714a36d08dSBob Peterson 
572d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp)
573d7b616e2SSteven Whitehouse {
57445138990SSteven Whitehouse 	struct gfs2_inode *ip;
575d7b616e2SSteven Whitehouse 	LIST_HEAD(written);
576d7b616e2SSteven Whitehouse 
57745138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
578a5b1d3fcSAndreas Gruenbacher 	list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
579a5b1d3fcSAndreas Gruenbacher 	while (!list_empty(&sdp->sd_log_ordered)) {
580a5b1d3fcSAndreas Gruenbacher 		ip = list_entry(sdp->sd_log_ordered.next, struct gfs2_inode, i_ordered);
5811f23bc78SAbhi Das 		if (ip->i_inode.i_mapping->nrpages == 0) {
5821f23bc78SAbhi Das 			test_and_clear_bit(GIF_ORDERED, &ip->i_flags);
5831f23bc78SAbhi Das 			list_del(&ip->i_ordered);
584d7b616e2SSteven Whitehouse 			continue;
5851f23bc78SAbhi Das 		}
5861f23bc78SAbhi Das 		list_move(&ip->i_ordered, &written);
58745138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
58845138990SSteven Whitehouse 		filemap_fdatawrite(ip->i_inode.i_mapping);
58945138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
590d7b616e2SSteven Whitehouse 	}
591a5b1d3fcSAndreas Gruenbacher 	list_splice(&written, &sdp->sd_log_ordered);
59245138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
593d7b616e2SSteven Whitehouse }
594d7b616e2SSteven Whitehouse 
595d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
596d7b616e2SSteven Whitehouse {
59745138990SSteven Whitehouse 	struct gfs2_inode *ip;
598d7b616e2SSteven Whitehouse 
59945138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
600a5b1d3fcSAndreas Gruenbacher 	while (!list_empty(&sdp->sd_log_ordered)) {
601a5b1d3fcSAndreas Gruenbacher 		ip = list_entry(sdp->sd_log_ordered.next, struct gfs2_inode, i_ordered);
60245138990SSteven Whitehouse 		list_del(&ip->i_ordered);
60345138990SSteven Whitehouse 		WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
60445138990SSteven Whitehouse 		if (ip->i_inode.i_mapping->nrpages == 0)
605d7b616e2SSteven Whitehouse 			continue;
60645138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
60745138990SSteven Whitehouse 		filemap_fdatawait(ip->i_inode.i_mapping);
60845138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
609d7b616e2SSteven Whitehouse 	}
61045138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
611d7b616e2SSteven Whitehouse }
61245138990SSteven Whitehouse 
61345138990SSteven Whitehouse void gfs2_ordered_del_inode(struct gfs2_inode *ip)
61445138990SSteven Whitehouse {
61545138990SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
61645138990SSteven Whitehouse 
61745138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
61845138990SSteven Whitehouse 	if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
61945138990SSteven Whitehouse 		list_del(&ip->i_ordered);
62045138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
621d7b616e2SSteven Whitehouse }
622d7b616e2SSteven Whitehouse 
6235d054964SBenjamin Marzinski void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
6245d054964SBenjamin Marzinski {
6255d054964SBenjamin Marzinski 	struct buffer_head *bh = bd->bd_bh;
6265d054964SBenjamin Marzinski 	struct gfs2_glock *gl = bd->bd_gl;
6275d054964SBenjamin Marzinski 
6285d054964SBenjamin Marzinski 	bh->b_private = NULL;
6295d054964SBenjamin Marzinski 	bd->bd_blkno = bh->b_blocknr;
6309290a9a7SBob Peterson 	gfs2_remove_from_ail(bd); /* drops ref on bh */
6319290a9a7SBob Peterson 	bd->bd_bh = NULL;
6325d054964SBenjamin Marzinski 	sdp->sd_log_num_revoke++;
633638803d4SBob Peterson 	if (atomic_inc_return(&gl->gl_revokes) == 1)
6349287c645SAndreas Gruenbacher 		gfs2_glock_hold(gl);
6355d054964SBenjamin Marzinski 	set_bit(GLF_LFLUSH, &gl->gl_flags);
636a5b1d3fcSAndreas Gruenbacher 	list_add(&bd->bd_list, &sdp->sd_log_revokes);
6375d054964SBenjamin Marzinski }
6385d054964SBenjamin Marzinski 
639fe5e7ba1SBob Peterson void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
640fe5e7ba1SBob Peterson {
641fe5e7ba1SBob Peterson 	if (atomic_dec_return(&gl->gl_revokes) == 0) {
642fe5e7ba1SBob Peterson 		clear_bit(GLF_LFLUSH, &gl->gl_flags);
643fe5e7ba1SBob Peterson 		gfs2_glock_queue_put(gl);
644fe5e7ba1SBob Peterson 	}
645fe5e7ba1SBob Peterson }
646fe5e7ba1SBob Peterson 
6475e4c7632SBob Peterson /**
6485e4c7632SBob Peterson  * gfs2_write_revokes - Add as many revokes to the system transaction as we can
6495e4c7632SBob Peterson  * @sdp: The GFS2 superblock
6505e4c7632SBob Peterson  *
6515e4c7632SBob Peterson  * Our usual strategy is to defer writing revokes as much as we can in the hope
6525e4c7632SBob Peterson  * that we'll eventually overwrite the journal, which will make those revokes
6535e4c7632SBob Peterson  * go away.  This changes when we flush the log: at that point, there will
6545e4c7632SBob Peterson  * likely be some left-over space in the last revoke block of that transaction.
6555e4c7632SBob Peterson  * We can fill that space with additional revokes for blocks that have already
6565e4c7632SBob Peterson  * been written back.  This will basically come at no cost now, and will save
6575e4c7632SBob Peterson  * us from having to keep track of those blocks on the AIL2 list later.
6585e4c7632SBob Peterson  */
6595d054964SBenjamin Marzinski void gfs2_write_revokes(struct gfs2_sbd *sdp)
6605d054964SBenjamin Marzinski {
6615e4c7632SBob Peterson 	/* number of revokes we still have room for */
6625d054964SBenjamin Marzinski 	int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
6635d054964SBenjamin Marzinski 
6645e4c7632SBob Peterson 	gfs2_log_lock(sdp);
6655d054964SBenjamin Marzinski 	while (sdp->sd_log_num_revoke > max_revokes)
6665d054964SBenjamin Marzinski 		max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
6675d054964SBenjamin Marzinski 	max_revokes -= sdp->sd_log_num_revoke;
6685d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6695d054964SBenjamin Marzinski 		atomic_dec(&sdp->sd_log_blks_free);
6705d054964SBenjamin Marzinski 		/* If no blocks have been reserved, we need to also
6715d054964SBenjamin Marzinski 		 * reserve a block for the header */
6725d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6735d054964SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free);
6745d054964SBenjamin Marzinski 	}
6755e4c7632SBob Peterson 	gfs2_ail1_empty(sdp, max_revokes);
6765d054964SBenjamin Marzinski 	gfs2_log_unlock(sdp);
6775d054964SBenjamin Marzinski 
6785d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6795d054964SBenjamin Marzinski 		atomic_inc(&sdp->sd_log_blks_free);
6805d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6815d054964SBenjamin Marzinski 			atomic_inc(&sdp->sd_log_blks_free);
6825d054964SBenjamin Marzinski 	}
6835d054964SBenjamin Marzinski }
6845d054964SBenjamin Marzinski 
685b3b94faaSDavid Teigland /**
6867c70b896SBob Peterson  * gfs2_write_log_header - Write a journal log header buffer at lblock
687588bff95SBob Peterson  * @sdp: The GFS2 superblock
688c1696fb8SBob Peterson  * @jd: journal descriptor of the journal to which we are writing
689588bff95SBob Peterson  * @seq: sequence number
690588bff95SBob Peterson  * @tail: tail of the log
6917c70b896SBob Peterson  * @lblock: value for lh_blkno (block number relative to start of journal)
692c1696fb8SBob Peterson  * @flags: log header flags GFS2_LOG_HEAD_*
693588bff95SBob Peterson  * @op_flags: flags to pass to the bio
694588bff95SBob Peterson  *
695588bff95SBob Peterson  * Returns: the initialized log buffer descriptor
696588bff95SBob Peterson  */
697588bff95SBob Peterson 
698c1696fb8SBob Peterson void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
6997c70b896SBob Peterson 			   u64 seq, u32 tail, u32 lblock, u32 flags,
7007c70b896SBob Peterson 			   int op_flags)
701588bff95SBob Peterson {
702588bff95SBob Peterson 	struct gfs2_log_header *lh;
703c1696fb8SBob Peterson 	u32 hash, crc;
704ade48088SBob Peterson 	struct page *page;
705c1696fb8SBob Peterson 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
706c1696fb8SBob Peterson 	struct timespec64 tv;
707c1696fb8SBob Peterson 	struct super_block *sb = sdp->sd_vfs;
7087c70b896SBob Peterson 	u64 dblock;
709588bff95SBob Peterson 
710ade48088SBob Peterson 	if (gfs2_withdrawn(sdp))
711ade48088SBob Peterson 		goto out;
712ade48088SBob Peterson 
713ade48088SBob Peterson 	page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
714588bff95SBob Peterson 	lh = page_address(page);
715588bff95SBob Peterson 	clear_page(lh);
716588bff95SBob Peterson 
717588bff95SBob Peterson 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
718588bff95SBob Peterson 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
719588bff95SBob Peterson 	lh->lh_header.__pad0 = cpu_to_be64(0);
720588bff95SBob Peterson 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
721588bff95SBob Peterson 	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
722588bff95SBob Peterson 	lh->lh_sequence = cpu_to_be64(seq);
723588bff95SBob Peterson 	lh->lh_flags = cpu_to_be32(flags);
724588bff95SBob Peterson 	lh->lh_tail = cpu_to_be32(tail);
7257c70b896SBob Peterson 	lh->lh_blkno = cpu_to_be32(lblock);
726c1696fb8SBob Peterson 	hash = ~crc32(~0, lh, LH_V1_SIZE);
727588bff95SBob Peterson 	lh->lh_hash = cpu_to_be32(hash);
728588bff95SBob Peterson 
729ee9c7f9aSArnd Bergmann 	ktime_get_coarse_real_ts64(&tv);
730c1696fb8SBob Peterson 	lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
731c1696fb8SBob Peterson 	lh->lh_sec = cpu_to_be64(tv.tv_sec);
7327c70b896SBob Peterson 	if (!list_empty(&jd->extent_list))
73319ebc050SAndreas Gruenbacher 		dblock = gfs2_log_bmap(jd, lblock);
7347c70b896SBob Peterson 	else {
7357c70b896SBob Peterson 		int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock);
7367c70b896SBob Peterson 		if (gfs2_assert_withdraw(sdp, ret == 0))
7377c70b896SBob Peterson 			return;
7387c70b896SBob Peterson 	}
7397c70b896SBob Peterson 	lh->lh_addr = cpu_to_be64(dblock);
740c1696fb8SBob Peterson 	lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
741c1696fb8SBob Peterson 
742c1696fb8SBob Peterson 	/* We may only write local statfs, quota, etc., when writing to our
743c1696fb8SBob Peterson 	   own journal. The values are left 0 when recovering a journal
744c1696fb8SBob Peterson 	   different from our own. */
745c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
746c1696fb8SBob Peterson 		lh->lh_statfs_addr =
747c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
748c1696fb8SBob Peterson 		lh->lh_quota_addr =
749c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
750c1696fb8SBob Peterson 
751c1696fb8SBob Peterson 		spin_lock(&sdp->sd_statfs_spin);
752c1696fb8SBob Peterson 		lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
753c1696fb8SBob Peterson 		lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
754c1696fb8SBob Peterson 		lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
755c1696fb8SBob Peterson 		spin_unlock(&sdp->sd_statfs_spin);
756c1696fb8SBob Peterson 	}
757c1696fb8SBob Peterson 
758c1696fb8SBob Peterson 	BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
759c1696fb8SBob Peterson 
760c1696fb8SBob Peterson 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
761c1696fb8SBob Peterson 		     sb->s_blocksize - LH_V1_SIZE - 4);
762c1696fb8SBob Peterson 	lh->lh_crc = cpu_to_be32(crc);
763c1696fb8SBob Peterson 
7647c70b896SBob Peterson 	gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock);
765f4686c26SAbhi Das 	gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags);
766ade48088SBob Peterson out:
767588bff95SBob Peterson 	log_flush_wait(sdp);
768588bff95SBob Peterson }
769588bff95SBob Peterson 
770588bff95SBob Peterson /**
77134cc1781SSteven Whitehouse  * log_write_header - Get and initialize a journal header buffer
77234cc1781SSteven Whitehouse  * @sdp: The GFS2 superblock
773c1696fb8SBob Peterson  * @flags: The log header flags, including log header origin
77434cc1781SSteven Whitehouse  *
77534cc1781SSteven Whitehouse  * Returns: the initialized log buffer descriptor
77634cc1781SSteven Whitehouse  */
77734cc1781SSteven Whitehouse 
778fdb76a42SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
77934cc1781SSteven Whitehouse {
78034cc1781SSteven Whitehouse 	unsigned int tail;
7810f0b9b63SJan Kara 	int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
7822e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
78334cc1781SSteven Whitehouse 
7842e60d768SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
78534cc1781SSteven Whitehouse 	tail = current_tail(sdp);
78634cc1781SSteven Whitehouse 
78734cc1781SSteven Whitehouse 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
78834cc1781SSteven Whitehouse 		gfs2_ordered_wait(sdp);
78934cc1781SSteven Whitehouse 		log_flush_wait(sdp);
79070fd7614SChristoph Hellwig 		op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
79134cc1781SSteven Whitehouse 	}
792e8c92ed7SSteven Whitehouse 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
793c1696fb8SBob Peterson 	gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail,
7947c70b896SBob Peterson 			      sdp->sd_log_flush_head, flags, op_flags);
79519ebc050SAndreas Gruenbacher 	gfs2_log_incr_head(sdp);
79634cc1781SSteven Whitehouse 
79734cc1781SSteven Whitehouse 	if (sdp->sd_log_tail != tail)
79834cc1781SSteven Whitehouse 		log_pull_tail(sdp, tail);
79934cc1781SSteven Whitehouse }
80034cc1781SSteven Whitehouse 
80134cc1781SSteven Whitehouse /**
8022ca0c2fbSBob Peterson  * ail_drain - drain the ail lists after a withdraw
8032ca0c2fbSBob Peterson  * @sdp: Pointer to GFS2 superblock
8042ca0c2fbSBob Peterson  */
8052ca0c2fbSBob Peterson static void ail_drain(struct gfs2_sbd *sdp)
8062ca0c2fbSBob Peterson {
8072ca0c2fbSBob Peterson 	struct gfs2_trans *tr;
8082ca0c2fbSBob Peterson 
8092ca0c2fbSBob Peterson 	spin_lock(&sdp->sd_ail_lock);
8102ca0c2fbSBob Peterson 	/*
8112ca0c2fbSBob Peterson 	 * For transactions on the sd_ail1_list we need to drain both the
8122ca0c2fbSBob Peterson 	 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
8132ca0c2fbSBob Peterson 	 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
8142ca0c2fbSBob Peterson 	 * before revokes are sent for that block. Items on the sd_ail2_list
8152ca0c2fbSBob Peterson 	 * should have already gotten beyond that point, so no need.
8162ca0c2fbSBob Peterson 	 */
8172ca0c2fbSBob Peterson 	while (!list_empty(&sdp->sd_ail1_list)) {
8182ca0c2fbSBob Peterson 		tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
8192ca0c2fbSBob Peterson 				      tr_list);
8202ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
8212ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
8222ca0c2fbSBob Peterson 		list_del(&tr->tr_list);
8232ca0c2fbSBob Peterson 		kfree(tr);
8242ca0c2fbSBob Peterson 	}
8252ca0c2fbSBob Peterson 	while (!list_empty(&sdp->sd_ail2_list)) {
8262ca0c2fbSBob Peterson 		tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
8272ca0c2fbSBob Peterson 				      tr_list);
8282ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
8292ca0c2fbSBob Peterson 		list_del(&tr->tr_list);
8302ca0c2fbSBob Peterson 		kfree(tr);
8312ca0c2fbSBob Peterson 	}
8322ca0c2fbSBob Peterson 	spin_unlock(&sdp->sd_ail_lock);
8332ca0c2fbSBob Peterson }
8342ca0c2fbSBob Peterson 
8352ca0c2fbSBob Peterson /**
836b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
837b3b94faaSDavid Teigland  * @sdp: the filesystem
838b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
839805c0907SBob Peterson  * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
840b3b94faaSDavid Teigland  *
841b3b94faaSDavid Teigland  */
842b3b94faaSDavid Teigland 
843c1696fb8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
844b3b94faaSDavid Teigland {
8452ca0c2fbSBob Peterson 	struct gfs2_trans *tr = NULL;
8462e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
847b3b94faaSDavid Teigland 
848484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
849f55ab26aSSteven Whitehouse 
8502ca0c2fbSBob Peterson 	/*
8512ca0c2fbSBob Peterson 	 * Do this check while holding the log_flush_lock to prevent new
8522ca0c2fbSBob Peterson 	 * buffers from being added to the ail via gfs2_pin()
8532ca0c2fbSBob Peterson 	 */
8542ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
8552ca0c2fbSBob Peterson 		goto out;
8562ca0c2fbSBob Peterson 
8572bcd610dSSteven Whitehouse 	/* Log might have been flushed while we waited for the flush lock */
8582bcd610dSSteven Whitehouse 	if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
859484adff8SSteven Whitehouse 		up_write(&sdp->sd_log_flush_lock);
860f55ab26aSSteven Whitehouse 		return;
861f55ab26aSSteven Whitehouse 	}
862805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 1, flags);
863f55ab26aSSteven Whitehouse 
864c1696fb8SBob Peterson 	if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
865400ac52eSBenjamin Marzinski 		clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
866400ac52eSBenjamin Marzinski 
867b1ab1e44SSteven Whitehouse 	sdp->sd_log_flush_head = sdp->sd_log_head;
86816ca9412SBenjamin Marzinski 	tr = sdp->sd_log_tr;
86916ca9412SBenjamin Marzinski 	if (tr) {
87016ca9412SBenjamin Marzinski 		sdp->sd_log_tr = NULL;
87116ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail1_list);
87216ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail2_list);
873b1ab1e44SSteven Whitehouse 		tr->tr_first = sdp->sd_log_flush_head;
8742e60d768SBenjamin Marzinski 		if (unlikely (state == SFS_FROZEN))
875ca399c96SBob Peterson 			if (gfs2_assert_withdraw_delayed(sdp,
876ca399c96SBob Peterson 			       !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
877ca399c96SBob Peterson 				goto out;
87816ca9412SBenjamin Marzinski 	}
879b3b94faaSDavid Teigland 
8802e60d768SBenjamin Marzinski 	if (unlikely(state == SFS_FROZEN))
881ca399c96SBob Peterson 		if (gfs2_assert_withdraw_delayed(sdp, !sdp->sd_log_num_revoke))
882ca399c96SBob Peterson 			goto out;
883ca399c96SBob Peterson 	if (gfs2_assert_withdraw_delayed(sdp,
884ca399c96SBob Peterson 			sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke))
885ca399c96SBob Peterson 		goto out;
886b3b94faaSDavid Teigland 
887d7b616e2SSteven Whitehouse 	gfs2_ordered_write(sdp);
8882ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
8892ca0c2fbSBob Peterson 		goto out;
890d69a3c65SSteven Whitehouse 	lops_before_commit(sdp, tr);
8912ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
8922ca0c2fbSBob Peterson 		goto out;
893f4686c26SAbhi Das 	gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE);
8942ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
8952ca0c2fbSBob Peterson 		goto out;
896d7b616e2SSteven Whitehouse 
89734cc1781SSteven Whitehouse 	if (sdp->sd_log_head != sdp->sd_log_flush_head) {
898428fd95dSBob Peterson 		log_flush_wait(sdp);
899c1696fb8SBob Peterson 		log_write_header(sdp, flags);
90034cc1781SSteven Whitehouse 	} else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
901fd041f0bSSteven Whitehouse 		atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
90263997775SSteven Whitehouse 		trace_gfs2_log_blocks(sdp, -1);
903c1696fb8SBob Peterson 		log_write_header(sdp, flags);
9042332c443SRobert Peterson 	}
9052ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
9062ca0c2fbSBob Peterson 		goto out;
90716ca9412SBenjamin Marzinski 	lops_after_commit(sdp, tr);
908fe1a698fSSteven Whitehouse 
909fe1a698fSSteven Whitehouse 	gfs2_log_lock(sdp);
910b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
911faa31ce8SSteven Whitehouse 	sdp->sd_log_blks_reserved = 0;
9125d439758SAndreas Gruenbacher 	sdp->sd_log_committed_revoke = 0;
913b3b94faaSDavid Teigland 
914d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
91516ca9412SBenjamin Marzinski 	if (tr && !list_empty(&tr->tr_ail1_list)) {
91616ca9412SBenjamin Marzinski 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
91716ca9412SBenjamin Marzinski 		tr = NULL;
918b3b94faaSDavid Teigland 	}
919d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
920b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
92124972557SBenjamin Marzinski 
922c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
92324972557SBenjamin Marzinski 		if (!sdp->sd_log_idle) {
92424972557SBenjamin Marzinski 			for (;;) {
92524972557SBenjamin Marzinski 				gfs2_ail1_start(sdp);
92624972557SBenjamin Marzinski 				gfs2_ail1_wait(sdp);
9275e4c7632SBob Peterson 				if (gfs2_ail1_empty(sdp, 0))
92824972557SBenjamin Marzinski 					break;
92924972557SBenjamin Marzinski 			}
93030fe70a8SBob Peterson 			if (gfs2_withdrawn(sdp))
93130fe70a8SBob Peterson 				goto out;
93224972557SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
93324972557SBenjamin Marzinski 			trace_gfs2_log_blocks(sdp, -1);
934c1696fb8SBob Peterson 			log_write_header(sdp, flags);
93524972557SBenjamin Marzinski 			sdp->sd_log_head = sdp->sd_log_flush_head;
93624972557SBenjamin Marzinski 		}
937c1696fb8SBob Peterson 		if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
938c1696fb8SBob Peterson 			     GFS2_LOG_HEAD_FLUSH_FREEZE))
93924972557SBenjamin Marzinski 			gfs2_log_shutdown(sdp);
940c1696fb8SBob Peterson 		if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
9412e60d768SBenjamin Marzinski 			atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
94224972557SBenjamin Marzinski 	}
94324972557SBenjamin Marzinski 
94430fe70a8SBob Peterson out:
9452ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp)) {
9462ca0c2fbSBob Peterson 		ail_drain(sdp); /* frees all transactions */
9472ca0c2fbSBob Peterson 		tr = NULL;
9482ca0c2fbSBob Peterson 	}
9492ca0c2fbSBob Peterson 
950805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 0, flags);
951484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
952b3b94faaSDavid Teigland 
95316ca9412SBenjamin Marzinski 	kfree(tr);
954b3b94faaSDavid Teigland }
955b3b94faaSDavid Teigland 
956d69a3c65SSteven Whitehouse /**
957d69a3c65SSteven Whitehouse  * gfs2_merge_trans - Merge a new transaction into a cached transaction
958d69a3c65SSteven Whitehouse  * @old: Original transaction to be expanded
959d69a3c65SSteven Whitehouse  * @new: New transaction to be merged
960d69a3c65SSteven Whitehouse  */
961d69a3c65SSteven Whitehouse 
962d69a3c65SSteven Whitehouse static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
963d69a3c65SSteven Whitehouse {
9649862ca05SBob Peterson 	WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
965d69a3c65SSteven Whitehouse 
966d69a3c65SSteven Whitehouse 	old->tr_num_buf_new	+= new->tr_num_buf_new;
967d69a3c65SSteven Whitehouse 	old->tr_num_databuf_new	+= new->tr_num_databuf_new;
968d69a3c65SSteven Whitehouse 	old->tr_num_buf_rm	+= new->tr_num_buf_rm;
969d69a3c65SSteven Whitehouse 	old->tr_num_databuf_rm	+= new->tr_num_databuf_rm;
970d69a3c65SSteven Whitehouse 	old->tr_num_revoke	+= new->tr_num_revoke;
971a31b4ec5SBob Peterson 	old->tr_num_revoke_rm	+= new->tr_num_revoke_rm;
972d69a3c65SSteven Whitehouse 
973d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
974d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_buf, &old->tr_buf);
975d69a3c65SSteven Whitehouse }
976d69a3c65SSteven Whitehouse 
977b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
978b3b94faaSDavid Teigland {
9792332c443SRobert Peterson 	unsigned int reserved;
980ac39aaddSSteven Whitehouse 	unsigned int unused;
981022ef4feSSteven Whitehouse 	unsigned int maxres;
982b3b94faaSDavid Teigland 
983b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
984b3b94faaSDavid Teigland 
985d69a3c65SSteven Whitehouse 	if (sdp->sd_log_tr) {
986d69a3c65SSteven Whitehouse 		gfs2_merge_trans(sdp->sd_log_tr, tr);
987d69a3c65SSteven Whitehouse 	} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
9889862ca05SBob Peterson 		gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
98916ca9412SBenjamin Marzinski 		sdp->sd_log_tr = tr;
9909862ca05SBob Peterson 		set_bit(TR_ATTACHED, &tr->tr_flags);
99116ca9412SBenjamin Marzinski 	}
992022ef4feSSteven Whitehouse 
993a31b4ec5SBob Peterson 	sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
994022ef4feSSteven Whitehouse 	reserved = calc_reserved(sdp);
995022ef4feSSteven Whitehouse 	maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
996022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, maxres >= reserved);
997022ef4feSSteven Whitehouse 	unused = maxres - reserved;
998022ef4feSSteven Whitehouse 	atomic_add(unused, &sdp->sd_log_blks_free);
999022ef4feSSteven Whitehouse 	trace_gfs2_log_blocks(sdp, unused);
1000022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
1001022ef4feSSteven Whitehouse 			     sdp->sd_jdesc->jd_blocks);
1002022ef4feSSteven Whitehouse 	sdp->sd_log_blks_reserved = reserved;
1003022ef4feSSteven Whitehouse 
1004b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
1005b3b94faaSDavid Teigland }
1006b3b94faaSDavid Teigland 
1007b3b94faaSDavid Teigland /**
1008b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
1009b3b94faaSDavid Teigland  * @sdp: the filesystem
1010b3b94faaSDavid Teigland  * @tr: the transaction
1011b3b94faaSDavid Teigland  *
10125e687eacSBenjamin Marzinski  * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
10135e687eacSBenjamin Marzinski  * or the total number of used blocks (pinned blocks plus AIL blocks)
10145e687eacSBenjamin Marzinski  * is greater than thresh2.
10155e687eacSBenjamin Marzinski  *
10165e687eacSBenjamin Marzinski  * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
10175e687eacSBenjamin Marzinski  * journal size.
10185e687eacSBenjamin Marzinski  *
1019b3b94faaSDavid Teigland  * Returns: errno
1020b3b94faaSDavid Teigland  */
1021b3b94faaSDavid Teigland 
1022b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1023b3b94faaSDavid Teigland {
1024b3b94faaSDavid Teigland 	log_refund(sdp, tr);
1025b3b94faaSDavid Teigland 
10265e687eacSBenjamin Marzinski 	if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
10275e687eacSBenjamin Marzinski 	    ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
10285e687eacSBenjamin Marzinski 	    atomic_read(&sdp->sd_log_thresh2)))
10295e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_logd_waitq);
1030faa31ce8SSteven Whitehouse }
1031b3b94faaSDavid Teigland 
1032b3b94faaSDavid Teigland /**
1033b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
1034b3b94faaSDavid Teigland  * @sdp: the filesystem
1035b3b94faaSDavid Teigland  *
1036b3b94faaSDavid Teigland  */
1037b3b94faaSDavid Teigland 
1038feed98a8SBob Peterson static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
1039b3b94faaSDavid Teigland {
1040b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
1041b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
1042b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1043b3b94faaSDavid Teigland 
1044b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
1045b3b94faaSDavid Teigland 
1046805c0907SBob Peterson 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
1047b3b94faaSDavid Teigland 
1048a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1049a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
1050b3b94faaSDavid Teigland 
1051b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
1052b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
1053a25311c8SSteven Whitehouse }
1054a25311c8SSteven Whitehouse 
10555e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
10565e687eacSBenjamin Marzinski {
1057f07b3520SBob Peterson 	return (atomic_read(&sdp->sd_log_pinned) +
1058f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_blks_needed) >=
1059f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh1));
10605e687eacSBenjamin Marzinski }
10615e687eacSBenjamin Marzinski 
10625e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
10635e687eacSBenjamin Marzinski {
10645e687eacSBenjamin Marzinski 	unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
1065b066a4eeSAbhi Das 
1066b066a4eeSAbhi Das 	if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
1067b066a4eeSAbhi Das 		return 1;
1068b066a4eeSAbhi Das 
1069f07b3520SBob Peterson 	return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
1070f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh2);
10715e687eacSBenjamin Marzinski }
1072ec69b188SSteven Whitehouse 
1073ec69b188SSteven Whitehouse /**
1074ec69b188SSteven Whitehouse  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
1075ec69b188SSteven Whitehouse  * @sdp: Pointer to GFS2 superblock
1076ec69b188SSteven Whitehouse  *
1077ec69b188SSteven Whitehouse  * Also, periodically check to make sure that we're using the most recent
1078ec69b188SSteven Whitehouse  * journal index.
1079ec69b188SSteven Whitehouse  */
1080ec69b188SSteven Whitehouse 
1081ec69b188SSteven Whitehouse int gfs2_logd(void *data)
1082ec69b188SSteven Whitehouse {
1083ec69b188SSteven Whitehouse 	struct gfs2_sbd *sdp = data;
10845e687eacSBenjamin Marzinski 	unsigned long t = 1;
10855e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
1086b63f5e84SBob Peterson 	bool did_flush;
1087ec69b188SSteven Whitehouse 
1088ec69b188SSteven Whitehouse 	while (!kthread_should_stop()) {
1089ec69b188SSteven Whitehouse 
1090942b0cddSBob Peterson 		/* Check for errors writing to the journal */
1091942b0cddSBob Peterson 		if (sdp->sd_log_error) {
1092badb55ecSAndreas Gruenbacher 			gfs2_lm(sdp,
1093942b0cddSBob Peterson 				"GFS2: fsid=%s: error %d: "
1094942b0cddSBob Peterson 				"withdrawing the file system to "
1095942b0cddSBob Peterson 				"prevent further damage.\n",
1096942b0cddSBob Peterson 				sdp->sd_fsname, sdp->sd_log_error);
1097badb55ecSAndreas Gruenbacher 			gfs2_withdraw(sdp);
1098942b0cddSBob Peterson 		}
1099942b0cddSBob Peterson 
1100b63f5e84SBob Peterson 		did_flush = false;
11015e687eacSBenjamin Marzinski 		if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
11025e4c7632SBob Peterson 			gfs2_ail1_empty(sdp, 0);
1103805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1104805c0907SBob Peterson 				       GFS2_LFC_LOGD_JFLUSH_REQD);
1105b63f5e84SBob Peterson 			did_flush = true;
1106ec69b188SSteven Whitehouse 		}
1107ec69b188SSteven Whitehouse 
11085e687eacSBenjamin Marzinski 		if (gfs2_ail_flush_reqd(sdp)) {
11095e687eacSBenjamin Marzinski 			gfs2_ail1_start(sdp);
111026b06a69SSteven Whitehouse 			gfs2_ail1_wait(sdp);
11115e4c7632SBob Peterson 			gfs2_ail1_empty(sdp, 0);
1112805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1113805c0907SBob Peterson 				       GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1114b63f5e84SBob Peterson 			did_flush = true;
11155e687eacSBenjamin Marzinski 		}
11165e687eacSBenjamin Marzinski 
1117b63f5e84SBob Peterson 		if (!gfs2_ail_flush_reqd(sdp) || did_flush)
11185e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_log_waitq);
111926b06a69SSteven Whitehouse 
1120ec69b188SSteven Whitehouse 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
1121a0acae0eSTejun Heo 
1122a0acae0eSTejun Heo 		try_to_freeze();
11235e687eacSBenjamin Marzinski 
11245e687eacSBenjamin Marzinski 		do {
11255e687eacSBenjamin Marzinski 			prepare_to_wait(&sdp->sd_logd_waitq, &wait,
11265f487490SSteven Whitehouse 					TASK_INTERRUPTIBLE);
11275e687eacSBenjamin Marzinski 			if (!gfs2_ail_flush_reqd(sdp) &&
11285e687eacSBenjamin Marzinski 			    !gfs2_jrnl_flush_reqd(sdp) &&
11295e687eacSBenjamin Marzinski 			    !kthread_should_stop())
11305e687eacSBenjamin Marzinski 				t = schedule_timeout(t);
11315e687eacSBenjamin Marzinski 		} while(t && !gfs2_ail_flush_reqd(sdp) &&
11325e687eacSBenjamin Marzinski 			!gfs2_jrnl_flush_reqd(sdp) &&
11335e687eacSBenjamin Marzinski 			!kthread_should_stop());
11345e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_logd_waitq, &wait);
1135ec69b188SSteven Whitehouse 	}
1136ec69b188SSteven Whitehouse 
1137ec69b188SSteven Whitehouse 	return 0;
1138ec69b188SSteven Whitehouse }
1139ec69b188SSteven Whitehouse 
1140