xref: /openbmc/linux/fs/gfs2/log.c (revision f4e2f5e1)
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 
1429592ea80SBob Peterson static void dump_ail_list(struct gfs2_sbd *sdp)
1439592ea80SBob Peterson {
1449592ea80SBob Peterson 	struct gfs2_trans *tr;
1459592ea80SBob Peterson 	struct gfs2_bufdata *bd;
1469592ea80SBob Peterson 	struct buffer_head *bh;
1479592ea80SBob Peterson 
1489592ea80SBob Peterson 	fs_err(sdp, "Error: In gfs2_ail1_flush for ten minutes! t=%d\n",
1499592ea80SBob Peterson 	       current->journal_info ? 1 : 0);
1509592ea80SBob Peterson 
1519592ea80SBob Peterson 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
1529592ea80SBob Peterson 		list_for_each_entry_reverse(bd, &tr->tr_ail1_list,
1539592ea80SBob Peterson 					    bd_ail_st_list) {
1549592ea80SBob Peterson 			bh = bd->bd_bh;
1559592ea80SBob Peterson 			fs_err(sdp, "bd %p: blk:0x%llx bh=%p ", bd,
1569592ea80SBob Peterson 			       (unsigned long long)bd->bd_blkno, bh);
1579592ea80SBob Peterson 			if (!bh) {
1589592ea80SBob Peterson 				fs_err(sdp, "\n");
1599592ea80SBob Peterson 				continue;
1609592ea80SBob Peterson 			}
1619592ea80SBob Peterson 			fs_err(sdp, "0x%llx up2:%d dirt:%d lkd:%d req:%d "
1629592ea80SBob Peterson 			       "map:%d new:%d ar:%d aw:%d delay:%d "
1639592ea80SBob Peterson 			       "io err:%d unwritten:%d dfr:%d pin:%d esc:%d\n",
1649592ea80SBob Peterson 			       (unsigned long long)bh->b_blocknr,
1659592ea80SBob Peterson 			       buffer_uptodate(bh), buffer_dirty(bh),
1669592ea80SBob Peterson 			       buffer_locked(bh), buffer_req(bh),
1679592ea80SBob Peterson 			       buffer_mapped(bh), buffer_new(bh),
1689592ea80SBob Peterson 			       buffer_async_read(bh), buffer_async_write(bh),
1699592ea80SBob Peterson 			       buffer_delay(bh), buffer_write_io_error(bh),
1709592ea80SBob Peterson 			       buffer_unwritten(bh),
1719592ea80SBob Peterson 			       buffer_defer_completion(bh),
1729592ea80SBob Peterson 			       buffer_pinned(bh), buffer_escaped(bh));
1739592ea80SBob Peterson 		}
1749592ea80SBob Peterson 	}
1759592ea80SBob Peterson }
1764667a0ecSSteven Whitehouse 
1774667a0ecSSteven Whitehouse /**
1784667a0ecSSteven Whitehouse  * gfs2_ail1_flush - start writeback of some ail1 entries
1794667a0ecSSteven Whitehouse  * @sdp: The super block
1804667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
1814667a0ecSSteven Whitehouse  *
1824667a0ecSSteven Whitehouse  * Writes back some ail1 entries, according to the limits in the
1834667a0ecSSteven Whitehouse  * writeback control structure
1844667a0ecSSteven Whitehouse  */
1854667a0ecSSteven Whitehouse 
1864667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
1874667a0ecSSteven Whitehouse {
1884667a0ecSSteven Whitehouse 	struct list_head *head = &sdp->sd_ail1_list;
18916ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
190885bcecaSSteven Whitehouse 	struct blk_plug plug;
19175b46c43SBob Peterson 	int ret;
1929592ea80SBob Peterson 	unsigned long flush_start = jiffies;
1934667a0ecSSteven Whitehouse 
194c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 1);
195885bcecaSSteven Whitehouse 	blk_start_plug(&plug);
1964667a0ecSSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
1974f1de018SSteven Whitehouse restart:
19875b46c43SBob Peterson 	ret = 0;
1999592ea80SBob Peterson 	if (time_after(jiffies, flush_start + (HZ * 600))) {
2009592ea80SBob Peterson 		dump_ail_list(sdp);
2019592ea80SBob Peterson 		goto out;
2029592ea80SBob Peterson 	}
20316ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, head, tr_list) {
2044667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
2054667a0ecSSteven Whitehouse 			break;
206b1676cbbSBob Peterson 		ret = gfs2_ail1_start_one(sdp, wbc, tr);
207b1676cbbSBob Peterson 		if (ret) {
208b1676cbbSBob Peterson 			if (ret == -EBUSY)
2094f1de018SSteven Whitehouse 				goto restart;
210b1676cbbSBob Peterson 			break;
211b1676cbbSBob Peterson 		}
2124667a0ecSSteven Whitehouse 	}
2139592ea80SBob Peterson out:
2144667a0ecSSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
215885bcecaSSteven Whitehouse 	blk_finish_plug(&plug);
21649003128SBob Peterson 	if (ret) {
21749003128SBob Peterson 		gfs2_lm(sdp, "gfs2_ail1_start_one (generic_writepages) "
21849003128SBob Peterson 			"returned: %d\n", ret);
219badb55ecSAndreas Gruenbacher 		gfs2_withdraw(sdp);
22049003128SBob Peterson 	}
221c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 0);
2224667a0ecSSteven Whitehouse }
2234667a0ecSSteven Whitehouse 
2244667a0ecSSteven Whitehouse /**
2254667a0ecSSteven Whitehouse  * gfs2_ail1_start - start writeback of all ail1 entries
2264667a0ecSSteven Whitehouse  * @sdp: The superblock
2274667a0ecSSteven Whitehouse  */
2284667a0ecSSteven Whitehouse 
2294667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp)
2304667a0ecSSteven Whitehouse {
2314667a0ecSSteven Whitehouse 	struct writeback_control wbc = {
2324667a0ecSSteven Whitehouse 		.sync_mode = WB_SYNC_NONE,
2334667a0ecSSteven Whitehouse 		.nr_to_write = LONG_MAX,
2344667a0ecSSteven Whitehouse 		.range_start = 0,
2354667a0ecSSteven Whitehouse 		.range_end = LLONG_MAX,
2364667a0ecSSteven Whitehouse 	};
2374667a0ecSSteven Whitehouse 
2384667a0ecSSteven Whitehouse 	return gfs2_ail1_flush(sdp, &wbc);
239ddacfaf7SSteven Whitehouse }
240ddacfaf7SSteven Whitehouse 
241ddacfaf7SSteven Whitehouse /**
242ddacfaf7SSteven Whitehouse  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
243ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
2445e4c7632SBob Peterson  * @tr: the transaction
2455e4c7632SBob Peterson  * @max_revokes: If nonzero, issue revokes for the bd items for written buffers
246ddacfaf7SSteven Whitehouse  *
247ddacfaf7SSteven Whitehouse  */
248ddacfaf7SSteven Whitehouse 
2495e4c7632SBob Peterson static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
2505e4c7632SBob Peterson 				int *max_revokes)
251ddacfaf7SSteven Whitehouse {
252ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
253ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
254ddacfaf7SSteven Whitehouse 
25516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
256ddacfaf7SSteven Whitehouse 					 bd_ail_st_list) {
257ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
25816ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
259036330c9SBob Peterson 		/*
260036330c9SBob Peterson 		 * If another process flagged an io error, e.g. writing to the
261036330c9SBob Peterson 		 * journal, error all other bhs and move them off the ail1 to
262036330c9SBob Peterson 		 * prevent a tight loop when unmount tries to flush ail1,
263036330c9SBob Peterson 		 * regardless of whether they're still busy. If no outside
264036330c9SBob Peterson 		 * errors were found and the buffer is busy, move to the next.
265036330c9SBob Peterson 		 * If the ail buffer is not busy and caught an error, flag it
266036330c9SBob Peterson 		 * for others.
267036330c9SBob Peterson 		 */
268036330c9SBob Peterson 		if (!sdp->sd_log_error && buffer_busy(bh))
269ddacfaf7SSteven Whitehouse 			continue;
270b524abccSBob Peterson 		if (!buffer_uptodate(bh) &&
271036330c9SBob Peterson 		    !cmpxchg(&sdp->sd_log_error, 0, -EIO)) {
272ddacfaf7SSteven Whitehouse 			gfs2_io_error_bh(sdp, bh);
27369511080SBob Peterson 			gfs2_withdraw_delayed(sdp);
2749e1a9ecdSAndreas Gruenbacher 		}
2755e4c7632SBob Peterson 		/*
2765e4c7632SBob Peterson 		 * If we have space for revokes and the bd is no longer on any
2775e4c7632SBob Peterson 		 * buf list, we can just add a revoke for it immediately and
2785e4c7632SBob Peterson 		 * avoid having to put it on the ail2 list, where it would need
2795e4c7632SBob Peterson 		 * to be revoked later.
2805e4c7632SBob Peterson 		 */
2815e4c7632SBob Peterson 		if (*max_revokes && list_empty(&bd->bd_list)) {
2825e4c7632SBob Peterson 			gfs2_add_revoke(sdp, bd);
2835e4c7632SBob Peterson 			(*max_revokes)--;
2845e4c7632SBob Peterson 			continue;
2855e4c7632SBob Peterson 		}
28616ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
287ddacfaf7SSteven Whitehouse 	}
288ddacfaf7SSteven Whitehouse }
289ddacfaf7SSteven Whitehouse 
2904667a0ecSSteven Whitehouse /**
2914667a0ecSSteven Whitehouse  * gfs2_ail1_empty - Try to empty the ail1 lists
2924667a0ecSSteven Whitehouse  * @sdp: The superblock
2935e4c7632SBob Peterson  * @max_revokes: If non-zero, add revokes where appropriate
2944667a0ecSSteven Whitehouse  *
2954667a0ecSSteven Whitehouse  * Tries to empty the ail1 lists, starting with the oldest first
2964667a0ecSSteven Whitehouse  */
297b3b94faaSDavid Teigland 
2985e4c7632SBob Peterson static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int max_revokes)
299b3b94faaSDavid Teigland {
30016ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *s;
3015d054964SBenjamin Marzinski 	int oldest_tr = 1;
302b3b94faaSDavid Teigland 	int ret;
303b3b94faaSDavid Teigland 
304d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
30516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
3065e4c7632SBob Peterson 		gfs2_ail1_empty_one(sdp, tr, &max_revokes);
3075d054964SBenjamin Marzinski 		if (list_empty(&tr->tr_ail1_list) && oldest_tr)
30816ca9412SBenjamin Marzinski 			list_move(&tr->tr_list, &sdp->sd_ail2_list);
3094667a0ecSSteven Whitehouse 		else
3105d054964SBenjamin Marzinski 			oldest_tr = 0;
311b3b94faaSDavid Teigland 	}
312b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
313d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
314b3b94faaSDavid Teigland 
31569511080SBob Peterson 	if (test_bit(SDF_WITHDRAWING, &sdp->sd_flags)) {
316badb55ecSAndreas Gruenbacher 		gfs2_lm(sdp, "fatal: I/O error(s)\n");
317badb55ecSAndreas Gruenbacher 		gfs2_withdraw(sdp);
318badb55ecSAndreas Gruenbacher 	}
3199e1a9ecdSAndreas Gruenbacher 
320b3b94faaSDavid Teigland 	return ret;
321b3b94faaSDavid Teigland }
322b3b94faaSDavid Teigland 
32326b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
32426b06a69SSteven Whitehouse {
32516ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
32626b06a69SSteven Whitehouse 	struct gfs2_bufdata *bd;
32726b06a69SSteven Whitehouse 	struct buffer_head *bh;
32826b06a69SSteven Whitehouse 
32926b06a69SSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
33016ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
33116ca9412SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
33226b06a69SSteven Whitehouse 			bh = bd->bd_bh;
33326b06a69SSteven Whitehouse 			if (!buffer_locked(bh))
33426b06a69SSteven Whitehouse 				continue;
33526b06a69SSteven Whitehouse 			get_bh(bh);
33626b06a69SSteven Whitehouse 			spin_unlock(&sdp->sd_ail_lock);
33726b06a69SSteven Whitehouse 			wait_on_buffer(bh);
33826b06a69SSteven Whitehouse 			brelse(bh);
33926b06a69SSteven Whitehouse 			return;
34026b06a69SSteven Whitehouse 		}
34126b06a69SSteven Whitehouse 	}
34226b06a69SSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
34326b06a69SSteven Whitehouse }
344ddacfaf7SSteven Whitehouse 
345ddacfaf7SSteven Whitehouse /**
3462ca0c2fbSBob Peterson  * gfs2_ail_empty_tr - empty one of the ail lists for a transaction
347ddacfaf7SSteven Whitehouse  */
348ddacfaf7SSteven Whitehouse 
3492ca0c2fbSBob Peterson static void gfs2_ail_empty_tr(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
3502ca0c2fbSBob Peterson 			      struct list_head *head)
351ddacfaf7SSteven Whitehouse {
352ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd;
353ddacfaf7SSteven Whitehouse 
354ddacfaf7SSteven Whitehouse 	while (!list_empty(head)) {
3552ca0c2fbSBob Peterson 		bd = list_first_entry(head, struct gfs2_bufdata,
356ddacfaf7SSteven Whitehouse 				      bd_ail_st_list);
35716ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
358f91a0d3eSSteven Whitehouse 		gfs2_remove_from_ail(bd);
359ddacfaf7SSteven Whitehouse 	}
360ddacfaf7SSteven Whitehouse }
361ddacfaf7SSteven Whitehouse 
362b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
363b3b94faaSDavid Teigland {
36416ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *safe;
365b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
366b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
367b3b94faaSDavid Teigland 	int a, b, rm;
368b3b94faaSDavid Teigland 
369d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
370b3b94faaSDavid Teigland 
37116ca9412SBenjamin Marzinski 	list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
37216ca9412SBenjamin Marzinski 		a = (old_tail <= tr->tr_first);
37316ca9412SBenjamin Marzinski 		b = (tr->tr_first < new_tail);
374b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
375b3b94faaSDavid Teigland 		if (!rm)
376b3b94faaSDavid Teigland 			continue;
377b3b94faaSDavid Teigland 
3782ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
37916ca9412SBenjamin Marzinski 		list_del(&tr->tr_list);
38016ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
38116ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
38216ca9412SBenjamin Marzinski 		kfree(tr);
383b3b94faaSDavid Teigland 	}
384b3b94faaSDavid Teigland 
385d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
386b3b94faaSDavid Teigland }
387b3b94faaSDavid Teigland 
388b3b94faaSDavid Teigland /**
38924972557SBenjamin Marzinski  * gfs2_log_release - Release a given number of log blocks
39024972557SBenjamin Marzinski  * @sdp: The GFS2 superblock
39124972557SBenjamin Marzinski  * @blks: The number of blocks
39224972557SBenjamin Marzinski  *
39324972557SBenjamin Marzinski  */
39424972557SBenjamin Marzinski 
39524972557SBenjamin Marzinski void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
39624972557SBenjamin Marzinski {
39724972557SBenjamin Marzinski 
39824972557SBenjamin Marzinski 	atomic_add(blks, &sdp->sd_log_blks_free);
39924972557SBenjamin Marzinski 	trace_gfs2_log_blocks(sdp, blks);
40024972557SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
40124972557SBenjamin Marzinski 				  sdp->sd_jdesc->jd_blocks);
40224972557SBenjamin Marzinski 	up_read(&sdp->sd_log_flush_lock);
40324972557SBenjamin Marzinski }
40424972557SBenjamin Marzinski 
40524972557SBenjamin Marzinski /**
406b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
407b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
408b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
409b3b94faaSDavid Teigland  *
41089918647SSteven Whitehouse  * Note that we never give out the last few blocks of the journal. Thats
4112332c443SRobert Peterson  * due to the fact that there is a small number of header blocks
412b004157aSSteven Whitehouse  * associated with each log flush. The exact number can't be known until
413b004157aSSteven Whitehouse  * flush time, so we ensure that we have just enough free blocks at all
414b004157aSSteven Whitehouse  * times to avoid running out during a log flush.
415b004157aSSteven Whitehouse  *
4165e687eacSBenjamin Marzinski  * We no longer flush the log here, instead we wake up logd to do that
4175e687eacSBenjamin Marzinski  * for us. To avoid the thundering herd and to ensure that we deal fairly
4185e687eacSBenjamin Marzinski  * with queued waiters, we use an exclusive wait. This means that when we
4195e687eacSBenjamin Marzinski  * get woken with enough journal space to get our reservation, we need to
4205e687eacSBenjamin Marzinski  * wake the next waiter on the list.
4215e687eacSBenjamin Marzinski  *
422b3b94faaSDavid Teigland  * Returns: errno
423b3b94faaSDavid Teigland  */
424b3b94faaSDavid Teigland 
425b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
426b3b94faaSDavid Teigland {
4272e60d768SBenjamin Marzinski 	int ret = 0;
4285d054964SBenjamin Marzinski 	unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
4295e687eacSBenjamin Marzinski 	unsigned wanted = blks + reserved_blks;
4305e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
4315e687eacSBenjamin Marzinski 	int did_wait = 0;
4325e687eacSBenjamin Marzinski 	unsigned int free_blocks;
433b3b94faaSDavid Teigland 
434b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
435b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
436b3b94faaSDavid Teigland 		return -EINVAL;
437f07b3520SBob Peterson 	atomic_add(blks, &sdp->sd_log_blks_needed);
4385e687eacSBenjamin Marzinski retry:
4395e687eacSBenjamin Marzinski 	free_blocks = atomic_read(&sdp->sd_log_blks_free);
4405e687eacSBenjamin Marzinski 	if (unlikely(free_blocks <= wanted)) {
4415e687eacSBenjamin Marzinski 		do {
4425e687eacSBenjamin Marzinski 			prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
4435e687eacSBenjamin Marzinski 					TASK_UNINTERRUPTIBLE);
4445e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_logd_waitq);
4455e687eacSBenjamin Marzinski 			did_wait = 1;
4465e687eacSBenjamin Marzinski 			if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
4475e687eacSBenjamin Marzinski 				io_schedule();
4485e687eacSBenjamin Marzinski 			free_blocks = atomic_read(&sdp->sd_log_blks_free);
4495e687eacSBenjamin Marzinski 		} while(free_blocks <= wanted);
4505e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_log_waitq, &wait);
451b3b94faaSDavid Teigland 	}
4522e60d768SBenjamin Marzinski 	atomic_inc(&sdp->sd_reserving_log);
4535e687eacSBenjamin Marzinski 	if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
4542e60d768SBenjamin Marzinski 				free_blocks - blks) != free_blocks) {
4552e60d768SBenjamin Marzinski 		if (atomic_dec_and_test(&sdp->sd_reserving_log))
4562e60d768SBenjamin Marzinski 			wake_up(&sdp->sd_reserving_log_wait);
4575e687eacSBenjamin Marzinski 		goto retry;
4582e60d768SBenjamin Marzinski 	}
459f07b3520SBob Peterson 	atomic_sub(blks, &sdp->sd_log_blks_needed);
46063997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, -blks);
4615e687eacSBenjamin Marzinski 
4625e687eacSBenjamin Marzinski 	/*
4635e687eacSBenjamin Marzinski 	 * If we waited, then so might others, wake them up _after_ we get
4645e687eacSBenjamin Marzinski 	 * our share of the log.
4655e687eacSBenjamin Marzinski 	 */
4665e687eacSBenjamin Marzinski 	if (unlikely(did_wait))
4675e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_log_waitq);
468484adff8SSteven Whitehouse 
469484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
47024972557SBenjamin Marzinski 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
47124972557SBenjamin Marzinski 		gfs2_log_release(sdp, blks);
4722e60d768SBenjamin Marzinski 		ret = -EROFS;
47324972557SBenjamin Marzinski 	}
4742e60d768SBenjamin Marzinski 	if (atomic_dec_and_test(&sdp->sd_reserving_log))
4752e60d768SBenjamin Marzinski 		wake_up(&sdp->sd_reserving_log_wait);
4762e60d768SBenjamin Marzinski 	return ret;
477b3b94faaSDavid Teigland }
478b3b94faaSDavid Teigland 
479b3b94faaSDavid Teigland /**
480b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
481b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
482b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
483b3b94faaSDavid Teigland  * @older: The older journal block of the pair
484b3b94faaSDavid Teigland  *
485b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
486b3b94faaSDavid Teigland  *   blocks in the journal
487b3b94faaSDavid Teigland  *
488b3b94faaSDavid Teigland  * Returns: the distance in blocks
489b3b94faaSDavid Teigland  */
490b3b94faaSDavid Teigland 
491faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
492b3b94faaSDavid Teigland 					unsigned int older)
493b3b94faaSDavid Teigland {
494b3b94faaSDavid Teigland 	int dist;
495b3b94faaSDavid Teigland 
496b3b94faaSDavid Teigland 	dist = newer - older;
497b3b94faaSDavid Teigland 	if (dist < 0)
498b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
499b3b94faaSDavid Teigland 
500b3b94faaSDavid Teigland 	return dist;
501b3b94faaSDavid Teigland }
502b3b94faaSDavid Teigland 
5032332c443SRobert Peterson /**
5042332c443SRobert Peterson  * calc_reserved - Calculate the number of blocks to reserve when
5052332c443SRobert Peterson  *                 refunding a transaction's unused buffers.
5062332c443SRobert Peterson  * @sdp: The GFS2 superblock
5072332c443SRobert Peterson  *
5082332c443SRobert Peterson  * This is complex.  We need to reserve room for all our currently used
5092332c443SRobert Peterson  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
5102332c443SRobert Peterson  * all our journaled data buffers for journaled files (e.g. files in the
5112332c443SRobert Peterson  * meta_fs like rindex, or files for which chattr +j was done.)
5122332c443SRobert Peterson  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
5132332c443SRobert Peterson  * will count it as free space (sd_log_blks_free) and corruption will follow.
5142332c443SRobert Peterson  *
5152332c443SRobert Peterson  * We can have metadata bufs and jdata bufs in the same journal.  So each
5162332c443SRobert Peterson  * type gets its own log header, for which we need to reserve a block.
5172332c443SRobert Peterson  * In fact, each type has the potential for needing more than one header
5182332c443SRobert Peterson  * in cases where we have more buffers than will fit on a journal page.
5192332c443SRobert Peterson  * Metadata journal entries take up half the space of journaled buffer entries.
5202332c443SRobert Peterson  * Thus, metadata entries have buf_limit (502) and journaled buffers have
5212332c443SRobert Peterson  * databuf_limit (251) before they cause a wrap around.
5222332c443SRobert Peterson  *
5232332c443SRobert Peterson  * Also, we need to reserve blocks for revoke journal entries and one for an
5242332c443SRobert Peterson  * overall header for the lot.
5252332c443SRobert Peterson  *
5262332c443SRobert Peterson  * Returns: the number of blocks reserved
5272332c443SRobert Peterson  */
5282332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp)
5292332c443SRobert Peterson {
5302332c443SRobert Peterson 	unsigned int reserved = 0;
531022ef4feSSteven Whitehouse 	unsigned int mbuf;
532022ef4feSSteven Whitehouse 	unsigned int dbuf;
533022ef4feSSteven Whitehouse 	struct gfs2_trans *tr = sdp->sd_log_tr;
5342332c443SRobert Peterson 
535022ef4feSSteven Whitehouse 	if (tr) {
536022ef4feSSteven Whitehouse 		mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
537022ef4feSSteven Whitehouse 		dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
538022ef4feSSteven Whitehouse 		reserved = mbuf + dbuf;
539022ef4feSSteven Whitehouse 		/* Account for header blocks */
540022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
541022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
542022ef4feSSteven Whitehouse 	}
5432332c443SRobert Peterson 
5445d439758SAndreas Gruenbacher 	if (sdp->sd_log_committed_revoke > 0)
5455d439758SAndreas Gruenbacher 		reserved += gfs2_struct2blk(sdp, sdp->sd_log_committed_revoke);
5462332c443SRobert Peterson 	/* One for the overall header */
5472332c443SRobert Peterson 	if (reserved)
5482332c443SRobert Peterson 		reserved++;
5492332c443SRobert Peterson 	return reserved;
5502332c443SRobert Peterson }
5512332c443SRobert Peterson 
552b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
553b3b94faaSDavid Teigland {
55416ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
555b3b94faaSDavid Teigland 	unsigned int tail;
556b3b94faaSDavid Teigland 
557d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
558b3b94faaSDavid Teigland 
559faa31ce8SSteven Whitehouse 	if (list_empty(&sdp->sd_ail1_list)) {
560b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
561faa31ce8SSteven Whitehouse 	} else {
562969183bcSAndreas Gruenbacher 		tr = list_last_entry(&sdp->sd_ail1_list, struct gfs2_trans,
56316ca9412SBenjamin Marzinski 				tr_list);
56416ca9412SBenjamin Marzinski 		tail = tr->tr_first;
565b3b94faaSDavid Teigland 	}
566b3b94faaSDavid Teigland 
567d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
568b3b94faaSDavid Teigland 
569b3b94faaSDavid Teigland 	return tail;
570b3b94faaSDavid Teigland }
571b3b94faaSDavid Teigland 
5722332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
573b3b94faaSDavid Teigland {
574b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
575b3b94faaSDavid Teigland 
576b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
577b3b94faaSDavid Teigland 
578fd041f0bSSteven Whitehouse 	atomic_add(dist, &sdp->sd_log_blks_free);
57963997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, dist);
5805e687eacSBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
5815e687eacSBenjamin Marzinski 			     sdp->sd_jdesc->jd_blocks);
582b3b94faaSDavid Teigland 
583b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
584b3b94faaSDavid Teigland }
585b3b94faaSDavid Teigland 
586b3b94faaSDavid Teigland 
5879ff78289SBob Peterson void log_flush_wait(struct gfs2_sbd *sdp)
588b3b94faaSDavid Teigland {
58916615be1SSteven Whitehouse 	DEFINE_WAIT(wait);
590b3b94faaSDavid Teigland 
59116615be1SSteven Whitehouse 	if (atomic_read(&sdp->sd_log_in_flight)) {
59216615be1SSteven Whitehouse 		do {
59316615be1SSteven Whitehouse 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
59416615be1SSteven Whitehouse 					TASK_UNINTERRUPTIBLE);
59516615be1SSteven Whitehouse 			if (atomic_read(&sdp->sd_log_in_flight))
59616615be1SSteven Whitehouse 				io_schedule();
59716615be1SSteven Whitehouse 		} while(atomic_read(&sdp->sd_log_in_flight));
59816615be1SSteven Whitehouse 		finish_wait(&sdp->sd_log_flush_wait, &wait);
599b3b94faaSDavid Teigland 	}
600b3b94faaSDavid Teigland }
601b3b94faaSDavid Teigland 
60245138990SSteven Whitehouse static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
6034a36d08dSBob Peterson {
60445138990SSteven Whitehouse 	struct gfs2_inode *ipa, *ipb;
6054a36d08dSBob Peterson 
60645138990SSteven Whitehouse 	ipa = list_entry(a, struct gfs2_inode, i_ordered);
60745138990SSteven Whitehouse 	ipb = list_entry(b, struct gfs2_inode, i_ordered);
6084a36d08dSBob Peterson 
60945138990SSteven Whitehouse 	if (ipa->i_no_addr < ipb->i_no_addr)
6104a36d08dSBob Peterson 		return -1;
61145138990SSteven Whitehouse 	if (ipa->i_no_addr > ipb->i_no_addr)
6124a36d08dSBob Peterson 		return 1;
6134a36d08dSBob Peterson 	return 0;
6144a36d08dSBob Peterson }
6154a36d08dSBob Peterson 
616d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp)
617d7b616e2SSteven Whitehouse {
61845138990SSteven Whitehouse 	struct gfs2_inode *ip;
619d7b616e2SSteven Whitehouse 	LIST_HEAD(written);
620d7b616e2SSteven Whitehouse 
62145138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
622a5b1d3fcSAndreas Gruenbacher 	list_sort(NULL, &sdp->sd_log_ordered, &ip_cmp);
623a5b1d3fcSAndreas Gruenbacher 	while (!list_empty(&sdp->sd_log_ordered)) {
624969183bcSAndreas Gruenbacher 		ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
6251f23bc78SAbhi Das 		if (ip->i_inode.i_mapping->nrpages == 0) {
6261f23bc78SAbhi Das 			test_and_clear_bit(GIF_ORDERED, &ip->i_flags);
6271f23bc78SAbhi Das 			list_del(&ip->i_ordered);
628d7b616e2SSteven Whitehouse 			continue;
6291f23bc78SAbhi Das 		}
6301f23bc78SAbhi Das 		list_move(&ip->i_ordered, &written);
63145138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
63245138990SSteven Whitehouse 		filemap_fdatawrite(ip->i_inode.i_mapping);
63345138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
634d7b616e2SSteven Whitehouse 	}
635a5b1d3fcSAndreas Gruenbacher 	list_splice(&written, &sdp->sd_log_ordered);
63645138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
637d7b616e2SSteven Whitehouse }
638d7b616e2SSteven Whitehouse 
639d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
640d7b616e2SSteven Whitehouse {
64145138990SSteven Whitehouse 	struct gfs2_inode *ip;
642d7b616e2SSteven Whitehouse 
64345138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
644a5b1d3fcSAndreas Gruenbacher 	while (!list_empty(&sdp->sd_log_ordered)) {
645969183bcSAndreas Gruenbacher 		ip = list_first_entry(&sdp->sd_log_ordered, struct gfs2_inode, i_ordered);
64645138990SSteven Whitehouse 		list_del(&ip->i_ordered);
64745138990SSteven Whitehouse 		WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
64845138990SSteven Whitehouse 		if (ip->i_inode.i_mapping->nrpages == 0)
649d7b616e2SSteven Whitehouse 			continue;
65045138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
65145138990SSteven Whitehouse 		filemap_fdatawait(ip->i_inode.i_mapping);
65245138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
653d7b616e2SSteven Whitehouse 	}
65445138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
655d7b616e2SSteven Whitehouse }
65645138990SSteven Whitehouse 
65745138990SSteven Whitehouse void gfs2_ordered_del_inode(struct gfs2_inode *ip)
65845138990SSteven Whitehouse {
65945138990SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
66045138990SSteven Whitehouse 
66145138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
66245138990SSteven Whitehouse 	if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
66345138990SSteven Whitehouse 		list_del(&ip->i_ordered);
66445138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
665d7b616e2SSteven Whitehouse }
666d7b616e2SSteven Whitehouse 
6675d054964SBenjamin Marzinski void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
6685d054964SBenjamin Marzinski {
6695d054964SBenjamin Marzinski 	struct buffer_head *bh = bd->bd_bh;
6705d054964SBenjamin Marzinski 	struct gfs2_glock *gl = bd->bd_gl;
6715d054964SBenjamin Marzinski 
672f4e2f5e1SAndreas Gruenbacher 	sdp->sd_log_num_revoke++;
673f4e2f5e1SAndreas Gruenbacher 	if (atomic_inc_return(&gl->gl_revokes) == 1)
674f4e2f5e1SAndreas Gruenbacher 		gfs2_glock_hold(gl);
6755d054964SBenjamin Marzinski 	bh->b_private = NULL;
6765d054964SBenjamin Marzinski 	bd->bd_blkno = bh->b_blocknr;
6779290a9a7SBob Peterson 	gfs2_remove_from_ail(bd); /* drops ref on bh */
6789290a9a7SBob Peterson 	bd->bd_bh = NULL;
6795d054964SBenjamin Marzinski 	set_bit(GLF_LFLUSH, &gl->gl_flags);
680a5b1d3fcSAndreas Gruenbacher 	list_add(&bd->bd_list, &sdp->sd_log_revokes);
6815d054964SBenjamin Marzinski }
6825d054964SBenjamin Marzinski 
683fe5e7ba1SBob Peterson void gfs2_glock_remove_revoke(struct gfs2_glock *gl)
684fe5e7ba1SBob Peterson {
685fe5e7ba1SBob Peterson 	if (atomic_dec_return(&gl->gl_revokes) == 0) {
686fe5e7ba1SBob Peterson 		clear_bit(GLF_LFLUSH, &gl->gl_flags);
687fe5e7ba1SBob Peterson 		gfs2_glock_queue_put(gl);
688fe5e7ba1SBob Peterson 	}
689fe5e7ba1SBob Peterson }
690fe5e7ba1SBob Peterson 
6915e4c7632SBob Peterson /**
6925e4c7632SBob Peterson  * gfs2_write_revokes - Add as many revokes to the system transaction as we can
6935e4c7632SBob Peterson  * @sdp: The GFS2 superblock
6945e4c7632SBob Peterson  *
6955e4c7632SBob Peterson  * Our usual strategy is to defer writing revokes as much as we can in the hope
6965e4c7632SBob Peterson  * that we'll eventually overwrite the journal, which will make those revokes
6975e4c7632SBob Peterson  * go away.  This changes when we flush the log: at that point, there will
6985e4c7632SBob Peterson  * likely be some left-over space in the last revoke block of that transaction.
6995e4c7632SBob Peterson  * We can fill that space with additional revokes for blocks that have already
7005e4c7632SBob Peterson  * been written back.  This will basically come at no cost now, and will save
7015e4c7632SBob Peterson  * us from having to keep track of those blocks on the AIL2 list later.
7025e4c7632SBob Peterson  */
7035d054964SBenjamin Marzinski void gfs2_write_revokes(struct gfs2_sbd *sdp)
7045d054964SBenjamin Marzinski {
7055e4c7632SBob Peterson 	/* number of revokes we still have room for */
7065d054964SBenjamin Marzinski 	int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
7075d054964SBenjamin Marzinski 
7085e4c7632SBob Peterson 	gfs2_log_lock(sdp);
7095d054964SBenjamin Marzinski 	while (sdp->sd_log_num_revoke > max_revokes)
7105d054964SBenjamin Marzinski 		max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
7115d054964SBenjamin Marzinski 	max_revokes -= sdp->sd_log_num_revoke;
7125d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
7135d054964SBenjamin Marzinski 		atomic_dec(&sdp->sd_log_blks_free);
7145d054964SBenjamin Marzinski 		/* If no blocks have been reserved, we need to also
7155d054964SBenjamin Marzinski 		 * reserve a block for the header */
7165d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
7175d054964SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free);
7185d054964SBenjamin Marzinski 	}
7195e4c7632SBob Peterson 	gfs2_ail1_empty(sdp, max_revokes);
7205d054964SBenjamin Marzinski 	gfs2_log_unlock(sdp);
7215d054964SBenjamin Marzinski 
7225d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
7235d054964SBenjamin Marzinski 		atomic_inc(&sdp->sd_log_blks_free);
7245d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
7255d054964SBenjamin Marzinski 			atomic_inc(&sdp->sd_log_blks_free);
7265d054964SBenjamin Marzinski 	}
7275d054964SBenjamin Marzinski }
7285d054964SBenjamin Marzinski 
729b3b94faaSDavid Teigland /**
7307c70b896SBob Peterson  * gfs2_write_log_header - Write a journal log header buffer at lblock
731588bff95SBob Peterson  * @sdp: The GFS2 superblock
732c1696fb8SBob Peterson  * @jd: journal descriptor of the journal to which we are writing
733588bff95SBob Peterson  * @seq: sequence number
734588bff95SBob Peterson  * @tail: tail of the log
7357c70b896SBob Peterson  * @lblock: value for lh_blkno (block number relative to start of journal)
736c1696fb8SBob Peterson  * @flags: log header flags GFS2_LOG_HEAD_*
737588bff95SBob Peterson  * @op_flags: flags to pass to the bio
738588bff95SBob Peterson  *
739588bff95SBob Peterson  * Returns: the initialized log buffer descriptor
740588bff95SBob Peterson  */
741588bff95SBob Peterson 
742c1696fb8SBob Peterson void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
7437c70b896SBob Peterson 			   u64 seq, u32 tail, u32 lblock, u32 flags,
7447c70b896SBob Peterson 			   int op_flags)
745588bff95SBob Peterson {
746588bff95SBob Peterson 	struct gfs2_log_header *lh;
747c1696fb8SBob Peterson 	u32 hash, crc;
748ade48088SBob Peterson 	struct page *page;
749c1696fb8SBob Peterson 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
750c1696fb8SBob Peterson 	struct timespec64 tv;
751c1696fb8SBob Peterson 	struct super_block *sb = sdp->sd_vfs;
7527c70b896SBob Peterson 	u64 dblock;
753588bff95SBob Peterson 
754ade48088SBob Peterson 	if (gfs2_withdrawn(sdp))
755ade48088SBob Peterson 		goto out;
756ade48088SBob Peterson 
757ade48088SBob Peterson 	page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
758588bff95SBob Peterson 	lh = page_address(page);
759588bff95SBob Peterson 	clear_page(lh);
760588bff95SBob Peterson 
761588bff95SBob Peterson 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
762588bff95SBob Peterson 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
763588bff95SBob Peterson 	lh->lh_header.__pad0 = cpu_to_be64(0);
764588bff95SBob Peterson 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
765588bff95SBob Peterson 	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
766588bff95SBob Peterson 	lh->lh_sequence = cpu_to_be64(seq);
767588bff95SBob Peterson 	lh->lh_flags = cpu_to_be32(flags);
768588bff95SBob Peterson 	lh->lh_tail = cpu_to_be32(tail);
7697c70b896SBob Peterson 	lh->lh_blkno = cpu_to_be32(lblock);
770c1696fb8SBob Peterson 	hash = ~crc32(~0, lh, LH_V1_SIZE);
771588bff95SBob Peterson 	lh->lh_hash = cpu_to_be32(hash);
772588bff95SBob Peterson 
773ee9c7f9aSArnd Bergmann 	ktime_get_coarse_real_ts64(&tv);
774c1696fb8SBob Peterson 	lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
775c1696fb8SBob Peterson 	lh->lh_sec = cpu_to_be64(tv.tv_sec);
7767c70b896SBob Peterson 	if (!list_empty(&jd->extent_list))
77719ebc050SAndreas Gruenbacher 		dblock = gfs2_log_bmap(jd, lblock);
7787c70b896SBob Peterson 	else {
7797c70b896SBob Peterson 		int ret = gfs2_lblk_to_dblk(jd->jd_inode, lblock, &dblock);
7807c70b896SBob Peterson 		if (gfs2_assert_withdraw(sdp, ret == 0))
7817c70b896SBob Peterson 			return;
7827c70b896SBob Peterson 	}
7837c70b896SBob Peterson 	lh->lh_addr = cpu_to_be64(dblock);
784c1696fb8SBob Peterson 	lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
785c1696fb8SBob Peterson 
786c1696fb8SBob Peterson 	/* We may only write local statfs, quota, etc., when writing to our
787c1696fb8SBob Peterson 	   own journal. The values are left 0 when recovering a journal
788c1696fb8SBob Peterson 	   different from our own. */
789c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
790c1696fb8SBob Peterson 		lh->lh_statfs_addr =
791c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
792c1696fb8SBob Peterson 		lh->lh_quota_addr =
793c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
794c1696fb8SBob Peterson 
795c1696fb8SBob Peterson 		spin_lock(&sdp->sd_statfs_spin);
796c1696fb8SBob Peterson 		lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
797c1696fb8SBob Peterson 		lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
798c1696fb8SBob Peterson 		lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
799c1696fb8SBob Peterson 		spin_unlock(&sdp->sd_statfs_spin);
800c1696fb8SBob Peterson 	}
801c1696fb8SBob Peterson 
802c1696fb8SBob Peterson 	BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
803c1696fb8SBob Peterson 
804c1696fb8SBob Peterson 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
805c1696fb8SBob Peterson 		     sb->s_blocksize - LH_V1_SIZE - 4);
806c1696fb8SBob Peterson 	lh->lh_crc = cpu_to_be32(crc);
807c1696fb8SBob Peterson 
8087c70b896SBob Peterson 	gfs2_log_write(sdp, page, sb->s_blocksize, 0, dblock);
809f4686c26SAbhi Das 	gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE | op_flags);
810ade48088SBob Peterson out:
811588bff95SBob Peterson 	log_flush_wait(sdp);
812588bff95SBob Peterson }
813588bff95SBob Peterson 
814588bff95SBob Peterson /**
81534cc1781SSteven Whitehouse  * log_write_header - Get and initialize a journal header buffer
81634cc1781SSteven Whitehouse  * @sdp: The GFS2 superblock
817c1696fb8SBob Peterson  * @flags: The log header flags, including log header origin
81834cc1781SSteven Whitehouse  *
81934cc1781SSteven Whitehouse  * Returns: the initialized log buffer descriptor
82034cc1781SSteven Whitehouse  */
82134cc1781SSteven Whitehouse 
822fdb76a42SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
82334cc1781SSteven Whitehouse {
82434cc1781SSteven Whitehouse 	unsigned int tail;
8250f0b9b63SJan Kara 	int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
8262e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
82734cc1781SSteven Whitehouse 
8282e60d768SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
82934cc1781SSteven Whitehouse 	tail = current_tail(sdp);
83034cc1781SSteven Whitehouse 
83134cc1781SSteven Whitehouse 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
83234cc1781SSteven Whitehouse 		gfs2_ordered_wait(sdp);
83334cc1781SSteven Whitehouse 		log_flush_wait(sdp);
83470fd7614SChristoph Hellwig 		op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
83534cc1781SSteven Whitehouse 	}
836e8c92ed7SSteven Whitehouse 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
837c1696fb8SBob Peterson 	gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail,
8387c70b896SBob Peterson 			      sdp->sd_log_flush_head, flags, op_flags);
83919ebc050SAndreas Gruenbacher 	gfs2_log_incr_head(sdp);
84034cc1781SSteven Whitehouse 
84134cc1781SSteven Whitehouse 	if (sdp->sd_log_tail != tail)
84234cc1781SSteven Whitehouse 		log_pull_tail(sdp, tail);
84334cc1781SSteven Whitehouse }
84434cc1781SSteven Whitehouse 
84534cc1781SSteven Whitehouse /**
8462ca0c2fbSBob Peterson  * ail_drain - drain the ail lists after a withdraw
8472ca0c2fbSBob Peterson  * @sdp: Pointer to GFS2 superblock
8482ca0c2fbSBob Peterson  */
8492ca0c2fbSBob Peterson static void ail_drain(struct gfs2_sbd *sdp)
8502ca0c2fbSBob Peterson {
8512ca0c2fbSBob Peterson 	struct gfs2_trans *tr;
8522ca0c2fbSBob Peterson 
8532ca0c2fbSBob Peterson 	spin_lock(&sdp->sd_ail_lock);
8542ca0c2fbSBob Peterson 	/*
8552ca0c2fbSBob Peterson 	 * For transactions on the sd_ail1_list we need to drain both the
8562ca0c2fbSBob Peterson 	 * ail1 and ail2 lists. That's because function gfs2_ail1_start_one
8572ca0c2fbSBob Peterson 	 * (temporarily) moves items from its tr_ail1 list to tr_ail2 list
8582ca0c2fbSBob Peterson 	 * before revokes are sent for that block. Items on the sd_ail2_list
8592ca0c2fbSBob Peterson 	 * should have already gotten beyond that point, so no need.
8602ca0c2fbSBob Peterson 	 */
8612ca0c2fbSBob Peterson 	while (!list_empty(&sdp->sd_ail1_list)) {
8622ca0c2fbSBob Peterson 		tr = list_first_entry(&sdp->sd_ail1_list, struct gfs2_trans,
8632ca0c2fbSBob Peterson 				      tr_list);
8642ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail1_list);
8652ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
8662ca0c2fbSBob Peterson 		list_del(&tr->tr_list);
8672ca0c2fbSBob Peterson 		kfree(tr);
8682ca0c2fbSBob Peterson 	}
8692ca0c2fbSBob Peterson 	while (!list_empty(&sdp->sd_ail2_list)) {
8702ca0c2fbSBob Peterson 		tr = list_first_entry(&sdp->sd_ail2_list, struct gfs2_trans,
8712ca0c2fbSBob Peterson 				      tr_list);
8722ca0c2fbSBob Peterson 		gfs2_ail_empty_tr(sdp, tr, &tr->tr_ail2_list);
8732ca0c2fbSBob Peterson 		list_del(&tr->tr_list);
8742ca0c2fbSBob Peterson 		kfree(tr);
8752ca0c2fbSBob Peterson 	}
8762ca0c2fbSBob Peterson 	spin_unlock(&sdp->sd_ail_lock);
8772ca0c2fbSBob Peterson }
8782ca0c2fbSBob Peterson 
8792ca0c2fbSBob Peterson /**
880b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
881b3b94faaSDavid Teigland  * @sdp: the filesystem
882b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
883805c0907SBob Peterson  * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
884b3b94faaSDavid Teigland  *
885b3b94faaSDavid Teigland  */
886b3b94faaSDavid Teigland 
887c1696fb8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
888b3b94faaSDavid Teigland {
8892ca0c2fbSBob Peterson 	struct gfs2_trans *tr = NULL;
8902e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
891b3b94faaSDavid Teigland 
892484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
893f55ab26aSSteven Whitehouse 
8942ca0c2fbSBob Peterson 	/*
8952ca0c2fbSBob Peterson 	 * Do this check while holding the log_flush_lock to prevent new
8962ca0c2fbSBob Peterson 	 * buffers from being added to the ail via gfs2_pin()
8972ca0c2fbSBob Peterson 	 */
8982ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
8992ca0c2fbSBob Peterson 		goto out;
9002ca0c2fbSBob Peterson 
9012bcd610dSSteven Whitehouse 	/* Log might have been flushed while we waited for the flush lock */
9022bcd610dSSteven Whitehouse 	if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
903484adff8SSteven Whitehouse 		up_write(&sdp->sd_log_flush_lock);
904f55ab26aSSteven Whitehouse 		return;
905f55ab26aSSteven Whitehouse 	}
906805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 1, flags);
907f55ab26aSSteven Whitehouse 
908c1696fb8SBob Peterson 	if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
909400ac52eSBenjamin Marzinski 		clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
910400ac52eSBenjamin Marzinski 
911b1ab1e44SSteven Whitehouse 	sdp->sd_log_flush_head = sdp->sd_log_head;
91216ca9412SBenjamin Marzinski 	tr = sdp->sd_log_tr;
91316ca9412SBenjamin Marzinski 	if (tr) {
91416ca9412SBenjamin Marzinski 		sdp->sd_log_tr = NULL;
91516ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail1_list);
91616ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail2_list);
917b1ab1e44SSteven Whitehouse 		tr->tr_first = sdp->sd_log_flush_head;
9182e60d768SBenjamin Marzinski 		if (unlikely (state == SFS_FROZEN))
919ca399c96SBob Peterson 			if (gfs2_assert_withdraw_delayed(sdp,
920ca399c96SBob Peterson 			       !tr->tr_num_buf_new && !tr->tr_num_databuf_new))
921ca399c96SBob Peterson 				goto out;
92216ca9412SBenjamin Marzinski 	}
923b3b94faaSDavid Teigland 
9242e60d768SBenjamin Marzinski 	if (unlikely(state == SFS_FROZEN))
925ca399c96SBob Peterson 		if (gfs2_assert_withdraw_delayed(sdp, !sdp->sd_log_num_revoke))
926ca399c96SBob Peterson 			goto out;
927ca399c96SBob Peterson 	if (gfs2_assert_withdraw_delayed(sdp,
928ca399c96SBob Peterson 			sdp->sd_log_num_revoke == sdp->sd_log_committed_revoke))
929ca399c96SBob Peterson 		goto out;
930b3b94faaSDavid Teigland 
931d7b616e2SSteven Whitehouse 	gfs2_ordered_write(sdp);
9322ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
9332ca0c2fbSBob Peterson 		goto out;
934d69a3c65SSteven Whitehouse 	lops_before_commit(sdp, tr);
9352ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
9362ca0c2fbSBob Peterson 		goto out;
937f4686c26SAbhi Das 	gfs2_log_submit_bio(&sdp->sd_log_bio, REQ_OP_WRITE);
9382ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
9392ca0c2fbSBob Peterson 		goto out;
940d7b616e2SSteven Whitehouse 
94134cc1781SSteven Whitehouse 	if (sdp->sd_log_head != sdp->sd_log_flush_head) {
942428fd95dSBob Peterson 		log_flush_wait(sdp);
943c1696fb8SBob Peterson 		log_write_header(sdp, flags);
94434cc1781SSteven Whitehouse 	} else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
945fd041f0bSSteven Whitehouse 		atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
94663997775SSteven Whitehouse 		trace_gfs2_log_blocks(sdp, -1);
947c1696fb8SBob Peterson 		log_write_header(sdp, flags);
9482332c443SRobert Peterson 	}
9492ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp))
9502ca0c2fbSBob Peterson 		goto out;
95116ca9412SBenjamin Marzinski 	lops_after_commit(sdp, tr);
952fe1a698fSSteven Whitehouse 
953fe1a698fSSteven Whitehouse 	gfs2_log_lock(sdp);
954b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
955faa31ce8SSteven Whitehouse 	sdp->sd_log_blks_reserved = 0;
9565d439758SAndreas Gruenbacher 	sdp->sd_log_committed_revoke = 0;
957b3b94faaSDavid Teigland 
958d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
95916ca9412SBenjamin Marzinski 	if (tr && !list_empty(&tr->tr_ail1_list)) {
96016ca9412SBenjamin Marzinski 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
96116ca9412SBenjamin Marzinski 		tr = NULL;
962b3b94faaSDavid Teigland 	}
963d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
964b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
96524972557SBenjamin Marzinski 
966c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
96724972557SBenjamin Marzinski 		if (!sdp->sd_log_idle) {
96824972557SBenjamin Marzinski 			for (;;) {
96924972557SBenjamin Marzinski 				gfs2_ail1_start(sdp);
97024972557SBenjamin Marzinski 				gfs2_ail1_wait(sdp);
9715e4c7632SBob Peterson 				if (gfs2_ail1_empty(sdp, 0))
97224972557SBenjamin Marzinski 					break;
97324972557SBenjamin Marzinski 			}
97430fe70a8SBob Peterson 			if (gfs2_withdrawn(sdp))
97530fe70a8SBob Peterson 				goto out;
97624972557SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
97724972557SBenjamin Marzinski 			trace_gfs2_log_blocks(sdp, -1);
978c1696fb8SBob Peterson 			log_write_header(sdp, flags);
97924972557SBenjamin Marzinski 			sdp->sd_log_head = sdp->sd_log_flush_head;
98024972557SBenjamin Marzinski 		}
981c1696fb8SBob Peterson 		if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
982c1696fb8SBob Peterson 			     GFS2_LOG_HEAD_FLUSH_FREEZE))
98324972557SBenjamin Marzinski 			gfs2_log_shutdown(sdp);
984c1696fb8SBob Peterson 		if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
9852e60d768SBenjamin Marzinski 			atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
98624972557SBenjamin Marzinski 	}
98724972557SBenjamin Marzinski 
98830fe70a8SBob Peterson out:
9892ca0c2fbSBob Peterson 	if (gfs2_withdrawn(sdp)) {
9902ca0c2fbSBob Peterson 		ail_drain(sdp); /* frees all transactions */
9912ca0c2fbSBob Peterson 		tr = NULL;
9922ca0c2fbSBob Peterson 	}
9932ca0c2fbSBob Peterson 
994805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 0, flags);
995484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
996b3b94faaSDavid Teigland 
99716ca9412SBenjamin Marzinski 	kfree(tr);
998b3b94faaSDavid Teigland }
999b3b94faaSDavid Teigland 
1000d69a3c65SSteven Whitehouse /**
1001d69a3c65SSteven Whitehouse  * gfs2_merge_trans - Merge a new transaction into a cached transaction
1002d69a3c65SSteven Whitehouse  * @old: Original transaction to be expanded
1003d69a3c65SSteven Whitehouse  * @new: New transaction to be merged
1004d69a3c65SSteven Whitehouse  */
1005d69a3c65SSteven Whitehouse 
1006d69a3c65SSteven Whitehouse static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
1007d69a3c65SSteven Whitehouse {
10089862ca05SBob Peterson 	WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
1009d69a3c65SSteven Whitehouse 
1010d69a3c65SSteven Whitehouse 	old->tr_num_buf_new	+= new->tr_num_buf_new;
1011d69a3c65SSteven Whitehouse 	old->tr_num_databuf_new	+= new->tr_num_databuf_new;
1012d69a3c65SSteven Whitehouse 	old->tr_num_buf_rm	+= new->tr_num_buf_rm;
1013d69a3c65SSteven Whitehouse 	old->tr_num_databuf_rm	+= new->tr_num_databuf_rm;
1014d69a3c65SSteven Whitehouse 	old->tr_num_revoke	+= new->tr_num_revoke;
1015a31b4ec5SBob Peterson 	old->tr_num_revoke_rm	+= new->tr_num_revoke_rm;
1016d69a3c65SSteven Whitehouse 
1017d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
1018d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_buf, &old->tr_buf);
1019d69a3c65SSteven Whitehouse }
1020d69a3c65SSteven Whitehouse 
1021b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1022b3b94faaSDavid Teigland {
10232332c443SRobert Peterson 	unsigned int reserved;
1024ac39aaddSSteven Whitehouse 	unsigned int unused;
1025022ef4feSSteven Whitehouse 	unsigned int maxres;
1026b3b94faaSDavid Teigland 
1027b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
1028b3b94faaSDavid Teigland 
1029d69a3c65SSteven Whitehouse 	if (sdp->sd_log_tr) {
1030d69a3c65SSteven Whitehouse 		gfs2_merge_trans(sdp->sd_log_tr, tr);
1031d69a3c65SSteven Whitehouse 	} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
10329862ca05SBob Peterson 		gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
103316ca9412SBenjamin Marzinski 		sdp->sd_log_tr = tr;
10349862ca05SBob Peterson 		set_bit(TR_ATTACHED, &tr->tr_flags);
103516ca9412SBenjamin Marzinski 	}
1036022ef4feSSteven Whitehouse 
1037a31b4ec5SBob Peterson 	sdp->sd_log_committed_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
1038022ef4feSSteven Whitehouse 	reserved = calc_reserved(sdp);
1039022ef4feSSteven Whitehouse 	maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
1040022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, maxres >= reserved);
1041022ef4feSSteven Whitehouse 	unused = maxres - reserved;
1042022ef4feSSteven Whitehouse 	atomic_add(unused, &sdp->sd_log_blks_free);
1043022ef4feSSteven Whitehouse 	trace_gfs2_log_blocks(sdp, unused);
1044022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
1045022ef4feSSteven Whitehouse 			     sdp->sd_jdesc->jd_blocks);
1046022ef4feSSteven Whitehouse 	sdp->sd_log_blks_reserved = reserved;
1047022ef4feSSteven Whitehouse 
1048b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
1049b3b94faaSDavid Teigland }
1050b3b94faaSDavid Teigland 
1051b3b94faaSDavid Teigland /**
1052b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
1053b3b94faaSDavid Teigland  * @sdp: the filesystem
1054b3b94faaSDavid Teigland  * @tr: the transaction
1055b3b94faaSDavid Teigland  *
10565e687eacSBenjamin Marzinski  * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
10575e687eacSBenjamin Marzinski  * or the total number of used blocks (pinned blocks plus AIL blocks)
10585e687eacSBenjamin Marzinski  * is greater than thresh2.
10595e687eacSBenjamin Marzinski  *
10605e687eacSBenjamin Marzinski  * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
10615e687eacSBenjamin Marzinski  * journal size.
10625e687eacSBenjamin Marzinski  *
1063b3b94faaSDavid Teigland  * Returns: errno
1064b3b94faaSDavid Teigland  */
1065b3b94faaSDavid Teigland 
1066b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
1067b3b94faaSDavid Teigland {
1068b3b94faaSDavid Teigland 	log_refund(sdp, tr);
1069b3b94faaSDavid Teigland 
10705e687eacSBenjamin Marzinski 	if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
10715e687eacSBenjamin Marzinski 	    ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
10725e687eacSBenjamin Marzinski 	    atomic_read(&sdp->sd_log_thresh2)))
10735e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_logd_waitq);
1074faa31ce8SSteven Whitehouse }
1075b3b94faaSDavid Teigland 
1076b3b94faaSDavid Teigland /**
1077b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
1078b3b94faaSDavid Teigland  * @sdp: the filesystem
1079b3b94faaSDavid Teigland  *
1080b3b94faaSDavid Teigland  */
1081b3b94faaSDavid Teigland 
1082feed98a8SBob Peterson static void gfs2_log_shutdown(struct gfs2_sbd *sdp)
1083b3b94faaSDavid Teigland {
1084b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
1085b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
1086b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
1087b3b94faaSDavid Teigland 
1088b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
1089b3b94faaSDavid Teigland 
1090805c0907SBob Peterson 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
1091b3b94faaSDavid Teigland 
1092a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
1093a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
1094b3b94faaSDavid Teigland 
1095b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
1096b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
1097a25311c8SSteven Whitehouse }
1098a25311c8SSteven Whitehouse 
10995e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
11005e687eacSBenjamin Marzinski {
1101f07b3520SBob Peterson 	return (atomic_read(&sdp->sd_log_pinned) +
1102f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_blks_needed) >=
1103f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh1));
11045e687eacSBenjamin Marzinski }
11055e687eacSBenjamin Marzinski 
11065e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
11075e687eacSBenjamin Marzinski {
11085e687eacSBenjamin Marzinski 	unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
1109b066a4eeSAbhi Das 
1110b066a4eeSAbhi Das 	if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
1111b066a4eeSAbhi Das 		return 1;
1112b066a4eeSAbhi Das 
1113f07b3520SBob Peterson 	return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
1114f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh2);
11155e687eacSBenjamin Marzinski }
1116ec69b188SSteven Whitehouse 
1117ec69b188SSteven Whitehouse /**
1118ec69b188SSteven Whitehouse  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
1119ec69b188SSteven Whitehouse  * @sdp: Pointer to GFS2 superblock
1120ec69b188SSteven Whitehouse  *
1121ec69b188SSteven Whitehouse  * Also, periodically check to make sure that we're using the most recent
1122ec69b188SSteven Whitehouse  * journal index.
1123ec69b188SSteven Whitehouse  */
1124ec69b188SSteven Whitehouse 
1125ec69b188SSteven Whitehouse int gfs2_logd(void *data)
1126ec69b188SSteven Whitehouse {
1127ec69b188SSteven Whitehouse 	struct gfs2_sbd *sdp = data;
11285e687eacSBenjamin Marzinski 	unsigned long t = 1;
11295e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
1130b63f5e84SBob Peterson 	bool did_flush;
1131ec69b188SSteven Whitehouse 
1132ec69b188SSteven Whitehouse 	while (!kthread_should_stop()) {
1133ec69b188SSteven Whitehouse 
1134d22f69a0SBob Peterson 		if (gfs2_withdrawn(sdp)) {
1135d22f69a0SBob Peterson 			msleep_interruptible(HZ);
1136d22f69a0SBob Peterson 			continue;
1137d22f69a0SBob Peterson 		}
1138942b0cddSBob Peterson 		/* Check for errors writing to the journal */
1139942b0cddSBob Peterson 		if (sdp->sd_log_error) {
1140badb55ecSAndreas Gruenbacher 			gfs2_lm(sdp,
1141942b0cddSBob Peterson 				"GFS2: fsid=%s: error %d: "
1142942b0cddSBob Peterson 				"withdrawing the file system to "
1143942b0cddSBob Peterson 				"prevent further damage.\n",
1144942b0cddSBob Peterson 				sdp->sd_fsname, sdp->sd_log_error);
1145badb55ecSAndreas Gruenbacher 			gfs2_withdraw(sdp);
1146d22f69a0SBob Peterson 			continue;
1147942b0cddSBob Peterson 		}
1148942b0cddSBob Peterson 
1149b63f5e84SBob Peterson 		did_flush = false;
11505e687eacSBenjamin Marzinski 		if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
11515e4c7632SBob Peterson 			gfs2_ail1_empty(sdp, 0);
1152805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1153805c0907SBob Peterson 				       GFS2_LFC_LOGD_JFLUSH_REQD);
1154b63f5e84SBob Peterson 			did_flush = true;
1155ec69b188SSteven Whitehouse 		}
1156ec69b188SSteven Whitehouse 
11575e687eacSBenjamin Marzinski 		if (gfs2_ail_flush_reqd(sdp)) {
11585e687eacSBenjamin Marzinski 			gfs2_ail1_start(sdp);
115926b06a69SSteven Whitehouse 			gfs2_ail1_wait(sdp);
11605e4c7632SBob Peterson 			gfs2_ail1_empty(sdp, 0);
1161805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1162805c0907SBob Peterson 				       GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1163b63f5e84SBob Peterson 			did_flush = true;
11645e687eacSBenjamin Marzinski 		}
11655e687eacSBenjamin Marzinski 
1166b63f5e84SBob Peterson 		if (!gfs2_ail_flush_reqd(sdp) || did_flush)
11675e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_log_waitq);
116826b06a69SSteven Whitehouse 
1169ec69b188SSteven Whitehouse 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
1170a0acae0eSTejun Heo 
1171a0acae0eSTejun Heo 		try_to_freeze();
11725e687eacSBenjamin Marzinski 
11735e687eacSBenjamin Marzinski 		do {
11745e687eacSBenjamin Marzinski 			prepare_to_wait(&sdp->sd_logd_waitq, &wait,
11755f487490SSteven Whitehouse 					TASK_INTERRUPTIBLE);
11765e687eacSBenjamin Marzinski 			if (!gfs2_ail_flush_reqd(sdp) &&
11775e687eacSBenjamin Marzinski 			    !gfs2_jrnl_flush_reqd(sdp) &&
11785e687eacSBenjamin Marzinski 			    !kthread_should_stop())
11795e687eacSBenjamin Marzinski 				t = schedule_timeout(t);
11805e687eacSBenjamin Marzinski 		} while(t && !gfs2_ail_flush_reqd(sdp) &&
11815e687eacSBenjamin Marzinski 			!gfs2_jrnl_flush_reqd(sdp) &&
11825e687eacSBenjamin Marzinski 			!kthread_should_stop());
11835e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_logd_waitq, &wait);
1184ec69b188SSteven Whitehouse 	}
1185ec69b188SSteven Whitehouse 
1186ec69b188SSteven Whitehouse 	return 0;
1187ec69b188SSteven Whitehouse }
1188ec69b188SSteven Whitehouse 
1189