xref: /openbmc/linux/fs/gfs2/log.c (revision 942b0cdd)
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>
17a25311c8SSteven Whitehouse #include <linux/delay.h>
18ec69b188SSteven Whitehouse #include <linux/kthread.h>
19ec69b188SSteven Whitehouse #include <linux/freezer.h>
20254db57fSSteven Whitehouse #include <linux/bio.h>
21885bcecaSSteven Whitehouse #include <linux/blkdev.h>
224667a0ecSSteven Whitehouse #include <linux/writeback.h>
234a36d08dSBob Peterson #include <linux/list_sort.h>
24b3b94faaSDavid Teigland 
25b3b94faaSDavid Teigland #include "gfs2.h"
265c676f6dSSteven Whitehouse #include "incore.h"
27b3b94faaSDavid Teigland #include "bmap.h"
28b3b94faaSDavid Teigland #include "glock.h"
29b3b94faaSDavid Teigland #include "log.h"
30b3b94faaSDavid Teigland #include "lops.h"
31b3b94faaSDavid Teigland #include "meta_io.h"
325c676f6dSSteven Whitehouse #include "util.h"
3371b86f56SSteven Whitehouse #include "dir.h"
3463997775SSteven Whitehouse #include "trace_gfs2.h"
35b3b94faaSDavid Teigland 
36b3b94faaSDavid Teigland /**
37b3b94faaSDavid Teigland  * gfs2_struct2blk - compute stuff
38b3b94faaSDavid Teigland  * @sdp: the filesystem
39b3b94faaSDavid Teigland  * @nstruct: the number of structures
40b3b94faaSDavid Teigland  * @ssize: the size of the structures
41b3b94faaSDavid Teigland  *
42b3b94faaSDavid Teigland  * Compute the number of log descriptor blocks needed to hold a certain number
43b3b94faaSDavid Teigland  * of structures of a certain size.
44b3b94faaSDavid Teigland  *
45b3b94faaSDavid Teigland  * Returns: the number of blocks needed (minimum is always 1)
46b3b94faaSDavid Teigland  */
47b3b94faaSDavid Teigland 
48b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
49b3b94faaSDavid Teigland 			     unsigned int ssize)
50b3b94faaSDavid Teigland {
51b3b94faaSDavid Teigland 	unsigned int blks;
52b3b94faaSDavid Teigland 	unsigned int first, second;
53b3b94faaSDavid Teigland 
54b3b94faaSDavid Teigland 	blks = 1;
55faa31ce8SSteven Whitehouse 	first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
56b3b94faaSDavid Teigland 
57b3b94faaSDavid Teigland 	if (nstruct > first) {
58568f4c96SSteven Whitehouse 		second = (sdp->sd_sb.sb_bsize -
59568f4c96SSteven Whitehouse 			  sizeof(struct gfs2_meta_header)) / ssize;
605c676f6dSSteven Whitehouse 		blks += DIV_ROUND_UP(nstruct - first, second);
61b3b94faaSDavid Teigland 	}
62b3b94faaSDavid Teigland 
63b3b94faaSDavid Teigland 	return blks;
64b3b94faaSDavid Teigland }
65b3b94faaSDavid Teigland 
66ddacfaf7SSteven Whitehouse /**
671e1a3d03SSteven Whitehouse  * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
681e1a3d03SSteven Whitehouse  * @mapping: The associated mapping (maybe NULL)
691e1a3d03SSteven Whitehouse  * @bd: The gfs2_bufdata to remove
701e1a3d03SSteven Whitehouse  *
71c618e87aSSteven Whitehouse  * The ail lock _must_ be held when calling this function
721e1a3d03SSteven Whitehouse  *
731e1a3d03SSteven Whitehouse  */
741e1a3d03SSteven Whitehouse 
75f91a0d3eSSteven Whitehouse void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
761e1a3d03SSteven Whitehouse {
7716ca9412SBenjamin Marzinski 	bd->bd_tr = NULL;
781ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_st_list);
791ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_gl_list);
801e1a3d03SSteven Whitehouse 	atomic_dec(&bd->bd_gl->gl_ail_count);
811e1a3d03SSteven Whitehouse 	brelse(bd->bd_bh);
821e1a3d03SSteven Whitehouse }
831e1a3d03SSteven Whitehouse 
841e1a3d03SSteven Whitehouse /**
85ddacfaf7SSteven Whitehouse  * gfs2_ail1_start_one - Start I/O on a part of the AIL
86ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
874667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
884667a0ecSSteven Whitehouse  * @ai: The ail structure
89ddacfaf7SSteven Whitehouse  *
90ddacfaf7SSteven Whitehouse  */
91ddacfaf7SSteven Whitehouse 
924f1de018SSteven Whitehouse static int gfs2_ail1_start_one(struct gfs2_sbd *sdp,
934667a0ecSSteven Whitehouse 			       struct writeback_control *wbc,
9416ca9412SBenjamin Marzinski 			       struct gfs2_trans *tr)
95d6a079e8SDave Chinner __releases(&sdp->sd_ail_lock)
96d6a079e8SDave Chinner __acquires(&sdp->sd_ail_lock)
97ddacfaf7SSteven Whitehouse {
985ac048bbSSteven Whitehouse 	struct gfs2_glock *gl = NULL;
994667a0ecSSteven Whitehouse 	struct address_space *mapping;
100ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
101ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
102ddacfaf7SSteven Whitehouse 
10316ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list, bd_ail_st_list) {
104ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
105ddacfaf7SSteven Whitehouse 
10616ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
107ddacfaf7SSteven Whitehouse 
108ddacfaf7SSteven Whitehouse 		if (!buffer_busy(bh)) {
10916615be1SSteven Whitehouse 			if (!buffer_uptodate(bh))
110ddacfaf7SSteven Whitehouse 				gfs2_io_error_bh(sdp, bh);
11116ca9412SBenjamin Marzinski 			list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
112ddacfaf7SSteven Whitehouse 			continue;
113ddacfaf7SSteven Whitehouse 		}
114ddacfaf7SSteven Whitehouse 
115ddacfaf7SSteven Whitehouse 		if (!buffer_dirty(bh))
116ddacfaf7SSteven Whitehouse 			continue;
1175ac048bbSSteven Whitehouse 		if (gl == bd->bd_gl)
1185ac048bbSSteven Whitehouse 			continue;
1195ac048bbSSteven Whitehouse 		gl = bd->bd_gl;
12016ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail1_list);
1214667a0ecSSteven Whitehouse 		mapping = bh->b_page->mapping;
1224f1de018SSteven Whitehouse 		if (!mapping)
1234f1de018SSteven Whitehouse 			continue;
124d6a079e8SDave Chinner 		spin_unlock(&sdp->sd_ail_lock);
1254667a0ecSSteven Whitehouse 		generic_writepages(mapping, wbc);
126d6a079e8SDave Chinner 		spin_lock(&sdp->sd_ail_lock);
1274667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
128ddacfaf7SSteven Whitehouse 			break;
1294f1de018SSteven Whitehouse 		return 1;
130ddacfaf7SSteven Whitehouse 	}
1314f1de018SSteven Whitehouse 
1324f1de018SSteven Whitehouse 	return 0;
1334667a0ecSSteven Whitehouse }
1344667a0ecSSteven Whitehouse 
1354667a0ecSSteven Whitehouse 
1364667a0ecSSteven Whitehouse /**
1374667a0ecSSteven Whitehouse  * gfs2_ail1_flush - start writeback of some ail1 entries
1384667a0ecSSteven Whitehouse  * @sdp: The super block
1394667a0ecSSteven Whitehouse  * @wbc: The writeback control structure
1404667a0ecSSteven Whitehouse  *
1414667a0ecSSteven Whitehouse  * Writes back some ail1 entries, according to the limits in the
1424667a0ecSSteven Whitehouse  * writeback control structure
1434667a0ecSSteven Whitehouse  */
1444667a0ecSSteven Whitehouse 
1454667a0ecSSteven Whitehouse void gfs2_ail1_flush(struct gfs2_sbd *sdp, struct writeback_control *wbc)
1464667a0ecSSteven Whitehouse {
1474667a0ecSSteven Whitehouse 	struct list_head *head = &sdp->sd_ail1_list;
14816ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
149885bcecaSSteven Whitehouse 	struct blk_plug plug;
1504667a0ecSSteven Whitehouse 
151c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 1);
152885bcecaSSteven Whitehouse 	blk_start_plug(&plug);
1534667a0ecSSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
1544f1de018SSteven Whitehouse restart:
15516ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, head, tr_list) {
1564667a0ecSSteven Whitehouse 		if (wbc->nr_to_write <= 0)
1574667a0ecSSteven Whitehouse 			break;
15816ca9412SBenjamin Marzinski 		if (gfs2_ail1_start_one(sdp, wbc, tr))
1594f1de018SSteven Whitehouse 			goto restart;
1604667a0ecSSteven Whitehouse 	}
1614667a0ecSSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
162885bcecaSSteven Whitehouse 	blk_finish_plug(&plug);
163c83ae9caSSteven Whitehouse 	trace_gfs2_ail_flush(sdp, wbc, 0);
1644667a0ecSSteven Whitehouse }
1654667a0ecSSteven Whitehouse 
1664667a0ecSSteven Whitehouse /**
1674667a0ecSSteven Whitehouse  * gfs2_ail1_start - start writeback of all ail1 entries
1684667a0ecSSteven Whitehouse  * @sdp: The superblock
1694667a0ecSSteven Whitehouse  */
1704667a0ecSSteven Whitehouse 
1714667a0ecSSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp)
1724667a0ecSSteven Whitehouse {
1734667a0ecSSteven Whitehouse 	struct writeback_control wbc = {
1744667a0ecSSteven Whitehouse 		.sync_mode = WB_SYNC_NONE,
1754667a0ecSSteven Whitehouse 		.nr_to_write = LONG_MAX,
1764667a0ecSSteven Whitehouse 		.range_start = 0,
1774667a0ecSSteven Whitehouse 		.range_end = LLONG_MAX,
1784667a0ecSSteven Whitehouse 	};
1794667a0ecSSteven Whitehouse 
1804667a0ecSSteven Whitehouse 	return gfs2_ail1_flush(sdp, &wbc);
181ddacfaf7SSteven Whitehouse }
182ddacfaf7SSteven Whitehouse 
183ddacfaf7SSteven Whitehouse /**
184ddacfaf7SSteven Whitehouse  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
185ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
186ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
187ddacfaf7SSteven Whitehouse  *
188ddacfaf7SSteven Whitehouse  */
189ddacfaf7SSteven Whitehouse 
19016ca9412SBenjamin Marzinski static void gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
191ddacfaf7SSteven Whitehouse {
192ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
193ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
194ddacfaf7SSteven Whitehouse 
19516ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(bd, s, &tr->tr_ail1_list,
196ddacfaf7SSteven Whitehouse 					 bd_ail_st_list) {
197ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
19816ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
1994667a0ecSSteven Whitehouse 		if (buffer_busy(bh))
200ddacfaf7SSteven Whitehouse 			continue;
201ddacfaf7SSteven Whitehouse 		if (!buffer_uptodate(bh))
202ddacfaf7SSteven Whitehouse 			gfs2_io_error_bh(sdp, bh);
20316ca9412SBenjamin Marzinski 		list_move(&bd->bd_ail_st_list, &tr->tr_ail2_list);
204ddacfaf7SSteven Whitehouse 	}
205ddacfaf7SSteven Whitehouse 
206ddacfaf7SSteven Whitehouse }
207ddacfaf7SSteven Whitehouse 
2084667a0ecSSteven Whitehouse /**
2094667a0ecSSteven Whitehouse  * gfs2_ail1_empty - Try to empty the ail1 lists
2104667a0ecSSteven Whitehouse  * @sdp: The superblock
2114667a0ecSSteven Whitehouse  *
2124667a0ecSSteven Whitehouse  * Tries to empty the ail1 lists, starting with the oldest first
2134667a0ecSSteven Whitehouse  */
214b3b94faaSDavid Teigland 
2154667a0ecSSteven Whitehouse static int gfs2_ail1_empty(struct gfs2_sbd *sdp)
216b3b94faaSDavid Teigland {
21716ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *s;
2185d054964SBenjamin Marzinski 	int oldest_tr = 1;
219b3b94faaSDavid Teigland 	int ret;
220b3b94faaSDavid Teigland 
221d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
22216ca9412SBenjamin Marzinski 	list_for_each_entry_safe_reverse(tr, s, &sdp->sd_ail1_list, tr_list) {
22316ca9412SBenjamin Marzinski 		gfs2_ail1_empty_one(sdp, tr);
2245d054964SBenjamin Marzinski 		if (list_empty(&tr->tr_ail1_list) && oldest_tr)
22516ca9412SBenjamin Marzinski 			list_move(&tr->tr_list, &sdp->sd_ail2_list);
2264667a0ecSSteven Whitehouse 		else
2275d054964SBenjamin Marzinski 			oldest_tr = 0;
228b3b94faaSDavid Teigland 	}
229b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
230d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
231b3b94faaSDavid Teigland 
232b3b94faaSDavid Teigland 	return ret;
233b3b94faaSDavid Teigland }
234b3b94faaSDavid Teigland 
23526b06a69SSteven Whitehouse static void gfs2_ail1_wait(struct gfs2_sbd *sdp)
23626b06a69SSteven Whitehouse {
23716ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
23826b06a69SSteven Whitehouse 	struct gfs2_bufdata *bd;
23926b06a69SSteven Whitehouse 	struct buffer_head *bh;
24026b06a69SSteven Whitehouse 
24126b06a69SSteven Whitehouse 	spin_lock(&sdp->sd_ail_lock);
24216ca9412SBenjamin Marzinski 	list_for_each_entry_reverse(tr, &sdp->sd_ail1_list, tr_list) {
24316ca9412SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail1_list, bd_ail_st_list) {
24426b06a69SSteven Whitehouse 			bh = bd->bd_bh;
24526b06a69SSteven Whitehouse 			if (!buffer_locked(bh))
24626b06a69SSteven Whitehouse 				continue;
24726b06a69SSteven Whitehouse 			get_bh(bh);
24826b06a69SSteven Whitehouse 			spin_unlock(&sdp->sd_ail_lock);
24926b06a69SSteven Whitehouse 			wait_on_buffer(bh);
25026b06a69SSteven Whitehouse 			brelse(bh);
25126b06a69SSteven Whitehouse 			return;
25226b06a69SSteven Whitehouse 		}
25326b06a69SSteven Whitehouse 	}
25426b06a69SSteven Whitehouse 	spin_unlock(&sdp->sd_ail_lock);
25526b06a69SSteven Whitehouse }
256ddacfaf7SSteven Whitehouse 
257ddacfaf7SSteven Whitehouse /**
258ddacfaf7SSteven Whitehouse  * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
259ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
260ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
261ddacfaf7SSteven Whitehouse  *
262ddacfaf7SSteven Whitehouse  */
263ddacfaf7SSteven Whitehouse 
26416ca9412SBenjamin Marzinski static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
265ddacfaf7SSteven Whitehouse {
26616ca9412SBenjamin Marzinski 	struct list_head *head = &tr->tr_ail2_list;
267ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd;
268ddacfaf7SSteven Whitehouse 
269ddacfaf7SSteven Whitehouse 	while (!list_empty(head)) {
270ddacfaf7SSteven Whitehouse 		bd = list_entry(head->prev, struct gfs2_bufdata,
271ddacfaf7SSteven Whitehouse 				bd_ail_st_list);
27216ca9412SBenjamin Marzinski 		gfs2_assert(sdp, bd->bd_tr == tr);
273f91a0d3eSSteven Whitehouse 		gfs2_remove_from_ail(bd);
274ddacfaf7SSteven Whitehouse 	}
275ddacfaf7SSteven Whitehouse }
276ddacfaf7SSteven Whitehouse 
277b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
278b3b94faaSDavid Teigland {
27916ca9412SBenjamin Marzinski 	struct gfs2_trans *tr, *safe;
280b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
281b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
282b3b94faaSDavid Teigland 	int a, b, rm;
283b3b94faaSDavid Teigland 
284d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
285b3b94faaSDavid Teigland 
28616ca9412SBenjamin Marzinski 	list_for_each_entry_safe(tr, safe, &sdp->sd_ail2_list, tr_list) {
28716ca9412SBenjamin Marzinski 		a = (old_tail <= tr->tr_first);
28816ca9412SBenjamin Marzinski 		b = (tr->tr_first < new_tail);
289b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
290b3b94faaSDavid Teigland 		if (!rm)
291b3b94faaSDavid Teigland 			continue;
292b3b94faaSDavid Teigland 
29316ca9412SBenjamin Marzinski 		gfs2_ail2_empty_one(sdp, tr);
29416ca9412SBenjamin Marzinski 		list_del(&tr->tr_list);
29516ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list));
29616ca9412SBenjamin Marzinski 		gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list));
29716ca9412SBenjamin Marzinski 		kfree(tr);
298b3b94faaSDavid Teigland 	}
299b3b94faaSDavid Teigland 
300d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
301b3b94faaSDavid Teigland }
302b3b94faaSDavid Teigland 
303b3b94faaSDavid Teigland /**
30424972557SBenjamin Marzinski  * gfs2_log_release - Release a given number of log blocks
30524972557SBenjamin Marzinski  * @sdp: The GFS2 superblock
30624972557SBenjamin Marzinski  * @blks: The number of blocks
30724972557SBenjamin Marzinski  *
30824972557SBenjamin Marzinski  */
30924972557SBenjamin Marzinski 
31024972557SBenjamin Marzinski void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
31124972557SBenjamin Marzinski {
31224972557SBenjamin Marzinski 
31324972557SBenjamin Marzinski 	atomic_add(blks, &sdp->sd_log_blks_free);
31424972557SBenjamin Marzinski 	trace_gfs2_log_blocks(sdp, blks);
31524972557SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
31624972557SBenjamin Marzinski 				  sdp->sd_jdesc->jd_blocks);
31724972557SBenjamin Marzinski 	up_read(&sdp->sd_log_flush_lock);
31824972557SBenjamin Marzinski }
31924972557SBenjamin Marzinski 
32024972557SBenjamin Marzinski /**
321b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
322b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
323b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
324b3b94faaSDavid Teigland  *
32589918647SSteven Whitehouse  * Note that we never give out the last few blocks of the journal. Thats
3262332c443SRobert Peterson  * due to the fact that there is a small number of header blocks
327b004157aSSteven Whitehouse  * associated with each log flush. The exact number can't be known until
328b004157aSSteven Whitehouse  * flush time, so we ensure that we have just enough free blocks at all
329b004157aSSteven Whitehouse  * times to avoid running out during a log flush.
330b004157aSSteven Whitehouse  *
3315e687eacSBenjamin Marzinski  * We no longer flush the log here, instead we wake up logd to do that
3325e687eacSBenjamin Marzinski  * for us. To avoid the thundering herd and to ensure that we deal fairly
3335e687eacSBenjamin Marzinski  * with queued waiters, we use an exclusive wait. This means that when we
3345e687eacSBenjamin Marzinski  * get woken with enough journal space to get our reservation, we need to
3355e687eacSBenjamin Marzinski  * wake the next waiter on the list.
3365e687eacSBenjamin Marzinski  *
337b3b94faaSDavid Teigland  * Returns: errno
338b3b94faaSDavid Teigland  */
339b3b94faaSDavid Teigland 
340b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
341b3b94faaSDavid Teigland {
3422e60d768SBenjamin Marzinski 	int ret = 0;
3435d054964SBenjamin Marzinski 	unsigned reserved_blks = 7 * (4096 / sdp->sd_vfs->s_blocksize);
3445e687eacSBenjamin Marzinski 	unsigned wanted = blks + reserved_blks;
3455e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
3465e687eacSBenjamin Marzinski 	int did_wait = 0;
3475e687eacSBenjamin Marzinski 	unsigned int free_blocks;
348b3b94faaSDavid Teigland 
349b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
350b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
351b3b94faaSDavid Teigland 		return -EINVAL;
352f07b3520SBob Peterson 	atomic_add(blks, &sdp->sd_log_blks_needed);
3535e687eacSBenjamin Marzinski retry:
3545e687eacSBenjamin Marzinski 	free_blocks = atomic_read(&sdp->sd_log_blks_free);
3555e687eacSBenjamin Marzinski 	if (unlikely(free_blocks <= wanted)) {
3565e687eacSBenjamin Marzinski 		do {
3575e687eacSBenjamin Marzinski 			prepare_to_wait_exclusive(&sdp->sd_log_waitq, &wait,
3585e687eacSBenjamin Marzinski 					TASK_UNINTERRUPTIBLE);
3595e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_logd_waitq);
3605e687eacSBenjamin Marzinski 			did_wait = 1;
3615e687eacSBenjamin Marzinski 			if (atomic_read(&sdp->sd_log_blks_free) <= wanted)
3625e687eacSBenjamin Marzinski 				io_schedule();
3635e687eacSBenjamin Marzinski 			free_blocks = atomic_read(&sdp->sd_log_blks_free);
3645e687eacSBenjamin Marzinski 		} while(free_blocks <= wanted);
3655e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_log_waitq, &wait);
366b3b94faaSDavid Teigland 	}
3672e60d768SBenjamin Marzinski 	atomic_inc(&sdp->sd_reserving_log);
3685e687eacSBenjamin Marzinski 	if (atomic_cmpxchg(&sdp->sd_log_blks_free, free_blocks,
3692e60d768SBenjamin Marzinski 				free_blocks - blks) != free_blocks) {
3702e60d768SBenjamin Marzinski 		if (atomic_dec_and_test(&sdp->sd_reserving_log))
3712e60d768SBenjamin Marzinski 			wake_up(&sdp->sd_reserving_log_wait);
3725e687eacSBenjamin Marzinski 		goto retry;
3732e60d768SBenjamin Marzinski 	}
374f07b3520SBob Peterson 	atomic_sub(blks, &sdp->sd_log_blks_needed);
37563997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, -blks);
3765e687eacSBenjamin Marzinski 
3775e687eacSBenjamin Marzinski 	/*
3785e687eacSBenjamin Marzinski 	 * If we waited, then so might others, wake them up _after_ we get
3795e687eacSBenjamin Marzinski 	 * our share of the log.
3805e687eacSBenjamin Marzinski 	 */
3815e687eacSBenjamin Marzinski 	if (unlikely(did_wait))
3825e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_log_waitq);
383484adff8SSteven Whitehouse 
384484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
38524972557SBenjamin Marzinski 	if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) {
38624972557SBenjamin Marzinski 		gfs2_log_release(sdp, blks);
3872e60d768SBenjamin Marzinski 		ret = -EROFS;
38824972557SBenjamin Marzinski 	}
3892e60d768SBenjamin Marzinski 	if (atomic_dec_and_test(&sdp->sd_reserving_log))
3902e60d768SBenjamin Marzinski 		wake_up(&sdp->sd_reserving_log_wait);
3912e60d768SBenjamin Marzinski 	return ret;
392b3b94faaSDavid Teigland }
393b3b94faaSDavid Teigland 
394b3b94faaSDavid Teigland /**
395b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
396b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
397b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
398b3b94faaSDavid Teigland  * @older: The older journal block of the pair
399b3b94faaSDavid Teigland  *
400b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
401b3b94faaSDavid Teigland  *   blocks in the journal
402b3b94faaSDavid Teigland  *
403b3b94faaSDavid Teigland  * Returns: the distance in blocks
404b3b94faaSDavid Teigland  */
405b3b94faaSDavid Teigland 
406faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
407b3b94faaSDavid Teigland 					unsigned int older)
408b3b94faaSDavid Teigland {
409b3b94faaSDavid Teigland 	int dist;
410b3b94faaSDavid Teigland 
411b3b94faaSDavid Teigland 	dist = newer - older;
412b3b94faaSDavid Teigland 	if (dist < 0)
413b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
414b3b94faaSDavid Teigland 
415b3b94faaSDavid Teigland 	return dist;
416b3b94faaSDavid Teigland }
417b3b94faaSDavid Teigland 
4182332c443SRobert Peterson /**
4192332c443SRobert Peterson  * calc_reserved - Calculate the number of blocks to reserve when
4202332c443SRobert Peterson  *                 refunding a transaction's unused buffers.
4212332c443SRobert Peterson  * @sdp: The GFS2 superblock
4222332c443SRobert Peterson  *
4232332c443SRobert Peterson  * This is complex.  We need to reserve room for all our currently used
4242332c443SRobert Peterson  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
4252332c443SRobert Peterson  * all our journaled data buffers for journaled files (e.g. files in the
4262332c443SRobert Peterson  * meta_fs like rindex, or files for which chattr +j was done.)
4272332c443SRobert Peterson  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
4282332c443SRobert Peterson  * will count it as free space (sd_log_blks_free) and corruption will follow.
4292332c443SRobert Peterson  *
4302332c443SRobert Peterson  * We can have metadata bufs and jdata bufs in the same journal.  So each
4312332c443SRobert Peterson  * type gets its own log header, for which we need to reserve a block.
4322332c443SRobert Peterson  * In fact, each type has the potential for needing more than one header
4332332c443SRobert Peterson  * in cases where we have more buffers than will fit on a journal page.
4342332c443SRobert Peterson  * Metadata journal entries take up half the space of journaled buffer entries.
4352332c443SRobert Peterson  * Thus, metadata entries have buf_limit (502) and journaled buffers have
4362332c443SRobert Peterson  * databuf_limit (251) before they cause a wrap around.
4372332c443SRobert Peterson  *
4382332c443SRobert Peterson  * Also, we need to reserve blocks for revoke journal entries and one for an
4392332c443SRobert Peterson  * overall header for the lot.
4402332c443SRobert Peterson  *
4412332c443SRobert Peterson  * Returns: the number of blocks reserved
4422332c443SRobert Peterson  */
4432332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp)
4442332c443SRobert Peterson {
4452332c443SRobert Peterson 	unsigned int reserved = 0;
446022ef4feSSteven Whitehouse 	unsigned int mbuf;
447022ef4feSSteven Whitehouse 	unsigned int dbuf;
448022ef4feSSteven Whitehouse 	struct gfs2_trans *tr = sdp->sd_log_tr;
4492332c443SRobert Peterson 
450022ef4feSSteven Whitehouse 	if (tr) {
451022ef4feSSteven Whitehouse 		mbuf = tr->tr_num_buf_new - tr->tr_num_buf_rm;
452022ef4feSSteven Whitehouse 		dbuf = tr->tr_num_databuf_new - tr->tr_num_databuf_rm;
453022ef4feSSteven Whitehouse 		reserved = mbuf + dbuf;
454022ef4feSSteven Whitehouse 		/* Account for header blocks */
455022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(mbuf, buf_limit(sdp));
456022ef4feSSteven Whitehouse 		reserved += DIV_ROUND_UP(dbuf, databuf_limit(sdp));
457022ef4feSSteven Whitehouse 	}
4582332c443SRobert Peterson 
4592e95e3f6SBenjamin Marzinski 	if (sdp->sd_log_commited_revoke > 0)
460022ef4feSSteven Whitehouse 		reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
4612332c443SRobert Peterson 					  sizeof(u64));
4622332c443SRobert Peterson 	/* One for the overall header */
4632332c443SRobert Peterson 	if (reserved)
4642332c443SRobert Peterson 		reserved++;
4652332c443SRobert Peterson 	return reserved;
4662332c443SRobert Peterson }
4672332c443SRobert Peterson 
468b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
469b3b94faaSDavid Teigland {
47016ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
471b3b94faaSDavid Teigland 	unsigned int tail;
472b3b94faaSDavid Teigland 
473d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
474b3b94faaSDavid Teigland 
475faa31ce8SSteven Whitehouse 	if (list_empty(&sdp->sd_ail1_list)) {
476b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
477faa31ce8SSteven Whitehouse 	} else {
47816ca9412SBenjamin Marzinski 		tr = list_entry(sdp->sd_ail1_list.prev, struct gfs2_trans,
47916ca9412SBenjamin Marzinski 				tr_list);
48016ca9412SBenjamin Marzinski 		tail = tr->tr_first;
481b3b94faaSDavid Teigland 	}
482b3b94faaSDavid Teigland 
483d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
484b3b94faaSDavid Teigland 
485b3b94faaSDavid Teigland 	return tail;
486b3b94faaSDavid Teigland }
487b3b94faaSDavid Teigland 
4882332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
489b3b94faaSDavid Teigland {
490b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
491b3b94faaSDavid Teigland 
492b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
493b3b94faaSDavid Teigland 
494fd041f0bSSteven Whitehouse 	atomic_add(dist, &sdp->sd_log_blks_free);
49563997775SSteven Whitehouse 	trace_gfs2_log_blocks(sdp, dist);
4965e687eacSBenjamin Marzinski 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
4975e687eacSBenjamin Marzinski 			     sdp->sd_jdesc->jd_blocks);
498b3b94faaSDavid Teigland 
499b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
500b3b94faaSDavid Teigland }
501b3b94faaSDavid Teigland 
502b3b94faaSDavid Teigland 
50334cc1781SSteven Whitehouse static void log_flush_wait(struct gfs2_sbd *sdp)
504b3b94faaSDavid Teigland {
50516615be1SSteven Whitehouse 	DEFINE_WAIT(wait);
506b3b94faaSDavid Teigland 
50716615be1SSteven Whitehouse 	if (atomic_read(&sdp->sd_log_in_flight)) {
50816615be1SSteven Whitehouse 		do {
50916615be1SSteven Whitehouse 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
51016615be1SSteven Whitehouse 					TASK_UNINTERRUPTIBLE);
51116615be1SSteven Whitehouse 			if (atomic_read(&sdp->sd_log_in_flight))
51216615be1SSteven Whitehouse 				io_schedule();
51316615be1SSteven Whitehouse 		} while(atomic_read(&sdp->sd_log_in_flight));
51416615be1SSteven Whitehouse 		finish_wait(&sdp->sd_log_flush_wait, &wait);
515b3b94faaSDavid Teigland 	}
516b3b94faaSDavid Teigland }
517b3b94faaSDavid Teigland 
51845138990SSteven Whitehouse static int ip_cmp(void *priv, struct list_head *a, struct list_head *b)
5194a36d08dSBob Peterson {
52045138990SSteven Whitehouse 	struct gfs2_inode *ipa, *ipb;
5214a36d08dSBob Peterson 
52245138990SSteven Whitehouse 	ipa = list_entry(a, struct gfs2_inode, i_ordered);
52345138990SSteven Whitehouse 	ipb = list_entry(b, struct gfs2_inode, i_ordered);
5244a36d08dSBob Peterson 
52545138990SSteven Whitehouse 	if (ipa->i_no_addr < ipb->i_no_addr)
5264a36d08dSBob Peterson 		return -1;
52745138990SSteven Whitehouse 	if (ipa->i_no_addr > ipb->i_no_addr)
5284a36d08dSBob Peterson 		return 1;
5294a36d08dSBob Peterson 	return 0;
5304a36d08dSBob Peterson }
5314a36d08dSBob Peterson 
532d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp)
533d7b616e2SSteven Whitehouse {
53445138990SSteven Whitehouse 	struct gfs2_inode *ip;
535d7b616e2SSteven Whitehouse 	LIST_HEAD(written);
536d7b616e2SSteven Whitehouse 
53745138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
53845138990SSteven Whitehouse 	list_sort(NULL, &sdp->sd_log_le_ordered, &ip_cmp);
539d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
54045138990SSteven Whitehouse 		ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
54145138990SSteven Whitehouse 		list_move(&ip->i_ordered, &written);
54245138990SSteven Whitehouse 		if (ip->i_inode.i_mapping->nrpages == 0)
543d7b616e2SSteven Whitehouse 			continue;
54445138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
54545138990SSteven Whitehouse 		filemap_fdatawrite(ip->i_inode.i_mapping);
54645138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
547d7b616e2SSteven Whitehouse 	}
548d7b616e2SSteven Whitehouse 	list_splice(&written, &sdp->sd_log_le_ordered);
54945138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
550d7b616e2SSteven Whitehouse }
551d7b616e2SSteven Whitehouse 
552d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
553d7b616e2SSteven Whitehouse {
55445138990SSteven Whitehouse 	struct gfs2_inode *ip;
555d7b616e2SSteven Whitehouse 
55645138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
557d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
55845138990SSteven Whitehouse 		ip = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_inode, i_ordered);
55945138990SSteven Whitehouse 		list_del(&ip->i_ordered);
56045138990SSteven Whitehouse 		WARN_ON(!test_and_clear_bit(GIF_ORDERED, &ip->i_flags));
56145138990SSteven Whitehouse 		if (ip->i_inode.i_mapping->nrpages == 0)
562d7b616e2SSteven Whitehouse 			continue;
56345138990SSteven Whitehouse 		spin_unlock(&sdp->sd_ordered_lock);
56445138990SSteven Whitehouse 		filemap_fdatawait(ip->i_inode.i_mapping);
56545138990SSteven Whitehouse 		spin_lock(&sdp->sd_ordered_lock);
566d7b616e2SSteven Whitehouse 	}
56745138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
568d7b616e2SSteven Whitehouse }
56945138990SSteven Whitehouse 
57045138990SSteven Whitehouse void gfs2_ordered_del_inode(struct gfs2_inode *ip)
57145138990SSteven Whitehouse {
57245138990SSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
57345138990SSteven Whitehouse 
57445138990SSteven Whitehouse 	spin_lock(&sdp->sd_ordered_lock);
57545138990SSteven Whitehouse 	if (test_and_clear_bit(GIF_ORDERED, &ip->i_flags))
57645138990SSteven Whitehouse 		list_del(&ip->i_ordered);
57745138990SSteven Whitehouse 	spin_unlock(&sdp->sd_ordered_lock);
578d7b616e2SSteven Whitehouse }
579d7b616e2SSteven Whitehouse 
5805d054964SBenjamin Marzinski void gfs2_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd)
5815d054964SBenjamin Marzinski {
5825d054964SBenjamin Marzinski 	struct buffer_head *bh = bd->bd_bh;
5835d054964SBenjamin Marzinski 	struct gfs2_glock *gl = bd->bd_gl;
5845d054964SBenjamin Marzinski 
5855d054964SBenjamin Marzinski 	bh->b_private = NULL;
5865d054964SBenjamin Marzinski 	bd->bd_blkno = bh->b_blocknr;
5879290a9a7SBob Peterson 	gfs2_remove_from_ail(bd); /* drops ref on bh */
5889290a9a7SBob Peterson 	bd->bd_bh = NULL;
5895d054964SBenjamin Marzinski 	bd->bd_ops = &gfs2_revoke_lops;
5905d054964SBenjamin Marzinski 	sdp->sd_log_num_revoke++;
5915d054964SBenjamin Marzinski 	atomic_inc(&gl->gl_revokes);
5925d054964SBenjamin Marzinski 	set_bit(GLF_LFLUSH, &gl->gl_flags);
5935d054964SBenjamin Marzinski 	list_add(&bd->bd_list, &sdp->sd_log_le_revoke);
5945d054964SBenjamin Marzinski }
5955d054964SBenjamin Marzinski 
5965d054964SBenjamin Marzinski void gfs2_write_revokes(struct gfs2_sbd *sdp)
5975d054964SBenjamin Marzinski {
5985d054964SBenjamin Marzinski 	struct gfs2_trans *tr;
5995d054964SBenjamin Marzinski 	struct gfs2_bufdata *bd, *tmp;
6005d054964SBenjamin Marzinski 	int have_revokes = 0;
6015d054964SBenjamin Marzinski 	int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
6025d054964SBenjamin Marzinski 
6035d054964SBenjamin Marzinski 	gfs2_ail1_empty(sdp);
6045d054964SBenjamin Marzinski 	spin_lock(&sdp->sd_ail_lock);
6055d054964SBenjamin Marzinski 	list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
6065d054964SBenjamin Marzinski 		list_for_each_entry(bd, &tr->tr_ail2_list, bd_ail_st_list) {
6075d054964SBenjamin Marzinski 			if (list_empty(&bd->bd_list)) {
6085d054964SBenjamin Marzinski 				have_revokes = 1;
6095d054964SBenjamin Marzinski 				goto done;
6105d054964SBenjamin Marzinski 			}
6115d054964SBenjamin Marzinski 		}
6125d054964SBenjamin Marzinski 	}
6135d054964SBenjamin Marzinski done:
6145d054964SBenjamin Marzinski 	spin_unlock(&sdp->sd_ail_lock);
6155d054964SBenjamin Marzinski 	if (have_revokes == 0)
6165d054964SBenjamin Marzinski 		return;
6175d054964SBenjamin Marzinski 	while (sdp->sd_log_num_revoke > max_revokes)
6185d054964SBenjamin Marzinski 		max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
6195d054964SBenjamin Marzinski 	max_revokes -= sdp->sd_log_num_revoke;
6205d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6215d054964SBenjamin Marzinski 		atomic_dec(&sdp->sd_log_blks_free);
6225d054964SBenjamin Marzinski 		/* If no blocks have been reserved, we need to also
6235d054964SBenjamin Marzinski 		 * reserve a block for the header */
6245d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6255d054964SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free);
6265d054964SBenjamin Marzinski 	}
6275d054964SBenjamin Marzinski 	gfs2_log_lock(sdp);
6285d054964SBenjamin Marzinski 	spin_lock(&sdp->sd_ail_lock);
6295d054964SBenjamin Marzinski 	list_for_each_entry(tr, &sdp->sd_ail1_list, tr_list) {
6305d054964SBenjamin Marzinski 		list_for_each_entry_safe(bd, tmp, &tr->tr_ail2_list, bd_ail_st_list) {
6315d054964SBenjamin Marzinski 			if (max_revokes == 0)
6325d054964SBenjamin Marzinski 				goto out_of_blocks;
6335d054964SBenjamin Marzinski 			if (!list_empty(&bd->bd_list))
6345d054964SBenjamin Marzinski 				continue;
6355d054964SBenjamin Marzinski 			gfs2_add_revoke(sdp, bd);
6365d054964SBenjamin Marzinski 			max_revokes--;
6375d054964SBenjamin Marzinski 		}
6385d054964SBenjamin Marzinski 	}
6395d054964SBenjamin Marzinski out_of_blocks:
6405d054964SBenjamin Marzinski 	spin_unlock(&sdp->sd_ail_lock);
6415d054964SBenjamin Marzinski 	gfs2_log_unlock(sdp);
6425d054964SBenjamin Marzinski 
6435d054964SBenjamin Marzinski 	if (!sdp->sd_log_num_revoke) {
6445d054964SBenjamin Marzinski 		atomic_inc(&sdp->sd_log_blks_free);
6455d054964SBenjamin Marzinski 		if (!sdp->sd_log_blks_reserved)
6465d054964SBenjamin Marzinski 			atomic_inc(&sdp->sd_log_blks_free);
6475d054964SBenjamin Marzinski 	}
6485d054964SBenjamin Marzinski }
6495d054964SBenjamin Marzinski 
650b3b94faaSDavid Teigland /**
65134cc1781SSteven Whitehouse  * log_write_header - Get and initialize a journal header buffer
65234cc1781SSteven Whitehouse  * @sdp: The GFS2 superblock
65334cc1781SSteven Whitehouse  *
65434cc1781SSteven Whitehouse  * Returns: the initialized log buffer descriptor
65534cc1781SSteven Whitehouse  */
65634cc1781SSteven Whitehouse 
657fdb76a42SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags)
65834cc1781SSteven Whitehouse {
65934cc1781SSteven Whitehouse 	struct gfs2_log_header *lh;
66034cc1781SSteven Whitehouse 	unsigned int tail;
66134cc1781SSteven Whitehouse 	u32 hash;
6620f0b9b63SJan Kara 	int op_flags = REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC;
663e8c92ed7SSteven Whitehouse 	struct page *page = mempool_alloc(gfs2_page_pool, GFP_NOIO);
6642e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
665e8c92ed7SSteven Whitehouse 	lh = page_address(page);
666e8c92ed7SSteven Whitehouse 	clear_page(lh);
66734cc1781SSteven Whitehouse 
6682e60d768SBenjamin Marzinski 	gfs2_assert_withdraw(sdp, (state != SFS_FROZEN));
6692e60d768SBenjamin Marzinski 
67034cc1781SSteven Whitehouse 	tail = current_tail(sdp);
67134cc1781SSteven Whitehouse 
67234cc1781SSteven Whitehouse 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
67334cc1781SSteven Whitehouse 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
67434cc1781SSteven Whitehouse 	lh->lh_header.__pad0 = cpu_to_be64(0);
67534cc1781SSteven Whitehouse 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
67634cc1781SSteven Whitehouse 	lh->lh_header.mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid);
67734cc1781SSteven Whitehouse 	lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
67834cc1781SSteven Whitehouse 	lh->lh_flags = cpu_to_be32(flags);
67934cc1781SSteven Whitehouse 	lh->lh_tail = cpu_to_be32(tail);
68034cc1781SSteven Whitehouse 	lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
681e8c92ed7SSteven Whitehouse 	hash = gfs2_disk_hash(page_address(page), sizeof(struct gfs2_log_header));
68234cc1781SSteven Whitehouse 	lh->lh_hash = cpu_to_be32(hash);
68334cc1781SSteven Whitehouse 
68434cc1781SSteven Whitehouse 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) {
68534cc1781SSteven Whitehouse 		gfs2_ordered_wait(sdp);
68634cc1781SSteven Whitehouse 		log_flush_wait(sdp);
68770fd7614SChristoph Hellwig 		op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
68834cc1781SSteven Whitehouse 	}
68934cc1781SSteven Whitehouse 
690e8c92ed7SSteven Whitehouse 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
691e8c92ed7SSteven Whitehouse 	gfs2_log_write_page(sdp, page);
692e1b1afa6SMike Christie 	gfs2_log_flush_bio(sdp, REQ_OP_WRITE, op_flags);
693e8c92ed7SSteven Whitehouse 	log_flush_wait(sdp);
69434cc1781SSteven Whitehouse 
69534cc1781SSteven Whitehouse 	if (sdp->sd_log_tail != tail)
69634cc1781SSteven Whitehouse 		log_pull_tail(sdp, tail);
69734cc1781SSteven Whitehouse }
69834cc1781SSteven Whitehouse 
69934cc1781SSteven Whitehouse /**
700b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
701b3b94faaSDavid Teigland  * @sdp: the filesystem
702b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
703b3b94faaSDavid Teigland  *
704b3b94faaSDavid Teigland  */
705b3b94faaSDavid Teigland 
70624972557SBenjamin Marzinski void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl,
70724972557SBenjamin Marzinski 		    enum gfs2_flush_type type)
708b3b94faaSDavid Teigland {
70916ca9412SBenjamin Marzinski 	struct gfs2_trans *tr;
7102e60d768SBenjamin Marzinski 	enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state);
711b3b94faaSDavid Teigland 
712484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
713f55ab26aSSteven Whitehouse 
7142bcd610dSSteven Whitehouse 	/* Log might have been flushed while we waited for the flush lock */
7152bcd610dSSteven Whitehouse 	if (gl && !test_bit(GLF_LFLUSH, &gl->gl_flags)) {
716484adff8SSteven Whitehouse 		up_write(&sdp->sd_log_flush_lock);
717f55ab26aSSteven Whitehouse 		return;
718f55ab26aSSteven Whitehouse 	}
71963997775SSteven Whitehouse 	trace_gfs2_log_flush(sdp, 1);
720f55ab26aSSteven Whitehouse 
721400ac52eSBenjamin Marzinski 	if (type == SHUTDOWN_FLUSH)
722400ac52eSBenjamin Marzinski 		clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
723400ac52eSBenjamin Marzinski 
724b1ab1e44SSteven Whitehouse 	sdp->sd_log_flush_head = sdp->sd_log_head;
72516ca9412SBenjamin Marzinski 	tr = sdp->sd_log_tr;
72616ca9412SBenjamin Marzinski 	if (tr) {
72716ca9412SBenjamin Marzinski 		sdp->sd_log_tr = NULL;
72816ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail1_list);
72916ca9412SBenjamin Marzinski 		INIT_LIST_HEAD(&tr->tr_ail2_list);
730b1ab1e44SSteven Whitehouse 		tr->tr_first = sdp->sd_log_flush_head;
7312e60d768SBenjamin Marzinski 		if (unlikely (state == SFS_FROZEN))
7322e60d768SBenjamin Marzinski 			gfs2_assert_withdraw(sdp, !tr->tr_num_buf_new && !tr->tr_num_databuf_new);
73316ca9412SBenjamin Marzinski 	}
734b3b94faaSDavid Teigland 
7352e60d768SBenjamin Marzinski 	if (unlikely(state == SFS_FROZEN))
7362e60d768SBenjamin Marzinski 		gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
737b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
738b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
739b3b94faaSDavid Teigland 
740d7b616e2SSteven Whitehouse 	gfs2_ordered_write(sdp);
741d69a3c65SSteven Whitehouse 	lops_before_commit(sdp, tr);
742e1b1afa6SMike Christie 	gfs2_log_flush_bio(sdp, REQ_OP_WRITE, 0);
743d7b616e2SSteven Whitehouse 
74434cc1781SSteven Whitehouse 	if (sdp->sd_log_head != sdp->sd_log_flush_head) {
745428fd95dSBob Peterson 		log_flush_wait(sdp);
746fdb76a42SSteven Whitehouse 		log_write_header(sdp, 0);
74734cc1781SSteven Whitehouse 	} else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
748fd041f0bSSteven Whitehouse 		atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
74963997775SSteven Whitehouse 		trace_gfs2_log_blocks(sdp, -1);
750fdb76a42SSteven Whitehouse 		log_write_header(sdp, 0);
7512332c443SRobert Peterson 	}
75216ca9412SBenjamin Marzinski 	lops_after_commit(sdp, tr);
753fe1a698fSSteven Whitehouse 
754fe1a698fSSteven Whitehouse 	gfs2_log_lock(sdp);
755b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
756faa31ce8SSteven Whitehouse 	sdp->sd_log_blks_reserved = 0;
757b3b94faaSDavid Teigland 	sdp->sd_log_commited_revoke = 0;
758b3b94faaSDavid Teigland 
759d6a079e8SDave Chinner 	spin_lock(&sdp->sd_ail_lock);
76016ca9412SBenjamin Marzinski 	if (tr && !list_empty(&tr->tr_ail1_list)) {
76116ca9412SBenjamin Marzinski 		list_add(&tr->tr_list, &sdp->sd_ail1_list);
76216ca9412SBenjamin Marzinski 		tr = NULL;
763b3b94faaSDavid Teigland 	}
764d6a079e8SDave Chinner 	spin_unlock(&sdp->sd_ail_lock);
765b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
76624972557SBenjamin Marzinski 
76724972557SBenjamin Marzinski 	if (type != NORMAL_FLUSH) {
76824972557SBenjamin Marzinski 		if (!sdp->sd_log_idle) {
76924972557SBenjamin Marzinski 			for (;;) {
77024972557SBenjamin Marzinski 				gfs2_ail1_start(sdp);
77124972557SBenjamin Marzinski 				gfs2_ail1_wait(sdp);
77224972557SBenjamin Marzinski 				if (gfs2_ail1_empty(sdp))
77324972557SBenjamin Marzinski 					break;
77424972557SBenjamin Marzinski 			}
77524972557SBenjamin Marzinski 			atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
77624972557SBenjamin Marzinski 			trace_gfs2_log_blocks(sdp, -1);
77724972557SBenjamin Marzinski 			log_write_header(sdp, 0);
77824972557SBenjamin Marzinski 			sdp->sd_log_head = sdp->sd_log_flush_head;
77924972557SBenjamin Marzinski 		}
78024972557SBenjamin Marzinski 		if (type == SHUTDOWN_FLUSH || type == FREEZE_FLUSH)
78124972557SBenjamin Marzinski 			gfs2_log_shutdown(sdp);
7822e60d768SBenjamin Marzinski 		if (type == FREEZE_FLUSH)
7832e60d768SBenjamin Marzinski 			atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
78424972557SBenjamin Marzinski 	}
78524972557SBenjamin Marzinski 
78663997775SSteven Whitehouse 	trace_gfs2_log_flush(sdp, 0);
787484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
788b3b94faaSDavid Teigland 
78916ca9412SBenjamin Marzinski 	kfree(tr);
790b3b94faaSDavid Teigland }
791b3b94faaSDavid Teigland 
792d69a3c65SSteven Whitehouse /**
793d69a3c65SSteven Whitehouse  * gfs2_merge_trans - Merge a new transaction into a cached transaction
794d69a3c65SSteven Whitehouse  * @old: Original transaction to be expanded
795d69a3c65SSteven Whitehouse  * @new: New transaction to be merged
796d69a3c65SSteven Whitehouse  */
797d69a3c65SSteven Whitehouse 
798d69a3c65SSteven Whitehouse static void gfs2_merge_trans(struct gfs2_trans *old, struct gfs2_trans *new)
799d69a3c65SSteven Whitehouse {
8009862ca05SBob Peterson 	WARN_ON_ONCE(!test_bit(TR_ATTACHED, &old->tr_flags));
801d69a3c65SSteven Whitehouse 
802d69a3c65SSteven Whitehouse 	old->tr_num_buf_new	+= new->tr_num_buf_new;
803d69a3c65SSteven Whitehouse 	old->tr_num_databuf_new	+= new->tr_num_databuf_new;
804d69a3c65SSteven Whitehouse 	old->tr_num_buf_rm	+= new->tr_num_buf_rm;
805d69a3c65SSteven Whitehouse 	old->tr_num_databuf_rm	+= new->tr_num_databuf_rm;
806d69a3c65SSteven Whitehouse 	old->tr_num_revoke	+= new->tr_num_revoke;
807d69a3c65SSteven Whitehouse 	old->tr_num_revoke_rm	+= new->tr_num_revoke_rm;
808d69a3c65SSteven Whitehouse 
809d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_databuf, &old->tr_databuf);
810d69a3c65SSteven Whitehouse 	list_splice_tail_init(&new->tr_buf, &old->tr_buf);
811d69a3c65SSteven Whitehouse }
812d69a3c65SSteven Whitehouse 
813b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
814b3b94faaSDavid Teigland {
8152332c443SRobert Peterson 	unsigned int reserved;
816ac39aaddSSteven Whitehouse 	unsigned int unused;
817022ef4feSSteven Whitehouse 	unsigned int maxres;
818b3b94faaSDavid Teigland 
819b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
820b3b94faaSDavid Teigland 
821d69a3c65SSteven Whitehouse 	if (sdp->sd_log_tr) {
822d69a3c65SSteven Whitehouse 		gfs2_merge_trans(sdp->sd_log_tr, tr);
823d69a3c65SSteven Whitehouse 	} else if (tr->tr_num_buf_new || tr->tr_num_databuf_new) {
8249862ca05SBob Peterson 		gfs2_assert_withdraw(sdp, test_bit(TR_ALLOCED, &tr->tr_flags));
82516ca9412SBenjamin Marzinski 		sdp->sd_log_tr = tr;
8269862ca05SBob Peterson 		set_bit(TR_ATTACHED, &tr->tr_flags);
82716ca9412SBenjamin Marzinski 	}
828022ef4feSSteven Whitehouse 
829022ef4feSSteven Whitehouse 	sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
830022ef4feSSteven Whitehouse 	reserved = calc_reserved(sdp);
831022ef4feSSteven Whitehouse 	maxres = sdp->sd_log_blks_reserved + tr->tr_reserved;
832022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, maxres >= reserved);
833022ef4feSSteven Whitehouse 	unused = maxres - reserved;
834022ef4feSSteven Whitehouse 	atomic_add(unused, &sdp->sd_log_blks_free);
835022ef4feSSteven Whitehouse 	trace_gfs2_log_blocks(sdp, unused);
836022ef4feSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
837022ef4feSSteven Whitehouse 			     sdp->sd_jdesc->jd_blocks);
838022ef4feSSteven Whitehouse 	sdp->sd_log_blks_reserved = reserved;
839022ef4feSSteven Whitehouse 
840b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
841b3b94faaSDavid Teigland }
842b3b94faaSDavid Teigland 
843b3b94faaSDavid Teigland /**
844b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
845b3b94faaSDavid Teigland  * @sdp: the filesystem
846b3b94faaSDavid Teigland  * @tr: the transaction
847b3b94faaSDavid Teigland  *
8485e687eacSBenjamin Marzinski  * We wake up gfs2_logd if the number of pinned blocks exceed thresh1
8495e687eacSBenjamin Marzinski  * or the total number of used blocks (pinned blocks plus AIL blocks)
8505e687eacSBenjamin Marzinski  * is greater than thresh2.
8515e687eacSBenjamin Marzinski  *
8525e687eacSBenjamin Marzinski  * At mount time thresh1 is 1/3rd of journal size, thresh2 is 2/3rd of
8535e687eacSBenjamin Marzinski  * journal size.
8545e687eacSBenjamin Marzinski  *
855b3b94faaSDavid Teigland  * Returns: errno
856b3b94faaSDavid Teigland  */
857b3b94faaSDavid Teigland 
858b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
859b3b94faaSDavid Teigland {
860b3b94faaSDavid Teigland 	log_refund(sdp, tr);
861b3b94faaSDavid Teigland 
8625e687eacSBenjamin Marzinski 	if (atomic_read(&sdp->sd_log_pinned) > atomic_read(&sdp->sd_log_thresh1) ||
8635e687eacSBenjamin Marzinski 	    ((sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free)) >
8645e687eacSBenjamin Marzinski 	    atomic_read(&sdp->sd_log_thresh2)))
8655e687eacSBenjamin Marzinski 		wake_up(&sdp->sd_logd_waitq);
866faa31ce8SSteven Whitehouse }
867b3b94faaSDavid Teigland 
868b3b94faaSDavid Teigland /**
869b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
870b3b94faaSDavid Teigland  * @sdp: the filesystem
871b3b94faaSDavid Teigland  *
872b3b94faaSDavid Teigland  */
873b3b94faaSDavid Teigland 
874b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp)
875b3b94faaSDavid Teigland {
876b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
877b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
878b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
879b3b94faaSDavid Teigland 
880b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
881b3b94faaSDavid Teigland 
882fdb76a42SSteven Whitehouse 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT);
883b3b94faaSDavid Teigland 
884a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
885a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
886b3b94faaSDavid Teigland 
887b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
888b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
889a25311c8SSteven Whitehouse }
890a25311c8SSteven Whitehouse 
8915e687eacSBenjamin Marzinski static inline int gfs2_jrnl_flush_reqd(struct gfs2_sbd *sdp)
8925e687eacSBenjamin Marzinski {
893f07b3520SBob Peterson 	return (atomic_read(&sdp->sd_log_pinned) +
894f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_blks_needed) >=
895f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh1));
8965e687eacSBenjamin Marzinski }
8975e687eacSBenjamin Marzinski 
8985e687eacSBenjamin Marzinski static inline int gfs2_ail_flush_reqd(struct gfs2_sbd *sdp)
8995e687eacSBenjamin Marzinski {
9005e687eacSBenjamin Marzinski 	unsigned int used_blocks = sdp->sd_jdesc->jd_blocks - atomic_read(&sdp->sd_log_blks_free);
901b066a4eeSAbhi Das 
902b066a4eeSAbhi Das 	if (test_and_clear_bit(SDF_FORCE_AIL_FLUSH, &sdp->sd_flags))
903b066a4eeSAbhi Das 		return 1;
904b066a4eeSAbhi Das 
905f07b3520SBob Peterson 	return used_blocks + atomic_read(&sdp->sd_log_blks_needed) >=
906f07b3520SBob Peterson 		atomic_read(&sdp->sd_log_thresh2);
9075e687eacSBenjamin Marzinski }
908ec69b188SSteven Whitehouse 
909ec69b188SSteven Whitehouse /**
910ec69b188SSteven Whitehouse  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
911ec69b188SSteven Whitehouse  * @sdp: Pointer to GFS2 superblock
912ec69b188SSteven Whitehouse  *
913ec69b188SSteven Whitehouse  * Also, periodically check to make sure that we're using the most recent
914ec69b188SSteven Whitehouse  * journal index.
915ec69b188SSteven Whitehouse  */
916ec69b188SSteven Whitehouse 
917ec69b188SSteven Whitehouse int gfs2_logd(void *data)
918ec69b188SSteven Whitehouse {
919ec69b188SSteven Whitehouse 	struct gfs2_sbd *sdp = data;
9205e687eacSBenjamin Marzinski 	unsigned long t = 1;
9215e687eacSBenjamin Marzinski 	DEFINE_WAIT(wait);
922b63f5e84SBob Peterson 	bool did_flush;
923ec69b188SSteven Whitehouse 
924ec69b188SSteven Whitehouse 	while (!kthread_should_stop()) {
925ec69b188SSteven Whitehouse 
926942b0cddSBob Peterson 		/* Check for errors writing to the journal */
927942b0cddSBob Peterson 		if (sdp->sd_log_error) {
928942b0cddSBob Peterson 			gfs2_lm_withdraw(sdp,
929942b0cddSBob Peterson 					 "GFS2: fsid=%s: error %d: "
930942b0cddSBob Peterson 					 "withdrawing the file system to "
931942b0cddSBob Peterson 					 "prevent further damage.\n",
932942b0cddSBob Peterson 					 sdp->sd_fsname, sdp->sd_log_error);
933942b0cddSBob Peterson 		}
934942b0cddSBob Peterson 
935b63f5e84SBob Peterson 		did_flush = false;
9365e687eacSBenjamin Marzinski 		if (gfs2_jrnl_flush_reqd(sdp) || t == 0) {
9374667a0ecSSteven Whitehouse 			gfs2_ail1_empty(sdp);
93824972557SBenjamin Marzinski 			gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
939b63f5e84SBob Peterson 			did_flush = true;
940ec69b188SSteven Whitehouse 		}
941ec69b188SSteven Whitehouse 
9425e687eacSBenjamin Marzinski 		if (gfs2_ail_flush_reqd(sdp)) {
9435e687eacSBenjamin Marzinski 			gfs2_ail1_start(sdp);
94426b06a69SSteven Whitehouse 			gfs2_ail1_wait(sdp);
9454667a0ecSSteven Whitehouse 			gfs2_ail1_empty(sdp);
94624972557SBenjamin Marzinski 			gfs2_log_flush(sdp, NULL, NORMAL_FLUSH);
947b63f5e84SBob Peterson 			did_flush = true;
9485e687eacSBenjamin Marzinski 		}
9495e687eacSBenjamin Marzinski 
950b63f5e84SBob Peterson 		if (!gfs2_ail_flush_reqd(sdp) || did_flush)
9515e687eacSBenjamin Marzinski 			wake_up(&sdp->sd_log_waitq);
95226b06a69SSteven Whitehouse 
953ec69b188SSteven Whitehouse 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
954a0acae0eSTejun Heo 
955a0acae0eSTejun Heo 		try_to_freeze();
9565e687eacSBenjamin Marzinski 
9575e687eacSBenjamin Marzinski 		do {
9585e687eacSBenjamin Marzinski 			prepare_to_wait(&sdp->sd_logd_waitq, &wait,
9595f487490SSteven Whitehouse 					TASK_INTERRUPTIBLE);
9605e687eacSBenjamin Marzinski 			if (!gfs2_ail_flush_reqd(sdp) &&
9615e687eacSBenjamin Marzinski 			    !gfs2_jrnl_flush_reqd(sdp) &&
9625e687eacSBenjamin Marzinski 			    !kthread_should_stop())
9635e687eacSBenjamin Marzinski 				t = schedule_timeout(t);
9645e687eacSBenjamin Marzinski 		} while(t && !gfs2_ail_flush_reqd(sdp) &&
9655e687eacSBenjamin Marzinski 			!gfs2_jrnl_flush_reqd(sdp) &&
9665e687eacSBenjamin Marzinski 			!kthread_should_stop());
9675e687eacSBenjamin Marzinski 		finish_wait(&sdp->sd_logd_waitq, &wait);
968ec69b188SSteven Whitehouse 	}
969ec69b188SSteven Whitehouse 
970ec69b188SSteven Whitehouse 	return 0;
971ec69b188SSteven Whitehouse }
972ec69b188SSteven Whitehouse 
973