xref: /openbmc/linux/fs/gfs2/log.c (revision c9e58fb2)
1b3b94faaSDavid Teigland /*
2b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
3da6dd40dSBob Peterson  * Copyright (C) 2004-2007 Red Hat, Inc.  All rights reserved.
4b3b94faaSDavid Teigland  *
5b3b94faaSDavid Teigland  * This copyrighted material is made available to anyone wishing to use,
6b3b94faaSDavid Teigland  * modify, copy, or redistribute it subject to the terms and conditions
7e9fc2aa0SSteven Whitehouse  * of the GNU General Public License version 2.
8b3b94faaSDavid Teigland  */
9b3b94faaSDavid Teigland 
10b3b94faaSDavid Teigland #include <linux/sched.h>
11b3b94faaSDavid Teigland #include <linux/slab.h>
12b3b94faaSDavid Teigland #include <linux/spinlock.h>
13b3b94faaSDavid Teigland #include <linux/completion.h>
14b3b94faaSDavid Teigland #include <linux/buffer_head.h>
155c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h>
1671b86f56SSteven Whitehouse #include <linux/crc32.h>
17c1696fb8SBob Peterson #include <linux/crc32c.h>
18a25311c8SSteven Whitehouse #include <linux/delay.h>
19ec69b188SSteven Whitehouse #include <linux/kthread.h>
20ec69b188SSteven Whitehouse #include <linux/freezer.h>
21254db57fSSteven Whitehouse #include <linux/bio.h>
22885bcecaSSteven Whitehouse #include <linux/blkdev.h>
234667a0ecSSteven Whitehouse #include <linux/writeback.h>
244a36d08dSBob Peterson #include <linux/list_sort.h>
25b3b94faaSDavid Teigland 
26b3b94faaSDavid Teigland #include "gfs2.h"
275c676f6dSSteven Whitehouse #include "incore.h"
28b3b94faaSDavid Teigland #include "bmap.h"
29b3b94faaSDavid Teigland #include "glock.h"
30b3b94faaSDavid Teigland #include "log.h"
31b3b94faaSDavid Teigland #include "lops.h"
32b3b94faaSDavid Teigland #include "meta_io.h"
335c676f6dSSteven Whitehouse #include "util.h"
3471b86f56SSteven Whitehouse #include "dir.h"
3563997775SSteven Whitehouse #include "trace_gfs2.h"
36b3b94faaSDavid Teigland 
37b3b94faaSDavid Teigland /**
38b3b94faaSDavid Teigland  * gfs2_struct2blk - compute stuff
39b3b94faaSDavid Teigland  * @sdp: the filesystem
40b3b94faaSDavid Teigland  * @nstruct: the number of structures
41b3b94faaSDavid Teigland  * @ssize: the size of the structures
42b3b94faaSDavid Teigland  *
43b3b94faaSDavid Teigland  * Compute the number of log descriptor blocks needed to hold a certain number
44b3b94faaSDavid Teigland  * of structures of a certain size.
45b3b94faaSDavid Teigland  *
46b3b94faaSDavid Teigland  * Returns: the number of blocks needed (minimum is always 1)
47b3b94faaSDavid Teigland  */
48b3b94faaSDavid Teigland 
49b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
50b3b94faaSDavid Teigland 			     unsigned int ssize)
51b3b94faaSDavid Teigland {
52b3b94faaSDavid Teigland 	unsigned int blks;
53b3b94faaSDavid Teigland 	unsigned int first, second;
54b3b94faaSDavid Teigland 
55b3b94faaSDavid Teigland 	blks = 1;
56faa31ce8SSteven Whitehouse 	first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
57b3b94faaSDavid Teigland 
58b3b94faaSDavid Teigland 	if (nstruct > first) {
59568f4c96SSteven Whitehouse 		second = (sdp->sd_sb.sb_bsize -
60568f4c96SSteven Whitehouse 			  sizeof(struct gfs2_meta_header)) / ssize;
615c676f6dSSteven Whitehouse 		blks += DIV_ROUND_UP(nstruct - first, second);
62b3b94faaSDavid Teigland 	}
63b3b94faaSDavid Teigland 
64b3b94faaSDavid Teigland 	return blks;
65b3b94faaSDavid Teigland }
66b3b94faaSDavid Teigland 
67ddacfaf7SSteven Whitehouse /**
681e1a3d03SSteven Whitehouse  * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
691e1a3d03SSteven Whitehouse  * @mapping: The associated mapping (maybe NULL)
701e1a3d03SSteven Whitehouse  * @bd: The gfs2_bufdata to remove
711e1a3d03SSteven Whitehouse  *
72c618e87aSSteven Whitehouse  * The ail lock _must_ be held when calling this function
731e1a3d03SSteven Whitehouse  *
741e1a3d03SSteven Whitehouse  */
751e1a3d03SSteven Whitehouse 
769bc980cdSBob Peterson static void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
771e1a3d03SSteven Whitehouse {
7816ca9412SBenjamin Marzinski 	bd->bd_tr = NULL;
791ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_st_list);
801ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_gl_list);
811e1a3d03SSteven Whitehouse 	atomic_dec(&bd->bd_gl->gl_ail_count);
821e1a3d03SSteven Whitehouse 	brelse(bd->bd_bh);
831e1a3d03SSteven Whitehouse }
841e1a3d03SSteven Whitehouse 
851e1a3d03SSteven Whitehouse /**
86ddacfaf7SSteven Whitehouse  * gfs2_ail1_start_one - Start I/O on a part of the AIL
87ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
884667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
894667a0ecSSteven Whitehouse  * @ai: The ail structure
90ddacfaf7SSteven Whitehouse  *
91ddacfaf7SSteven Whitehouse  */
92ddacfaf7SSteven Whitehouse 
934f1de018SSteven Whitehouse static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
944667a0ecSSteven Whitehouse 			       struct writeback_control *wbc,
959e1a9ecdSAndreas Gruenbacher 			       struct gfs2_trans *tr,
969e1a9ecdSAndreas Gruenbacher 			       bool *withdraw)
97d6a079e8SDave Chinner __releases(&sdp->sd_ail_lock)
98d6a079e8SDave Chinner __acquires(&sdp->sd_ail_lock)
99ddacfaf7SSteven Whitehouse {
1005ac048bbSSteven Whitehouse 	struct gfs2_glock *gl = NULL;
1014667a0ecSSteven Whitehouse 	struct address_space *mapping;
102ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
103ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
104ddacfaf7SSteven Whitehouse 
10516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
106ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
107ddacfaf7SSteven Whitehouse 
10816ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
109ddacfaf7SSteven Whitehouse 
110ddacfaf7SSteven Whitehouse 		if (!buffer_busy(bh)) {
111b524abccSBob Peterson 			if (!buffer_uptodate(bh) &&
112b524abccSBob Peterson 			    !test_and_set_bit(SDF_AIL1_IO_ERROR,
113b524abccSBob Peterson 					      &sdp->sd_flags)) {
114ddacfaf7SSteven Whitehouse 				gfs2_io_error_bh(sdp, bh);
1159e1a9ecdSAndreas Gruenbacher 				*withdraw = true;
1169e1a9ecdSAndreas Gruenbacher 			}
11716ca9412SBenjamin Marzinski 			list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
118ddacfaf7SSteven Whitehouse 			continue;
119ddacfaf7SSteven Whitehouse 		}
120ddacfaf7SSteven Whitehouse 
121ddacfaf7SSteven Whitehouse 		if (!buffer_dirty(bh))
122ddacfaf7SSteven Whitehouse 			continue;
1235ac048bbSSteven Whitehouse 		if (gl == bd->bd_gl)
1245ac048bbSSteven Whitehouse 			continue;
1255ac048bbSSteven Whitehouse 		gl = bd->bd_gl;
12616ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
1274667a0ecSSteven Whitehouse 		mapping = bh->b_page->mapping;
1284f1de018SSteven Whitehouse 		if (!mapping)
1294f1de018SSteven Whitehouse 			continue;
130d6a079e8SDave Chinner 		spin_unlock(&sdp->sd_ail_lock);
1314667a0ecSSteven Whitehouse 		generic_writepages(mapping, wbc);
132d6a079e8SDave Chinner 		spin_lock(&sdp->sd_ail_lock);
1334667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
134ddacfaf7SSteven Whitehouse 			break;
1354f1de018SSteven Whitehouse 		return 1;
136ddacfaf7SSteven Whitehouse 	}
1374f1de018SSteven Whitehouse 
1384f1de018SSteven Whitehouse 	return 0;
1394667a0ecSSteven Whitehouse }
1404667a0ecSSteven Whitehouse 
1414667a0ecSSteven Whitehouse 
1424667a0ecSSteven Whitehouse /**
1434667a0ecSSteven Whitehouse  * gfs2_ail1_flush - start writeback of some ail1 entries
1444667a0ecSSteven Whitehouse  * @sdp: The super block
1454667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
1464667a0ecSSteven Whitehouse  *
1474667a0ecSSteven Whitehouse  * Writes back some ail1 entries, according to the limits in the
1484667a0ecSSteven Whitehouse  * writeback control structure
1494667a0ecSSteven Whitehouse  */
1504667a0ecSSteven Whitehouse 
1514667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
1524667a0ecSSteven Whitehouse {
1534667a0ecSSteven Whitehouse 	struct list_head *head = &sdp->sd_ail1_list;
15416ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
155885bcecaSSteven Whitehouse 	struct blk_plug plug;
1569e1a9ecdSAndreas Gruenbacher 	bool withdraw = false;
1574667a0ecSSteven Whitehouse 
158c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 1);
159885bcecaSSteven Whitehouse 	blk_start_plug(&plug);
1604667a0ecSSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
1614f1de018SSteven Whitehouse restart:
16216ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, head, tr_list) {
1634667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
1644667a0ecSSteven Whitehouse 			break;
1659e1a9ecdSAndreas Gruenbacher 		if (gfs2_ail1_start_one(sdp, wbc, tr, &withdraw))
1664f1de018SSteven Whitehouse 			goto restart;
1674667a0ecSSteven Whitehouse 	}
1684667a0ecSSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
169885bcecaSSteven Whitehouse 	blk_finish_plug(&plug);
1709e1a9ecdSAndreas Gruenbacher 	if (withdraw)
1719e1a9ecdSAndreas Gruenbacher 		gfs2_lm_withdraw(sdp, NULL);
172c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 0);
1734667a0ecSSteven Whitehouse }
1744667a0ecSSteven Whitehouse 
1754667a0ecSSteven Whitehouse /**
1764667a0ecSSteven Whitehouse  * gfs2_ail1_start - start writeback of all ail1 entries
1774667a0ecSSteven Whitehouse  * @sdp: The superblock
1784667a0ecSSteven Whitehouse  */
1794667a0ecSSteven Whitehouse 
1804667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp)
1814667a0ecSSteven Whitehouse {
1824667a0ecSSteven Whitehouse 	struct writeback_control wbc = {
1834667a0ecSSteven Whitehouse 		.sync_mode = WB_SYNC_NONE,
1844667a0ecSSteven Whitehouse 		.nr_to_write = LONG_MAX,
1854667a0ecSSteven Whitehouse 		.range_start = 0,
1864667a0ecSSteven Whitehouse 		.range_end = LLONG_MAX,
1874667a0ecSSteven Whitehouse 	};
1884667a0ecSSteven Whitehouse 
1894667a0ecSSteven Whitehouse 	return gfs2_ail1_flush(sdp, &wbc);
190ddacfaf7SSteven Whitehouse }
191ddacfaf7SSteven Whitehouse 
192ddacfaf7SSteven Whitehouse /**
193ddacfaf7SSteven Whitehouse  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
194ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
195ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
196ddacfaf7SSteven Whitehouse  *
197ddacfaf7SSteven Whitehouse  */
198ddacfaf7SSteven Whitehouse 
1999e1a9ecdSAndreas Gruenbacher static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr,
2009e1a9ecdSAndreas Gruenbacher 				bool *withdraw)
201ddacfaf7SSteven Whitehouse {
202ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
203ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
204ddacfaf7SSteven Whitehouse 
20516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
206ddacfaf7SSteven Whitehouse 					 bd_ail_st_list) {
207ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
20816ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
2094667a0ecSSteven Whitehouse 		if (buffer_busy(bh))
210ddacfaf7SSteven Whitehouse 			continue;
211b524abccSBob Peterson 		if (!buffer_uptodate(bh) &&
212b524abccSBob Peterson 		    !test_and_set_bit(SDF_AIL1_IO_ERROR, &sdp->sd_flags)) {
213ddacfaf7SSteven Whitehouse 			gfs2_io_error_bh(sdp, bh);
2149e1a9ecdSAndreas Gruenbacher 			*withdraw = true;
2159e1a9ecdSAndreas Gruenbacher 		}
21616ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
217ddacfaf7SSteven Whitehouse 	}
218ddacfaf7SSteven Whitehouse }
219ddacfaf7SSteven Whitehouse 
2204667a0ecSSteven Whitehouse /**
2214667a0ecSSteven Whitehouse  * gfs2_ail1_empty - Try to empty the ail1 lists
2224667a0ecSSteven Whitehouse  * @sdp: The superblock
2234667a0ecSSteven Whitehouse  *
2244667a0ecSSteven Whitehouse  * Tries to empty the ail1 lists, starting with the oldest first
2254667a0ecSSteven Whitehouse  */
226b3b94faaSDavid Teigland 
2274667a0ecSSteven Whitehouse static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
228b3b94faaSDavid Teigland {
22916ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *s;
2305d054964SBenjamin Marzinski 	int oldest_tr = 1;
231b3b94faaSDavid Teigland 	int ret;
2329e1a9ecdSAndreas Gruenbacher 	bool withdraw = false;
233b3b94faaSDavid Teigland 
234d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
23516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
2369e1a9ecdSAndreas Gruenbacher 		gfs2_ail1_empty_one(sdp, tr, &withdraw);
2375d054964SBenjamin Marzinski 		if (list_empty(&tr->tr_ail1_list) && oldest_tr)
23816ca9412SBenjamin Marzinski 			list_move(&tr->tr_list, &sdp->sd_ail2_list);
2394667a0ecSSteven Whitehouse 		else
2405d054964SBenjamin Marzinski 			oldest_tr = 0;
241b3b94faaSDavid Teigland 	}
242b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
243d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
244b3b94faaSDavid Teigland 
2459e1a9ecdSAndreas Gruenbacher 	if (withdraw)
2469e1a9ecdSAndreas Gruenbacher 		gfs2_lm_withdraw(sdp, "fatal: I/O error(s)\n");
2479e1a9ecdSAndreas Gruenbacher 
248b3b94faaSDavid Teigland 	return ret;
249b3b94faaSDavid Teigland }
250b3b94faaSDavid Teigland 
25126b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
25226b06a69SSteven Whitehouse {
25316ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
25426b06a69SSteven Whitehouse 	struct gfs2_bufdata *bd;
25526b06a69SSteven Whitehouse 	struct buffer_head *bh;
25626b06a69SSteven Whitehouse 
25726b06a69SSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
25816ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
25916ca9412SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
26026b06a69SSteven Whitehouse 			bh = bd->bd_bh;
26126b06a69SSteven Whitehouse 			if (!buffer_locked(bh))
26226b06a69SSteven Whitehouse 				continue;
26326b06a69SSteven Whitehouse 			get_bh(bh);
26426b06a69SSteven Whitehouse 			spin_unlock(&sdp->sd_ail_lock);
26526b06a69SSteven Whitehouse 			wait_on_buffer(bh);
26626b06a69SSteven Whitehouse 			brelse(bh);
26726b06a69SSteven Whitehouse 			return;
26826b06a69SSteven Whitehouse 		}
26926b06a69SSteven Whitehouse 	}
27026b06a69SSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
27126b06a69SSteven Whitehouse }
272ddacfaf7SSteven Whitehouse 
273ddacfaf7SSteven Whitehouse /**
274ddacfaf7SSteven Whitehouse  * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
275ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
276ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
277ddacfaf7SSteven Whitehouse  *
278ddacfaf7SSteven Whitehouse  */
279ddacfaf7SSteven Whitehouse 
28016ca9412SBenjamin Marzinski static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
281ddacfaf7SSteven Whitehouse {
28216ca9412SBenjamin Marzinski 	struct list_head *head = &tr->tr_ail2_list;
283ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd;
284ddacfaf7SSteven Whitehouse 
285ddacfaf7SSteven Whitehouse 	while (!list_empty(head)) {
286ddacfaf7SSteven Whitehouse 		bd = list_entry(head->prev, struct gfs2_bufdata,
287ddacfaf7SSteven Whitehouse 				bd_ail_st_list);
28816ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
289f91a0d3eSSteven Whitehouse 		gfs2_remove_from_ail(bd);
290ddacfaf7SSteven Whitehouse 	}
291ddacfaf7SSteven Whitehouse }
292ddacfaf7SSteven Whitehouse 
293b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
294b3b94faaSDavid Teigland {
29516ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *safe;
296b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
297b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
298b3b94faaSDavid Teigland 	int a, b, rm;
299b3b94faaSDavid Teigland 
300d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
301b3b94faaSDavid Teigland 
30216ca9412SBenjamin Marzinski 	list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
30316ca9412SBenjamin Marzinski 		a = (old_tail <= tr->tr_first);
30416ca9412SBenjamin Marzinski 		b = (tr->tr_first < new_tail);
305b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
306b3b94faaSDavid Teigland 		if (!rm)
307b3b94faaSDavid Teigland 			continue;
308b3b94faaSDavid Teigland 
30916ca9412SBenjamin Marzinski 		gfs2_ail2_empty_one(sdp, tr);
31016ca9412SBenjamin Marzinski 		list_del(&tr->tr_list);
31116ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
31216ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
31316ca9412SBenjamin Marzinski 		kfree(tr);
314b3b94faaSDavid Teigland 	}
315b3b94faaSDavid Teigland 
316d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
317b3b94faaSDavid Teigland }
318b3b94faaSDavid Teigland 
319b3b94faaSDavid Teigland /**
32024972557SBenjamin Marzinski  * gfs2_log_release - Release a given number of log blocks
32124972557SBenjamin Marzinski  * @sdp: The GFS2 superblock
32224972557SBenjamin Marzinski  * @blks: The number of blocks
32324972557SBenjamin Marzinski  *
32424972557SBenjamin Marzinski  */
32524972557SBenjamin Marzinski 
32624972557SBenjamin Marzinski void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
32724972557SBenjamin Marzinski {
32824972557SBenjamin Marzinski 
32924972557SBenjamin Marzinski 	atomic_add(blks, &sdp->sd_log_blks_free);
33024972557SBenjamin Marzinski 	trace_gfs2_log_blocks(sdp, blks);
33124972557SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
33224972557SBenjamin Marzinski 				  sdp->sd_jdesc->jd_blocks);
33324972557SBenjamin Marzinski 	up_read(&sdp->sd_log_flush_lock);
33424972557SBenjamin Marzinski }
33524972557SBenjamin Marzinski 
33624972557SBenjamin Marzinski /**
337b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
338b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
339b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
340b3b94faaSDavid Teigland  *
34189918647SSteven Whitehouse  * Note that we never give out the last few blocks of the journal. Thats
3422332c443SRobert Peterson  * due to the fact that there is a small number of header blocks
343b004157aSSteven Whitehouse  * associated with each log flush. The exact number can't be known until
344b004157aSSteven Whitehouse  * flush time, so we ensure that we have just enough free blocks at all
345b004157aSSteven Whitehouse  * times to avoid running out during a log flush.
346b004157aSSteven Whitehouse  *
3475e687eacSBenjamin Marzinski  * We no longer flush the log here, instead we wake up logd to do that
3485e687eacSBenjamin Marzinski  * for us. To avoid the thundering herd and to ensure that we deal fairly
3495e687eacSBenjamin Marzinski  * with queued waiters, we use an exclusive wait. This means that when we
3505e687eacSBenjamin Marzinski  * get woken with enough journal space to get our reservation, we need to
3515e687eacSBenjamin Marzinski  * wake the next waiter on the list.
3525e687eacSBenjamin Marzinski  *
353b3b94faaSDavid Teigland  * Returns: errno
354b3b94faaSDavid Teigland  */
355b3b94faaSDavid Teigland 
356b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
357b3b94faaSDavid Teigland {
3582e60d768SBenjamin Marzinski 	int ret = 0;
3595d054964SBenjamin Marzinski 	unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
3605e687eacSBenjamin Marzinski 	unsigned wanted = blks + reserved_blks;
3615e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
3625e687eacSBenjamin Marzinski 	int did_wait = 0;
3635e687eacSBenjamin Marzinski 	unsigned int free_blocks;
364b3b94faaSDavid Teigland 
365b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
366b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
367b3b94faaSDavid Teigland 		return -EINVAL;
368f07b3520SBob Peterson 	atomic_add(blks, &sdp->sd_log_blks_needed);
3695e687eacSBenjamin Marzinski retry:
3705e687eacSBenjamin Marzinski 	free_blocks = atomic_read(&sdp->sd_log_blks_free);
3715e687eacSBenjamin Marzinski 	if (unlikely(free_blocks <= wanted)) {
3725e687eacSBenjamin Marzinski 		do {
3735e687eacSBenjamin Marzinski 			prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
3745e687eacSBenjamin Marzinski 					TASK_UNINTERRUPTIBLE);
3755e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_logd_waitq);
3765e687eacSBenjamin Marzinski 			did_wait = 1;
3775e687eacSBenjamin Marzinski 			if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
3785e687eacSBenjamin Marzinski 				io_schedule();
3795e687eacSBenjamin Marzinski 			free_blocks = atomic_read(&sdp->sd_log_blks_free);
3805e687eacSBenjamin Marzinski 		} while(free_blocks <= wanted);
3815e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_log_waitq, &wait);
382b3b94faaSDavid Teigland 	}
3832e60d768SBenjamin Marzinski 	atomic_inc(&sdp->sd_reserving_log);
3845e687eacSBenjamin Marzinski 	if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
3852e60d768SBenjamin Marzinski 				free_blocks - blks) != free_blocks) {
3862e60d768SBenjamin Marzinski 		if (atomic_dec_and_test(&sdp->sd_reserving_log))
3872e60d768SBenjamin Marzinski 			wake_up(&sdp->sd_reserving_log_wait);
3885e687eacSBenjamin Marzinski 		goto retry;
3892e60d768SBenjamin Marzinski 	}
390f07b3520SBob Peterson 	atomic_sub(blks, &sdp->sd_log_blks_needed);
39163997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, -blks);
3925e687eacSBenjamin Marzinski 
3935e687eacSBenjamin Marzinski 	/*
3945e687eacSBenjamin Marzinski 	 * If we waited, then so might others, wake them up _after_ we get
3955e687eacSBenjamin Marzinski 	 * our share of the log.
3965e687eacSBenjamin Marzinski 	 */
3975e687eacSBenjamin Marzinski 	if (unlikely(did_wait))
3985e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_log_waitq);
399484adff8SSteven Whitehouse 
400484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
40124972557SBenjamin Marzinski 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
40224972557SBenjamin Marzinski 		gfs2_log_release(sdp, blks);
4032e60d768SBenjamin Marzinski 		ret = -EROFS;
40424972557SBenjamin Marzinski 	}
4052e60d768SBenjamin Marzinski 	if (atomic_dec_and_test(&sdp->sd_reserving_log))
4062e60d768SBenjamin Marzinski 		wake_up(&sdp->sd_reserving_log_wait);
4072e60d768SBenjamin Marzinski 	return ret;
408b3b94faaSDavid Teigland }
409b3b94faaSDavid Teigland 
410b3b94faaSDavid Teigland /**
411b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
412b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
413b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
414b3b94faaSDavid Teigland  * @older: The older journal block of the pair
415b3b94faaSDavid Teigland  *
416b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
417b3b94faaSDavid Teigland  *   blocks in the journal
418b3b94faaSDavid Teigland  *
419b3b94faaSDavid Teigland  * Returns: the distance in blocks
420b3b94faaSDavid Teigland  */
421b3b94faaSDavid Teigland 
422faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
423b3b94faaSDavid Teigland 					unsigned int older)
424b3b94faaSDavid Teigland {
425b3b94faaSDavid Teigland 	int dist;
426b3b94faaSDavid Teigland 
427b3b94faaSDavid Teigland 	dist = newer - older;
428b3b94faaSDavid Teigland 	if (dist < 0)
429b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
430b3b94faaSDavid Teigland 
431b3b94faaSDavid Teigland 	return dist;
432b3b94faaSDavid Teigland }
433b3b94faaSDavid Teigland 
4342332c443SRobert Peterson /**
4352332c443SRobert Peterson  * calc_reserved - Calculate the number of blocks to reserve when
4362332c443SRobert Peterson  *                 refunding a transaction's unused buffers.
4372332c443SRobert Peterson  * @sdp: The GFS2 superblock
4382332c443SRobert Peterson  *
4392332c443SRobert Peterson  * This is complex.  We need to reserve room for all our currently used
4402332c443SRobert Peterson  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
4412332c443SRobert Peterson  * all our journaled data buffers for journaled files (e.g. files in the
4422332c443SRobert Peterson  * meta_fs like rindex, or files for which chattr +j was done.)
4432332c443SRobert Peterson  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
4442332c443SRobert Peterson  * will count it as free space (sd_log_blks_free) and corruption will follow.
4452332c443SRobert Peterson  *
4462332c443SRobert Peterson  * We can have metadata bufs and jdata bufs in the same journal.  So each
4472332c443SRobert Peterson  * type gets its own log header, for which we need to reserve a block.
4482332c443SRobert Peterson  * In fact, each type has the potential for needing more than one header
4492332c443SRobert Peterson  * in cases where we have more buffers than will fit on a journal page.
4502332c443SRobert Peterson  * Metadata journal entries take up half the space of journaled buffer entries.
4512332c443SRobert Peterson  * Thus, metadata entries have buf_limit (502) and journaled buffers have
4522332c443SRobert Peterson  * databuf_limit (251) before they cause a wrap around.
4532332c443SRobert Peterson  *
4542332c443SRobert Peterson  * Also, we need to reserve blocks for revoke journal entries and one for an
4552332c443SRobert Peterson  * overall header for the lot.
4562332c443SRobert Peterson  *
4572332c443SRobert Peterson  * Returns: the number of blocks reserved
4582332c443SRobert Peterson  */
4592332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp)
4602332c443SRobert Peterson {
4612332c443SRobert Peterson 	unsigned int reserved = 0;
462022ef4feSSteven Whitehouse 	unsigned int mbuf;
463022ef4feSSteven Whitehouse 	unsigned int dbuf;
464022ef4feSSteven Whitehouse 	struct gfs2_trans *tr = sdp->sd_log_tr;
4652332c443SRobert Peterson 
466022ef4feSSteven Whitehouse 	if (tr) {
467022ef4feSSteven Whitehouse 		mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
468022ef4feSSteven Whitehouse 		dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
469022ef4feSSteven Whitehouse 		reserved = mbuf + dbuf;
470022ef4feSSteven Whitehouse 		/* Account for header blocks */
471022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
472022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
473022ef4feSSteven Whitehouse 	}
4742332c443SRobert Peterson 
4752e95e3f6SBenjamin Marzinski 	if (sdp->sd_log_commited_revoke > 0)
476022ef4feSSteven Whitehouse 		reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
4772332c443SRobert Peterson 					  sizeof(u64));
4782332c443SRobert Peterson 	/* One for the overall header */
4792332c443SRobert Peterson 	if (reserved)
4802332c443SRobert Peterson 		reserved++;
4812332c443SRobert Peterson 	return reserved;
4822332c443SRobert Peterson }
4832332c443SRobert Peterson 
484b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
485b3b94faaSDavid Teigland {
48616ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
487b3b94faaSDavid Teigland 	unsigned int tail;
488b3b94faaSDavid Teigland 
489d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
490b3b94faaSDavid Teigland 
491faa31ce8SSteven Whitehouse 	if (list_empty(&sdp->sd_ail1_list)) {
492b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
493faa31ce8SSteven Whitehouse 	} else {
49416ca9412SBenjamin Marzinski 		tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
49516ca9412SBenjamin Marzinski 				tr_list);
49616ca9412SBenjamin Marzinski 		tail = tr->tr_first;
497b3b94faaSDavid Teigland 	}
498b3b94faaSDavid Teigland 
499d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
500b3b94faaSDavid Teigland 
501b3b94faaSDavid Teigland 	return tail;
502b3b94faaSDavid Teigland }
503b3b94faaSDavid Teigland 
5042332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
505b3b94faaSDavid Teigland {
506b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
507b3b94faaSDavid Teigland 
508b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
509b3b94faaSDavid Teigland 
510fd041f0bSSteven Whitehouse 	atomic_add(dist, &sdp->sd_log_blks_free);
51163997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, dist);
5125e687eacSBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
5135e687eacSBenjamin Marzinski 			     sdp->sd_jdesc->jd_blocks);
514b3b94faaSDavid Teigland 
515b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
516b3b94faaSDavid Teigland }
517b3b94faaSDavid Teigland 
518b3b94faaSDavid Teigland 
51934cc1781SSteven Whitehouse static void log_flush_wait(struct gfs2_sbd *sdp)
520b3b94faaSDavid Teigland {
52116615be1SSteven Whitehouse 	DEFINE_WAIT(wait);
522b3b94faaSDavid Teigland 
52316615be1SSteven Whitehouse 	if (atomic_read(&sdp->sd_log_in_flight)) {
52416615be1SSteven Whitehouse 		do {
52516615be1SSteven Whitehouse 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
52616615be1SSteven Whitehouse 					TASK_UNINTERRUPTIBLE);
52716615be1SSteven Whitehouse 			if (atomic_read(&sdp->sd_log_in_flight))
52816615be1SSteven Whitehouse 				io_schedule();
52916615be1SSteven Whitehouse 		} while(atomic_read(&sdp->sd_log_in_flight));
53016615be1SSteven Whitehouse 		finish_wait(&sdp->sd_log_flush_wait, &wait);
531b3b94faaSDavid Teigland 	}
532b3b94faaSDavid Teigland }
533b3b94faaSDavid Teigland 
53445138990SSteven Whitehouse static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
5354a36d08dSBob Peterson {
53645138990SSteven Whitehouse 	struct gfs2_inode *ipa, *ipb;
5374a36d08dSBob Peterson 
53845138990SSteven Whitehouse 	ipa = list_entry(a, struct gfs2_inode, i_ordered);
53945138990SSteven Whitehouse 	ipb = list_entry(b, struct gfs2_inode, i_ordered);
5404a36d08dSBob Peterson 
54145138990SSteven Whitehouse 	if (ipa->i_no_addr < ipb->i_no_addr)
5424a36d08dSBob Peterson 		return -1;
54345138990SSteven Whitehouse 	if (ipa->i_no_addr > ipb->i_no_addr)
5444a36d08dSBob Peterson 		return 1;
5454a36d08dSBob Peterson 	return 0;
5464a36d08dSBob Peterson }
5474a36d08dSBob Peterson 
548d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp)
549d7b616e2SSteven Whitehouse {
55045138990SSteven Whitehouse 	struct gfs2_inode *ip;
551d7b616e2SSteven Whitehouse 	LIST_HEAD(written);
552d7b616e2SSteven Whitehouse 
55345138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
55445138990SSteven Whitehouse 	list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
555d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
55645138990SSteven Whitehouse 		ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
5571f23bc78SAbhi Das 		if (ip->i_inode.i_mapping->nrpages == 0) {
5581f23bc78SAbhi Das 			test_and_clear_bit(GIF_ORDERED, &ip->i_flags);
5591f23bc78SAbhi Das 			list_del(&ip->i_ordered);
560d7b616e2SSteven Whitehouse 			continue;
5611f23bc78SAbhi Das 		}
5621f23bc78SAbhi Das 		list_move(&ip->i_ordered, &written);
56345138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
56445138990SSteven Whitehouse 		filemap_fdatawrite(ip->i_inode.i_mapping);
56545138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
566d7b616e2SSteven Whitehouse 	}
567d7b616e2SSteven Whitehouse 	list_splice(&written, &sdp->sd_log_le_ordered);
56845138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
569d7b616e2SSteven Whitehouse }
570d7b616e2SSteven Whitehouse 
571d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
572d7b616e2SSteven Whitehouse {
57345138990SSteven Whitehouse 	struct gfs2_inode *ip;
574d7b616e2SSteven Whitehouse 
57545138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
576d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
57745138990SSteven Whitehouse 		ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
57845138990SSteven Whitehouse 		list_del(&ip->i_ordered);
57945138990SSteven Whitehouse 		WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
58045138990SSteven Whitehouse 		if (ip->i_inode.i_mapping->nrpages == 0)
581d7b616e2SSteven Whitehouse 			continue;
58245138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
58345138990SSteven Whitehouse 		filemap_fdatawait(ip->i_inode.i_mapping);
58445138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
585d7b616e2SSteven Whitehouse 	}
58645138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
587d7b616e2SSteven Whitehouse }
58845138990SSteven Whitehouse 
58945138990SSteven Whitehouse void gfs2_ordered_del_inode(struct gfs2_inode *ip)
59045138990SSteven Whitehouse {
59145138990SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
59245138990SSteven Whitehouse 
59345138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
59445138990SSteven Whitehouse 	if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
59545138990SSteven Whitehouse 		list_del(&ip->i_ordered);
59645138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
597d7b616e2SSteven Whitehouse }
598d7b616e2SSteven Whitehouse 
5995d054964SBenjamin Marzinski void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
6005d054964SBenjamin Marzinski {
6015d054964SBenjamin Marzinski 	struct buffer_head *bh = bd->bd_bh;
6025d054964SBenjamin Marzinski 	struct gfs2_glock *gl = bd->bd_gl;
6035d054964SBenjamin Marzinski 
6045d054964SBenjamin Marzinski 	bh->b_private = NULL;
6055d054964SBenjamin Marzinski 	bd->bd_blkno = bh->b_blocknr;
6069290a9a7SBob Peterson 	gfs2_remove_from_ail(bd); /* drops ref on bh */
6079290a9a7SBob Peterson 	bd->bd_bh = NULL;
6085d054964SBenjamin Marzinski 	bd->bd_ops = &gfs2_revoke_lops;
6095d054964SBenjamin Marzinski 	sdp->sd_log_num_revoke++;
6105d054964SBenjamin Marzinski 	atomic_inc(&gl->gl_revokes);
6115d054964SBenjamin Marzinski 	set_bit(GLF_LFLUSH, &gl->gl_flags);
6125d054964SBenjamin Marzinski 	list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
6135d054964SBenjamin Marzinski }
6145d054964SBenjamin Marzinski 
6155d054964SBenjamin Marzinski void gfs2_write_revokes(struct gfs2_sbd *sdp)
6165d054964SBenjamin Marzinski {
6175d054964SBenjamin Marzinski 	struct gfs2_trans *tr;
6185d054964SBenjamin Marzinski 	struct gfs2_bufdata *bd, *tmp;
6195d054964SBenjamin Marzinski 	int have_revokes = 0;
6205d054964SBenjamin Marzinski 	int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
6215d054964SBenjamin Marzinski 
6225d054964SBenjamin Marzinski 	gfs2_ail1_empty(sdp);
6235d054964SBenjamin Marzinski 	spin_lock(&sdp->sd_ail_lock);
624c9e58fb2SBob Peterson 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
6255d054964SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
6265d054964SBenjamin Marzinski 			if (list_empty(&bd->bd_list)) {
6275d054964SBenjamin Marzinski 				have_revokes = 1;
6285d054964SBenjamin Marzinski 				goto done;
6295d054964SBenjamin Marzinski 			}
6305d054964SBenjamin Marzinski 		}
6315d054964SBenjamin Marzinski 	}
6325d054964SBenjamin Marzinski done:
6335d054964SBenjamin Marzinski 	spin_unlock(&sdp->sd_ail_lock);
6345d054964SBenjamin Marzinski 	if (have_revokes == 0)
6355d054964SBenjamin Marzinski 		return;
6365d054964SBenjamin Marzinski 	while (sdp->sd_log_num_revoke > max_revokes)
6375d054964SBenjamin Marzinski 		max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
6385d054964SBenjamin Marzinski 	max_revokes -= sdp->sd_log_num_revoke;
6395d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6405d054964SBenjamin Marzinski 		atomic_dec(&sdp->sd_log_blks_free);
6415d054964SBenjamin Marzinski 		/* If no blocks have been reserved, we need to also
6425d054964SBenjamin Marzinski 		 * reserve a block for the header */
6435d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6445d054964SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free);
6455d054964SBenjamin Marzinski 	}
6465d054964SBenjamin Marzinski 	gfs2_log_lock(sdp);
6475d054964SBenjamin Marzinski 	spin_lock(&sdp->sd_ail_lock);
648c9e58fb2SBob Peterson 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
6495d054964SBenjamin Marzinski 		list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
6505d054964SBenjamin Marzinski 			if (max_revokes == 0)
6515d054964SBenjamin Marzinski 				goto out_of_blocks;
6525d054964SBenjamin Marzinski 			if (!list_empty(&bd->bd_list))
6535d054964SBenjamin Marzinski 				continue;
6545d054964SBenjamin Marzinski 			gfs2_add_revoke(sdp, bd);
6555d054964SBenjamin Marzinski 			max_revokes--;
6565d054964SBenjamin Marzinski 		}
6575d054964SBenjamin Marzinski 	}
6585d054964SBenjamin Marzinski out_of_blocks:
6595d054964SBenjamin Marzinski 	spin_unlock(&sdp->sd_ail_lock);
6605d054964SBenjamin Marzinski 	gfs2_log_unlock(sdp);
6615d054964SBenjamin Marzinski 
6625d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6635d054964SBenjamin Marzinski 		atomic_inc(&sdp->sd_log_blks_free);
6645d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6655d054964SBenjamin Marzinski 			atomic_inc(&sdp->sd_log_blks_free);
6665d054964SBenjamin Marzinski 	}
6675d054964SBenjamin Marzinski }
6685d054964SBenjamin Marzinski 
669b3b94faaSDavid Teigland /**
670588bff95SBob Peterson  * write_log_header - Write a journal log header buffer at sd_log_flush_head
671588bff95SBob Peterson  * @sdp: The GFS2 superblock
672c1696fb8SBob Peterson  * @jd: journal descriptor of the journal to which we are writing
673588bff95SBob Peterson  * @seq: sequence number
674588bff95SBob Peterson  * @tail: tail of the log
675c1696fb8SBob Peterson  * @flags: log header flags GFS2_LOG_HEAD_*
676588bff95SBob Peterson  * @op_flags: flags to pass to the bio
677588bff95SBob Peterson  *
678588bff95SBob Peterson  * Returns: the initialized log buffer descriptor
679588bff95SBob Peterson  */
680588bff95SBob Peterson 
681c1696fb8SBob Peterson void gfs2_write_log_header(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
682c1696fb8SBob Peterson 			   u64 seq, u32 tail, u32 flags, int op_flags)
683588bff95SBob Peterson {
684588bff95SBob Peterson 	struct gfs2_log_header *lh;
685c1696fb8SBob Peterson 	u32 hash, crc;
686588bff95SBob Peterson 	struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
687c1696fb8SBob Peterson 	struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
688c1696fb8SBob Peterson 	struct timespec64 tv;
689c1696fb8SBob Peterson 	struct super_block *sb = sdp->sd_vfs;
690c1696fb8SBob Peterson 	u64 addr;
691588bff95SBob Peterson 
692588bff95SBob Peterson 	lh = page_address(page);
693588bff95SBob Peterson 	clear_page(lh);
694588bff95SBob Peterson 
695588bff95SBob Peterson 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
696588bff95SBob Peterson 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
697588bff95SBob Peterson 	lh->lh_header.__pad0 = cpu_to_be64(0);
698588bff95SBob Peterson 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
699588bff95SBob Peterson 	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
700588bff95SBob Peterson 	lh->lh_sequence = cpu_to_be64(seq);
701588bff95SBob Peterson 	lh->lh_flags = cpu_to_be32(flags);
702588bff95SBob Peterson 	lh->lh_tail = cpu_to_be32(tail);
703588bff95SBob Peterson 	lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
704c1696fb8SBob Peterson 	hash = ~crc32(~0, lh, LH_V1_SIZE);
705588bff95SBob Peterson 	lh->lh_hash = cpu_to_be32(hash);
706588bff95SBob Peterson 
707ee9c7f9aSArnd Bergmann 	ktime_get_coarse_real_ts64(&tv);
708c1696fb8SBob Peterson 	lh->lh_nsec = cpu_to_be32(tv.tv_nsec);
709c1696fb8SBob Peterson 	lh->lh_sec = cpu_to_be64(tv.tv_sec);
710c1696fb8SBob Peterson 	addr = gfs2_log_bmap(sdp);
711c1696fb8SBob Peterson 	lh->lh_addr = cpu_to_be64(addr);
712c1696fb8SBob Peterson 	lh->lh_jinode = cpu_to_be64(GFS2_I(jd->jd_inode)->i_no_addr);
713c1696fb8SBob Peterson 
714c1696fb8SBob Peterson 	/* We may only write local statfs, quota, etc., when writing to our
715c1696fb8SBob Peterson 	   own journal. The values are left 0 when recovering a journal
716c1696fb8SBob Peterson 	   different from our own. */
717c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_RECOVERY)) {
718c1696fb8SBob Peterson 		lh->lh_statfs_addr =
719c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_sc_inode)->i_no_addr);
720c1696fb8SBob Peterson 		lh->lh_quota_addr =
721c1696fb8SBob Peterson 			cpu_to_be64(GFS2_I(sdp->sd_qc_inode)->i_no_addr);
722c1696fb8SBob Peterson 
723c1696fb8SBob Peterson 		spin_lock(&sdp->sd_statfs_spin);
724c1696fb8SBob Peterson 		lh->lh_local_total = cpu_to_be64(l_sc->sc_total);
725c1696fb8SBob Peterson 		lh->lh_local_free = cpu_to_be64(l_sc->sc_free);
726c1696fb8SBob Peterson 		lh->lh_local_dinodes = cpu_to_be64(l_sc->sc_dinodes);
727c1696fb8SBob Peterson 		spin_unlock(&sdp->sd_statfs_spin);
728c1696fb8SBob Peterson 	}
729c1696fb8SBob Peterson 
730c1696fb8SBob Peterson 	BUILD_BUG_ON(offsetof(struct gfs2_log_header, lh_crc) != LH_V1_SIZE);
731c1696fb8SBob Peterson 
732c1696fb8SBob Peterson 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
733c1696fb8SBob Peterson 		     sb->s_blocksize - LH_V1_SIZE - 4);
734c1696fb8SBob Peterson 	lh->lh_crc = cpu_to_be32(crc);
735c1696fb8SBob Peterson 
736c1696fb8SBob Peterson 	gfs2_log_write(sdp, page, sb->s_blocksize, 0, addr);
737588bff95SBob Peterson 	gfs2_log_flush_bio(sdp, REQ_OP_WRITE, op_flags);
738588bff95SBob Peterson 	log_flush_wait(sdp);
739588bff95SBob Peterson }
740588bff95SBob Peterson 
741588bff95SBob Peterson /**
74234cc1781SSteven Whitehouse  * log_write_header - Get and initialize a journal header buffer
74334cc1781SSteven Whitehouse  * @sdp: The GFS2 superblock
744c1696fb8SBob Peterson  * @flags: The log header flags, including log header origin
74534cc1781SSteven Whitehouse  *
74634cc1781SSteven Whitehouse  * Returns: the initialized log buffer descriptor
74734cc1781SSteven Whitehouse  */
74834cc1781SSteven Whitehouse 
749fdb76a42SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
75034cc1781SSteven Whitehouse {
75134cc1781SSteven Whitehouse 	unsigned int tail;
7520f0b9b63SJan Kara 	int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
7532e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
75434cc1781SSteven Whitehouse 
7552e60d768SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
75634cc1781SSteven Whitehouse 	tail = current_tail(sdp);
75734cc1781SSteven Whitehouse 
75834cc1781SSteven Whitehouse 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
75934cc1781SSteven Whitehouse 		gfs2_ordered_wait(sdp);
76034cc1781SSteven Whitehouse 		log_flush_wait(sdp);
76170fd7614SChristoph Hellwig 		op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
76234cc1781SSteven Whitehouse 	}
763e8c92ed7SSteven Whitehouse 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
764c1696fb8SBob Peterson 	gfs2_write_log_header(sdp, sdp->sd_jdesc, sdp->sd_log_sequence++, tail,
765c1696fb8SBob Peterson 			      flags, op_flags);
76634cc1781SSteven Whitehouse 
76734cc1781SSteven Whitehouse 	if (sdp->sd_log_tail != tail)
76834cc1781SSteven Whitehouse 		log_pull_tail(sdp, tail);
76934cc1781SSteven Whitehouse }
77034cc1781SSteven Whitehouse 
77134cc1781SSteven Whitehouse /**
772b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
773b3b94faaSDavid Teigland  * @sdp: the filesystem
774b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
775805c0907SBob Peterson  * @flags: The log header flags: GFS2_LOG_HEAD_FLUSH_* and debug flags
776b3b94faaSDavid Teigland  *
777b3b94faaSDavid Teigland  */
778b3b94faaSDavid Teigland 
779c1696fb8SBob Peterson void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl, u32 flags)
780b3b94faaSDavid Teigland {
78116ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
7822e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
783b3b94faaSDavid Teigland 
784484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
785f55ab26aSSteven Whitehouse 
7862bcd610dSSteven Whitehouse 	/* Log might have been flushed while we waited for the flush lock */
7872bcd610dSSteven Whitehouse 	if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
788484adff8SSteven Whitehouse 		up_write(&sdp->sd_log_flush_lock);
789f55ab26aSSteven Whitehouse 		return;
790f55ab26aSSteven Whitehouse 	}
791805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 1, flags);
792f55ab26aSSteven Whitehouse 
793c1696fb8SBob Peterson 	if (flags & GFS2_LOG_HEAD_FLUSH_SHUTDOWN)
794400ac52eSBenjamin Marzinski 		clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
795400ac52eSBenjamin Marzinski 
796b1ab1e44SSteven Whitehouse 	sdp->sd_log_flush_head = sdp->sd_log_head;
79716ca9412SBenjamin Marzinski 	tr = sdp->sd_log_tr;
79816ca9412SBenjamin Marzinski 	if (tr) {
79916ca9412SBenjamin Marzinski 		sdp->sd_log_tr = NULL;
80016ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail1_list);
80116ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail2_list);
802b1ab1e44SSteven Whitehouse 		tr->tr_first = sdp->sd_log_flush_head;
8032e60d768SBenjamin Marzinski 		if (unlikely (state == SFS_FROZEN))
8042e60d768SBenjamin Marzinski 			gfs2_assert_withdraw(sdp, !tr->tr_num_buf_new && !tr->tr_num_databuf_new);
80516ca9412SBenjamin Marzinski 	}
806b3b94faaSDavid Teigland 
8072e60d768SBenjamin Marzinski 	if (unlikely(state == SFS_FROZEN))
8082e60d768SBenjamin Marzinski 		gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
809b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
810b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
811b3b94faaSDavid Teigland 
812d7b616e2SSteven Whitehouse 	gfs2_ordered_write(sdp);
813d69a3c65SSteven Whitehouse 	lops_before_commit(sdp, tr);
814e1b1afa6SMike Christie 	gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
815d7b616e2SSteven Whitehouse 
81634cc1781SSteven Whitehouse 	if (sdp->sd_log_head != sdp->sd_log_flush_head) {
817428fd95dSBob Peterson 		log_flush_wait(sdp);
818c1696fb8SBob Peterson 		log_write_header(sdp, flags);
81934cc1781SSteven Whitehouse 	} else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
820fd041f0bSSteven Whitehouse 		atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
82163997775SSteven Whitehouse 		trace_gfs2_log_blocks(sdp, -1);
822c1696fb8SBob Peterson 		log_write_header(sdp, flags);
8232332c443SRobert Peterson 	}
82416ca9412SBenjamin Marzinski 	lops_after_commit(sdp, tr);
825fe1a698fSSteven Whitehouse 
826fe1a698fSSteven Whitehouse 	gfs2_log_lock(sdp);
827b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
828faa31ce8SSteven Whitehouse 	sdp->sd_log_blks_reserved = 0;
829b3b94faaSDavid Teigland 	sdp->sd_log_commited_revoke = 0;
830b3b94faaSDavid Teigland 
831d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
83216ca9412SBenjamin Marzinski 	if (tr && !list_empty(&tr->tr_ail1_list)) {
83316ca9412SBenjamin Marzinski 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
83416ca9412SBenjamin Marzinski 		tr = NULL;
835b3b94faaSDavid Teigland 	}
836d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
837b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
83824972557SBenjamin Marzinski 
839c1696fb8SBob Peterson 	if (!(flags & GFS2_LOG_HEAD_FLUSH_NORMAL)) {
84024972557SBenjamin Marzinski 		if (!sdp->sd_log_idle) {
84124972557SBenjamin Marzinski 			for (;;) {
84224972557SBenjamin Marzinski 				gfs2_ail1_start(sdp);
84324972557SBenjamin Marzinski 				gfs2_ail1_wait(sdp);
84424972557SBenjamin Marzinski 				if (gfs2_ail1_empty(sdp))
84524972557SBenjamin Marzinski 					break;
84624972557SBenjamin Marzinski 			}
84724972557SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
84824972557SBenjamin Marzinski 			trace_gfs2_log_blocks(sdp, -1);
849c1696fb8SBob Peterson 			log_write_header(sdp, flags);
85024972557SBenjamin Marzinski 			sdp->sd_log_head = sdp->sd_log_flush_head;
85124972557SBenjamin Marzinski 		}
852c1696fb8SBob Peterson 		if (flags & (GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
853c1696fb8SBob Peterson 			     GFS2_LOG_HEAD_FLUSH_FREEZE))
85424972557SBenjamin Marzinski 			gfs2_log_shutdown(sdp);
855c1696fb8SBob Peterson 		if (flags & GFS2_LOG_HEAD_FLUSH_FREEZE)
8562e60d768SBenjamin Marzinski 			atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
85724972557SBenjamin Marzinski 	}
85824972557SBenjamin Marzinski 
859805c0907SBob Peterson 	trace_gfs2_log_flush(sdp, 0, flags);
860484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
861b3b94faaSDavid Teigland 
86216ca9412SBenjamin Marzinski 	kfree(tr);
863b3b94faaSDavid Teigland }
864b3b94faaSDavid Teigland 
865d69a3c65SSteven Whitehouse /**
866d69a3c65SSteven Whitehouse  * gfs2_merge_trans - Merge a new transaction into a cached transaction
867d69a3c65SSteven Whitehouse  * @old: Original transaction to be expanded
868d69a3c65SSteven Whitehouse  * @new: New transaction to be merged
869d69a3c65SSteven Whitehouse  */
870d69a3c65SSteven Whitehouse 
871d69a3c65SSteven Whitehouse static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
872d69a3c65SSteven Whitehouse {
8739862ca05SBob Peterson 	WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
874d69a3c65SSteven Whitehouse 
875d69a3c65SSteven Whitehouse 	old->tr_num_buf_new	+= new->tr_num_buf_new;
876d69a3c65SSteven Whitehouse 	old->tr_num_databuf_new	+= new->tr_num_databuf_new;
877d69a3c65SSteven Whitehouse 	old->tr_num_buf_rm	+= new->tr_num_buf_rm;
878d69a3c65SSteven Whitehouse 	old->tr_num_databuf_rm	+= new->tr_num_databuf_rm;
879d69a3c65SSteven Whitehouse 	old->tr_num_revoke	+= new->tr_num_revoke;
880d69a3c65SSteven Whitehouse 	old->tr_num_revoke_rm	+= new->tr_num_revoke_rm;
881d69a3c65SSteven Whitehouse 
882d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
883d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_buf, &old->tr_buf);
884d69a3c65SSteven Whitehouse }
885d69a3c65SSteven Whitehouse 
886b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
887b3b94faaSDavid Teigland {
8882332c443SRobert Peterson 	unsigned int reserved;
889ac39aaddSSteven Whitehouse 	unsigned int unused;
890022ef4feSSteven Whitehouse 	unsigned int maxres;
891b3b94faaSDavid Teigland 
892b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
893b3b94faaSDavid Teigland 
894d69a3c65SSteven Whitehouse 	if (sdp->sd_log_tr) {
895d69a3c65SSteven Whitehouse 		gfs2_merge_trans(sdp->sd_log_tr, tr);
896d69a3c65SSteven Whitehouse 	} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
8979862ca05SBob Peterson 		gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
89816ca9412SBenjamin Marzinski 		sdp->sd_log_tr = tr;
8999862ca05SBob Peterson 		set_bit(TR_ATTACHED, &tr->tr_flags);
90016ca9412SBenjamin Marzinski 	}
901022ef4feSSteven Whitehouse 
902022ef4feSSteven Whitehouse 	sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
903022ef4feSSteven Whitehouse 	reserved = calc_reserved(sdp);
904022ef4feSSteven Whitehouse 	maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
905022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, maxres >= reserved);
906022ef4feSSteven Whitehouse 	unused = maxres - reserved;
907022ef4feSSteven Whitehouse 	atomic_add(unused, &sdp->sd_log_blks_free);
908022ef4feSSteven Whitehouse 	trace_gfs2_log_blocks(sdp, unused);
909022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
910022ef4feSSteven Whitehouse 			     sdp->sd_jdesc->jd_blocks);
911022ef4feSSteven Whitehouse 	sdp->sd_log_blks_reserved = reserved;
912022ef4feSSteven Whitehouse 
913b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
914b3b94faaSDavid Teigland }
915b3b94faaSDavid Teigland 
916b3b94faaSDavid Teigland /**
917b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
918b3b94faaSDavid Teigland  * @sdp: the filesystem
919b3b94faaSDavid Teigland  * @tr: the transaction
920b3b94faaSDavid Teigland  *
9215e687eacSBenjamin Marzinski  * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
9225e687eacSBenjamin Marzinski  * or the total number of used blocks (pinned blocks plus AIL blocks)
9235e687eacSBenjamin Marzinski  * is greater than thresh2.
9245e687eacSBenjamin Marzinski  *
9255e687eacSBenjamin Marzinski  * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
9265e687eacSBenjamin Marzinski  * journal size.
9275e687eacSBenjamin Marzinski  *
928b3b94faaSDavid Teigland  * Returns: errno
929b3b94faaSDavid Teigland  */
930b3b94faaSDavid Teigland 
931b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
932b3b94faaSDavid Teigland {
933b3b94faaSDavid Teigland 	log_refund(sdp, tr);
934b3b94faaSDavid Teigland 
9355e687eacSBenjamin Marzinski 	if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
9365e687eacSBenjamin Marzinski 	    ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
9375e687eacSBenjamin Marzinski 	    atomic_read(&sdp->sd_log_thresh2)))
9385e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_logd_waitq);
939faa31ce8SSteven Whitehouse }
940b3b94faaSDavid Teigland 
941b3b94faaSDavid Teigland /**
942b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
943b3b94faaSDavid Teigland  * @sdp: the filesystem
944b3b94faaSDavid Teigland  *
945b3b94faaSDavid Teigland  */
946b3b94faaSDavid Teigland 
947b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp)
948b3b94faaSDavid Teigland {
949b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
950b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
951b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
952b3b94faaSDavid Teigland 
953b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
954b3b94faaSDavid Teigland 
955805c0907SBob Peterson 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT | GFS2_LFC_SHUTDOWN);
956b3b94faaSDavid Teigland 
957a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
958a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
959b3b94faaSDavid Teigland 
960b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
961b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
962a25311c8SSteven Whitehouse }
963a25311c8SSteven Whitehouse 
9645e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
9655e687eacSBenjamin Marzinski {
966f07b3520SBob Peterson 	return (atomic_read(&sdp->sd_log_pinned) +
967f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_blks_needed) >=
968f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh1));
9695e687eacSBenjamin Marzinski }
9705e687eacSBenjamin Marzinski 
9715e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
9725e687eacSBenjamin Marzinski {
9735e687eacSBenjamin Marzinski 	unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
974b066a4eeSAbhi Das 
975b066a4eeSAbhi Das 	if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
976b066a4eeSAbhi Das 		return 1;
977b066a4eeSAbhi Das 
978f07b3520SBob Peterson 	return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
979f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh2);
9805e687eacSBenjamin Marzinski }
981ec69b188SSteven Whitehouse 
982ec69b188SSteven Whitehouse /**
983ec69b188SSteven Whitehouse  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
984ec69b188SSteven Whitehouse  * @sdp: Pointer to GFS2 superblock
985ec69b188SSteven Whitehouse  *
986ec69b188SSteven Whitehouse  * Also, periodically check to make sure that we're using the most recent
987ec69b188SSteven Whitehouse  * journal index.
988ec69b188SSteven Whitehouse  */
989ec69b188SSteven Whitehouse 
990ec69b188SSteven Whitehouse int gfs2_logd(void *data)
991ec69b188SSteven Whitehouse {
992ec69b188SSteven Whitehouse 	struct gfs2_sbd *sdp = data;
9935e687eacSBenjamin Marzinski 	unsigned long t = 1;
9945e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
995b63f5e84SBob Peterson 	bool did_flush;
996ec69b188SSteven Whitehouse 
997ec69b188SSteven Whitehouse 	while (!kthread_should_stop()) {
998ec69b188SSteven Whitehouse 
999942b0cddSBob Peterson 		/* Check for errors writing to the journal */
1000942b0cddSBob Peterson 		if (sdp->sd_log_error) {
1001942b0cddSBob Peterson 			gfs2_lm_withdraw(sdp,
1002942b0cddSBob Peterson 					 "GFS2: fsid=%s: error %d: "
1003942b0cddSBob Peterson 					 "withdrawing the file system to "
1004942b0cddSBob Peterson 					 "prevent further damage.\n",
1005942b0cddSBob Peterson 					 sdp->sd_fsname, sdp->sd_log_error);
1006942b0cddSBob Peterson 		}
1007942b0cddSBob Peterson 
1008b63f5e84SBob Peterson 		did_flush = false;
10095e687eacSBenjamin Marzinski 		if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
10104667a0ecSSteven Whitehouse 			gfs2_ail1_empty(sdp);
1011805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1012805c0907SBob Peterson 				       GFS2_LFC_LOGD_JFLUSH_REQD);
1013b63f5e84SBob Peterson 			did_flush = true;
1014ec69b188SSteven Whitehouse 		}
1015ec69b188SSteven Whitehouse 
10165e687eacSBenjamin Marzinski 		if (gfs2_ail_flush_reqd(sdp)) {
10175e687eacSBenjamin Marzinski 			gfs2_ail1_start(sdp);
101826b06a69SSteven Whitehouse 			gfs2_ail1_wait(sdp);
10194667a0ecSSteven Whitehouse 			gfs2_ail1_empty(sdp);
1020805c0907SBob Peterson 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
1021805c0907SBob Peterson 				       GFS2_LFC_LOGD_AIL_FLUSH_REQD);
1022b63f5e84SBob Peterson 			did_flush = true;
10235e687eacSBenjamin Marzinski 		}
10245e687eacSBenjamin Marzinski 
1025b63f5e84SBob Peterson 		if (!gfs2_ail_flush_reqd(sdp) || did_flush)
10265e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_log_waitq);
102726b06a69SSteven Whitehouse 
1028ec69b188SSteven Whitehouse 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
1029a0acae0eSTejun Heo 
1030a0acae0eSTejun Heo 		try_to_freeze();
10315e687eacSBenjamin Marzinski 
10325e687eacSBenjamin Marzinski 		do {
10335e687eacSBenjamin Marzinski 			prepare_to_wait(&sdp->sd_logd_waitq, &wait,
10345f487490SSteven Whitehouse 					TASK_INTERRUPTIBLE);
10355e687eacSBenjamin Marzinski 			if (!gfs2_ail_flush_reqd(sdp) &&
10365e687eacSBenjamin Marzinski 			    !gfs2_jrnl_flush_reqd(sdp) &&
10375e687eacSBenjamin Marzinski 			    !kthread_should_stop())
10385e687eacSBenjamin Marzinski 				t = schedule_timeout(t);
10395e687eacSBenjamin Marzinski 		} while(t && !gfs2_ail_flush_reqd(sdp) &&
10405e687eacSBenjamin Marzinski 			!gfs2_jrnl_flush_reqd(sdp) &&
10415e687eacSBenjamin Marzinski 			!kthread_should_stop());
10425e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_logd_waitq, &wait);
1043ec69b188SSteven Whitehouse 	}
1044ec69b188SSteven Whitehouse 
1045ec69b188SSteven Whitehouse 	return 0;
1046ec69b188SSteven Whitehouse }
1047ec69b188SSteven Whitehouse 
1048