xref: /openbmc/linux/fs/gfs2/log.c (revision 254db57f)
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>
177d308590SFabio Massimo Di Nitto #include <linux/lm_interface.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>
22b3b94faaSDavid Teigland 
23b3b94faaSDavid Teigland #include "gfs2.h"
245c676f6dSSteven Whitehouse #include "incore.h"
25b3b94faaSDavid Teigland #include "bmap.h"
26b3b94faaSDavid Teigland #include "glock.h"
27b3b94faaSDavid Teigland #include "log.h"
28b3b94faaSDavid Teigland #include "lops.h"
29b3b94faaSDavid Teigland #include "meta_io.h"
305c676f6dSSteven Whitehouse #include "util.h"
3171b86f56SSteven Whitehouse #include "dir.h"
32b3b94faaSDavid Teigland 
33b3b94faaSDavid Teigland #define PULL 1
34b3b94faaSDavid Teigland 
35b3b94faaSDavid Teigland /**
36b3b94faaSDavid Teigland  * gfs2_struct2blk - compute stuff
37b3b94faaSDavid Teigland  * @sdp: the filesystem
38b3b94faaSDavid Teigland  * @nstruct: the number of structures
39b3b94faaSDavid Teigland  * @ssize: the size of the structures
40b3b94faaSDavid Teigland  *
41b3b94faaSDavid Teigland  * Compute the number of log descriptor blocks needed to hold a certain number
42b3b94faaSDavid Teigland  * of structures of a certain size.
43b3b94faaSDavid Teigland  *
44b3b94faaSDavid Teigland  * Returns: the number of blocks needed (minimum is always 1)
45b3b94faaSDavid Teigland  */
46b3b94faaSDavid Teigland 
47b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
48b3b94faaSDavid Teigland 			     unsigned int ssize)
49b3b94faaSDavid Teigland {
50b3b94faaSDavid Teigland 	unsigned int blks;
51b3b94faaSDavid Teigland 	unsigned int first, second;
52b3b94faaSDavid Teigland 
53b3b94faaSDavid Teigland 	blks = 1;
54faa31ce8SSteven Whitehouse 	first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / ssize;
55b3b94faaSDavid Teigland 
56b3b94faaSDavid Teigland 	if (nstruct > first) {
57568f4c96SSteven Whitehouse 		second = (sdp->sd_sb.sb_bsize -
58568f4c96SSteven Whitehouse 			  sizeof(struct gfs2_meta_header)) / ssize;
595c676f6dSSteven Whitehouse 		blks += DIV_ROUND_UP(nstruct - first, second);
60b3b94faaSDavid Teigland 	}
61b3b94faaSDavid Teigland 
62b3b94faaSDavid Teigland 	return blks;
63b3b94faaSDavid Teigland }
64b3b94faaSDavid Teigland 
65ddacfaf7SSteven Whitehouse /**
661e1a3d03SSteven Whitehouse  * gfs2_remove_from_ail - Remove an entry from the ail lists, updating counters
671e1a3d03SSteven Whitehouse  * @mapping: The associated mapping (maybe NULL)
681e1a3d03SSteven Whitehouse  * @bd: The gfs2_bufdata to remove
691e1a3d03SSteven Whitehouse  *
701e1a3d03SSteven Whitehouse  * The log lock _must_ be held when calling this function
711e1a3d03SSteven Whitehouse  *
721e1a3d03SSteven Whitehouse  */
731e1a3d03SSteven Whitehouse 
74f91a0d3eSSteven Whitehouse void gfs2_remove_from_ail(struct gfs2_bufdata *bd)
751e1a3d03SSteven Whitehouse {
761e1a3d03SSteven Whitehouse 	bd->bd_ail = NULL;
771ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_st_list);
781ad38c43SSteven Whitehouse 	list_del_init(&bd->bd_ail_gl_list);
791e1a3d03SSteven Whitehouse 	atomic_dec(&bd->bd_gl->gl_ail_count);
801e1a3d03SSteven Whitehouse 	brelse(bd->bd_bh);
811e1a3d03SSteven Whitehouse }
821e1a3d03SSteven Whitehouse 
831e1a3d03SSteven Whitehouse /**
84ddacfaf7SSteven Whitehouse  * gfs2_ail1_start_one - Start I/O on a part of the AIL
85ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
86ddacfaf7SSteven Whitehouse  * @tr: the part of the AIL
87ddacfaf7SSteven Whitehouse  *
88ddacfaf7SSteven Whitehouse  */
89ddacfaf7SSteven Whitehouse 
90ddacfaf7SSteven Whitehouse static void gfs2_ail1_start_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
912d81afb8SHarvey Harrison __releases(&sdp->sd_log_lock)
922d81afb8SHarvey Harrison __acquires(&sdp->sd_log_lock)
93ddacfaf7SSteven Whitehouse {
94ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
95ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
96ddacfaf7SSteven Whitehouse 	int retry;
97ddacfaf7SSteven Whitehouse 
98ddacfaf7SSteven Whitehouse 	do {
99ddacfaf7SSteven Whitehouse 		retry = 0;
100ddacfaf7SSteven Whitehouse 
101ddacfaf7SSteven Whitehouse 		list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
102ddacfaf7SSteven Whitehouse 						 bd_ail_st_list) {
103ddacfaf7SSteven Whitehouse 			bh = bd->bd_bh;
104ddacfaf7SSteven Whitehouse 
105ddacfaf7SSteven Whitehouse 			gfs2_assert(sdp, bd->bd_ail == ai);
106ddacfaf7SSteven Whitehouse 
107ddacfaf7SSteven Whitehouse 			if (!buffer_busy(bh)) {
10816615be1SSteven Whitehouse 				if (!buffer_uptodate(bh))
109ddacfaf7SSteven Whitehouse 					gfs2_io_error_bh(sdp, bh);
110ddacfaf7SSteven Whitehouse 				list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
111ddacfaf7SSteven Whitehouse 				continue;
112ddacfaf7SSteven Whitehouse 			}
113ddacfaf7SSteven Whitehouse 
114ddacfaf7SSteven Whitehouse 			if (!buffer_dirty(bh))
115ddacfaf7SSteven Whitehouse 				continue;
116ddacfaf7SSteven Whitehouse 
117ddacfaf7SSteven Whitehouse 			list_move(&bd->bd_ail_st_list, &ai->ai_ail1_list);
118ddacfaf7SSteven Whitehouse 
11916615be1SSteven Whitehouse 			get_bh(bh);
120ddacfaf7SSteven Whitehouse 			gfs2_log_unlock(sdp);
12116615be1SSteven Whitehouse 			lock_buffer(bh);
12216615be1SSteven Whitehouse 			if (test_clear_buffer_dirty(bh)) {
12316615be1SSteven Whitehouse 				bh->b_end_io = end_buffer_write_sync;
12416615be1SSteven Whitehouse 				submit_bh(WRITE, bh);
12516615be1SSteven Whitehouse 			} else {
12616615be1SSteven Whitehouse 				unlock_buffer(bh);
12716615be1SSteven Whitehouse 				brelse(bh);
12816615be1SSteven Whitehouse 			}
129ddacfaf7SSteven Whitehouse 			gfs2_log_lock(sdp);
130ddacfaf7SSteven Whitehouse 
131ddacfaf7SSteven Whitehouse 			retry = 1;
132ddacfaf7SSteven Whitehouse 			break;
133ddacfaf7SSteven Whitehouse 		}
134ddacfaf7SSteven Whitehouse 	} while (retry);
135ddacfaf7SSteven Whitehouse }
136ddacfaf7SSteven Whitehouse 
137ddacfaf7SSteven Whitehouse /**
138ddacfaf7SSteven Whitehouse  * gfs2_ail1_empty_one - Check whether or not a trans in the AIL has been synced
139ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
140ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
141ddacfaf7SSteven Whitehouse  *
142ddacfaf7SSteven Whitehouse  */
143ddacfaf7SSteven Whitehouse 
144ddacfaf7SSteven Whitehouse static int gfs2_ail1_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai, int flags)
145ddacfaf7SSteven Whitehouse {
146ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd, *s;
147ddacfaf7SSteven Whitehouse 	struct buffer_head *bh;
148ddacfaf7SSteven Whitehouse 
149ddacfaf7SSteven Whitehouse 	list_for_each_entry_safe_reverse(bd, s, &ai->ai_ail1_list,
150ddacfaf7SSteven Whitehouse 					 bd_ail_st_list) {
151ddacfaf7SSteven Whitehouse 		bh = bd->bd_bh;
152ddacfaf7SSteven Whitehouse 
153ddacfaf7SSteven Whitehouse 		gfs2_assert(sdp, bd->bd_ail == ai);
154ddacfaf7SSteven Whitehouse 
155ddacfaf7SSteven Whitehouse 		if (buffer_busy(bh)) {
156ddacfaf7SSteven Whitehouse 			if (flags & DIO_ALL)
157ddacfaf7SSteven Whitehouse 				continue;
158ddacfaf7SSteven Whitehouse 			else
159ddacfaf7SSteven Whitehouse 				break;
160ddacfaf7SSteven Whitehouse 		}
161ddacfaf7SSteven Whitehouse 
162ddacfaf7SSteven Whitehouse 		if (!buffer_uptodate(bh))
163ddacfaf7SSteven Whitehouse 			gfs2_io_error_bh(sdp, bh);
164ddacfaf7SSteven Whitehouse 
165ddacfaf7SSteven Whitehouse 		list_move(&bd->bd_ail_st_list, &ai->ai_ail2_list);
166ddacfaf7SSteven Whitehouse 	}
167ddacfaf7SSteven Whitehouse 
168ddacfaf7SSteven Whitehouse 	return list_empty(&ai->ai_ail1_list);
169ddacfaf7SSteven Whitehouse }
170ddacfaf7SSteven Whitehouse 
171a25311c8SSteven Whitehouse static void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
172b3b94faaSDavid Teigland {
173693ddeabSBob Peterson 	struct list_head *head;
174cd915493SSteven Whitehouse 	u64 sync_gen;
17574669416SSteven Whitehouse 	struct list_head *first;
17674669416SSteven Whitehouse 	struct gfs2_ail *first_ai, *ai, *tmp;
17774669416SSteven Whitehouse 	int done = 0;
178b3b94faaSDavid Teigland 
179b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
180693ddeabSBob Peterson 	head = &sdp->sd_ail1_list;
181b3b94faaSDavid Teigland 	if (list_empty(head)) {
182b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
183b3b94faaSDavid Teigland 		return;
184b3b94faaSDavid Teigland 	}
185b3b94faaSDavid Teigland 	sync_gen = sdp->sd_ail_sync_gen++;
186b3b94faaSDavid Teigland 
187b3b94faaSDavid Teigland 	first = head->prev;
188b3b94faaSDavid Teigland 	first_ai = list_entry(first, struct gfs2_ail, ai_list);
189b3b94faaSDavid Teigland 	first_ai->ai_sync_gen = sync_gen;
19074669416SSteven Whitehouse 	gfs2_ail1_start_one(sdp, first_ai); /* This may drop log lock */
191b3b94faaSDavid Teigland 
192b3b94faaSDavid Teigland 	if (flags & DIO_ALL)
193b3b94faaSDavid Teigland 		first = NULL;
194b3b94faaSDavid Teigland 
19574669416SSteven Whitehouse 	while(!done) {
196484adff8SSteven Whitehouse 		if (first && (head->prev != first ||
197b3b94faaSDavid Teigland 			      gfs2_ail1_empty_one(sdp, first_ai, 0)))
198b3b94faaSDavid Teigland 			break;
199b3b94faaSDavid Teigland 
20074669416SSteven Whitehouse 		done = 1;
20174669416SSteven Whitehouse 		list_for_each_entry_safe_reverse(ai, tmp, head, ai_list) {
202b3b94faaSDavid Teigland 			if (ai->ai_sync_gen >= sync_gen)
203b3b94faaSDavid Teigland 				continue;
204b3b94faaSDavid Teigland 			ai->ai_sync_gen = sync_gen;
20574669416SSteven Whitehouse 			gfs2_ail1_start_one(sdp, ai); /* This may drop log lock */
20674669416SSteven Whitehouse 			done = 0;
207b3b94faaSDavid Teigland 			break;
208b3b94faaSDavid Teigland 		}
209b3b94faaSDavid Teigland 	}
210b3b94faaSDavid Teigland 
211b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
212b3b94faaSDavid Teigland }
213b3b94faaSDavid Teigland 
214ec69b188SSteven Whitehouse static int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
215b3b94faaSDavid Teigland {
216b3b94faaSDavid Teigland 	struct gfs2_ail *ai, *s;
217b3b94faaSDavid Teigland 	int ret;
218b3b94faaSDavid Teigland 
219b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
220b3b94faaSDavid Teigland 
221b3b94faaSDavid Teigland 	list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
222b3b94faaSDavid Teigland 		if (gfs2_ail1_empty_one(sdp, ai, flags))
223b3b94faaSDavid Teigland 			list_move(&ai->ai_list, &sdp->sd_ail2_list);
224b3b94faaSDavid Teigland 		else if (!(flags & DIO_ALL))
225b3b94faaSDavid Teigland 			break;
226b3b94faaSDavid Teigland 	}
227b3b94faaSDavid Teigland 
228b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
229b3b94faaSDavid Teigland 
230b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
231b3b94faaSDavid Teigland 
232b3b94faaSDavid Teigland 	return ret;
233b3b94faaSDavid Teigland }
234b3b94faaSDavid Teigland 
235ddacfaf7SSteven Whitehouse 
236ddacfaf7SSteven Whitehouse /**
237ddacfaf7SSteven Whitehouse  * gfs2_ail2_empty_one - Check whether or not a trans in the AIL has been synced
238ddacfaf7SSteven Whitehouse  * @sdp: the filesystem
239ddacfaf7SSteven Whitehouse  * @ai: the AIL entry
240ddacfaf7SSteven Whitehouse  *
241ddacfaf7SSteven Whitehouse  */
242ddacfaf7SSteven Whitehouse 
243ddacfaf7SSteven Whitehouse static void gfs2_ail2_empty_one(struct gfs2_sbd *sdp, struct gfs2_ail *ai)
244ddacfaf7SSteven Whitehouse {
245ddacfaf7SSteven Whitehouse 	struct list_head *head = &ai->ai_ail2_list;
246ddacfaf7SSteven Whitehouse 	struct gfs2_bufdata *bd;
247ddacfaf7SSteven Whitehouse 
248ddacfaf7SSteven Whitehouse 	while (!list_empty(head)) {
249ddacfaf7SSteven Whitehouse 		bd = list_entry(head->prev, struct gfs2_bufdata,
250ddacfaf7SSteven Whitehouse 				bd_ail_st_list);
251ddacfaf7SSteven Whitehouse 		gfs2_assert(sdp, bd->bd_ail == ai);
252f91a0d3eSSteven Whitehouse 		gfs2_remove_from_ail(bd);
253ddacfaf7SSteven Whitehouse 	}
254ddacfaf7SSteven Whitehouse }
255ddacfaf7SSteven Whitehouse 
256b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
257b3b94faaSDavid Teigland {
258b3b94faaSDavid Teigland 	struct gfs2_ail *ai, *safe;
259b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
260b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
261b3b94faaSDavid Teigland 	int a, b, rm;
262b3b94faaSDavid Teigland 
263b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
264b3b94faaSDavid Teigland 
265b3b94faaSDavid Teigland 	list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
266b3b94faaSDavid Teigland 		a = (old_tail <= ai->ai_first);
267b3b94faaSDavid Teigland 		b = (ai->ai_first < new_tail);
268b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
269b3b94faaSDavid Teigland 		if (!rm)
270b3b94faaSDavid Teigland 			continue;
271b3b94faaSDavid Teigland 
272b3b94faaSDavid Teigland 		gfs2_ail2_empty_one(sdp, ai);
273b3b94faaSDavid Teigland 		list_del(&ai->ai_list);
274b3b94faaSDavid Teigland 		gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
275b3b94faaSDavid Teigland 		gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
276b3b94faaSDavid Teigland 		kfree(ai);
277b3b94faaSDavid Teigland 	}
278b3b94faaSDavid Teigland 
279b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
280b3b94faaSDavid Teigland }
281b3b94faaSDavid Teigland 
282b3b94faaSDavid Teigland /**
283b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
284b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
285b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
286b3b94faaSDavid Teigland  *
28789918647SSteven Whitehouse  * Note that we never give out the last few blocks of the journal. Thats
2882332c443SRobert Peterson  * due to the fact that there is a small number of header blocks
289b004157aSSteven Whitehouse  * associated with each log flush. The exact number can't be known until
290b004157aSSteven Whitehouse  * flush time, so we ensure that we have just enough free blocks at all
291b004157aSSteven Whitehouse  * times to avoid running out during a log flush.
292b004157aSSteven Whitehouse  *
293b3b94faaSDavid Teigland  * Returns: errno
294b3b94faaSDavid Teigland  */
295b3b94faaSDavid Teigland 
296b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
297b3b94faaSDavid Teigland {
298b3b94faaSDavid Teigland 	unsigned int try = 0;
29989918647SSteven Whitehouse 	unsigned reserved_blks = 6 * (4096 / sdp->sd_vfs->s_blocksize);
300b3b94faaSDavid Teigland 
301b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
302b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
303b3b94faaSDavid Teigland 		return -EINVAL;
304b3b94faaSDavid Teigland 
30571b86f56SSteven Whitehouse 	mutex_lock(&sdp->sd_log_reserve_mutex);
306b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
307fd041f0bSSteven Whitehouse 	while(atomic_read(&sdp->sd_log_blks_free) <= (blks + reserved_blks)) {
308b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
309b3b94faaSDavid Teigland 		gfs2_ail1_empty(sdp, 0);
310b09e593dSSteven Whitehouse 		gfs2_log_flush(sdp, NULL);
311b3b94faaSDavid Teigland 
312b3b94faaSDavid Teigland 		if (try++)
313b3b94faaSDavid Teigland 			gfs2_ail1_start(sdp, 0);
314484adff8SSteven Whitehouse 		gfs2_log_lock(sdp);
315b3b94faaSDavid Teigland 	}
316fd041f0bSSteven Whitehouse 	atomic_sub(blks, &sdp->sd_log_blks_free);
317484adff8SSteven Whitehouse 	gfs2_log_unlock(sdp);
318484adff8SSteven Whitehouse 	mutex_unlock(&sdp->sd_log_reserve_mutex);
319484adff8SSteven Whitehouse 
320484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
321b3b94faaSDavid Teigland 
322b3b94faaSDavid Teigland 	return 0;
323b3b94faaSDavid Teigland }
324b3b94faaSDavid Teigland 
325b3b94faaSDavid Teigland /**
326b3b94faaSDavid Teigland  * gfs2_log_release - Release a given number of log blocks
327b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
328b3b94faaSDavid Teigland  * @blks: The number of blocks
329b3b94faaSDavid Teigland  *
330b3b94faaSDavid Teigland  */
331b3b94faaSDavid Teigland 
332b3b94faaSDavid Teigland void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
333b3b94faaSDavid Teigland {
334b3b94faaSDavid Teigland 
335b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
336fd041f0bSSteven Whitehouse 	atomic_add(blks, &sdp->sd_log_blks_free);
337b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
338fd041f0bSSteven Whitehouse 			     atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
339b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
340ed386507SSteven Whitehouse 	up_read(&sdp->sd_log_flush_lock);
341b3b94faaSDavid Teigland }
342b3b94faaSDavid Teigland 
343cd915493SSteven Whitehouse static u64 log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
344b3b94faaSDavid Teigland {
345da6dd40dSBob Peterson 	struct gfs2_journal_extent *je;
346b3b94faaSDavid Teigland 
347da6dd40dSBob Peterson 	list_for_each_entry(je, &sdp->sd_jdesc->extent_list, extent_list) {
348da6dd40dSBob Peterson 		if (lbn >= je->lblock && lbn < je->lblock + je->blocks)
349ff91cc9bSSteven Whitehouse 			return je->dblock + lbn - je->lblock;
350da6dd40dSBob Peterson 	}
351b3b94faaSDavid Teigland 
352da6dd40dSBob Peterson 	return -1;
353b3b94faaSDavid Teigland }
354b3b94faaSDavid Teigland 
355b3b94faaSDavid Teigland /**
356b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
357b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
358b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
359b3b94faaSDavid Teigland  * @older: The older journal block of the pair
360b3b94faaSDavid Teigland  *
361b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
362b3b94faaSDavid Teigland  *   blocks in the journal
363b3b94faaSDavid Teigland  *
364b3b94faaSDavid Teigland  * Returns: the distance in blocks
365b3b94faaSDavid Teigland  */
366b3b94faaSDavid Teigland 
367faa31ce8SSteven Whitehouse static inline unsigned int log_distance(struct gfs2_sbd *sdp, unsigned int newer,
368b3b94faaSDavid Teigland 					unsigned int older)
369b3b94faaSDavid Teigland {
370b3b94faaSDavid Teigland 	int dist;
371b3b94faaSDavid Teigland 
372b3b94faaSDavid Teigland 	dist = newer - older;
373b3b94faaSDavid Teigland 	if (dist < 0)
374b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
375b3b94faaSDavid Teigland 
376b3b94faaSDavid Teigland 	return dist;
377b3b94faaSDavid Teigland }
378b3b94faaSDavid Teigland 
3792332c443SRobert Peterson /**
3802332c443SRobert Peterson  * calc_reserved - Calculate the number of blocks to reserve when
3812332c443SRobert Peterson  *                 refunding a transaction's unused buffers.
3822332c443SRobert Peterson  * @sdp: The GFS2 superblock
3832332c443SRobert Peterson  *
3842332c443SRobert Peterson  * This is complex.  We need to reserve room for all our currently used
3852332c443SRobert Peterson  * metadata buffers (e.g. normal file I/O rewriting file time stamps) and
3862332c443SRobert Peterson  * all our journaled data buffers for journaled files (e.g. files in the
3872332c443SRobert Peterson  * meta_fs like rindex, or files for which chattr +j was done.)
3882332c443SRobert Peterson  * If we don't reserve enough space, gfs2_log_refund and gfs2_log_flush
3892332c443SRobert Peterson  * will count it as free space (sd_log_blks_free) and corruption will follow.
3902332c443SRobert Peterson  *
3912332c443SRobert Peterson  * We can have metadata bufs and jdata bufs in the same journal.  So each
3922332c443SRobert Peterson  * type gets its own log header, for which we need to reserve a block.
3932332c443SRobert Peterson  * In fact, each type has the potential for needing more than one header
3942332c443SRobert Peterson  * in cases where we have more buffers than will fit on a journal page.
3952332c443SRobert Peterson  * Metadata journal entries take up half the space of journaled buffer entries.
3962332c443SRobert Peterson  * Thus, metadata entries have buf_limit (502) and journaled buffers have
3972332c443SRobert Peterson  * databuf_limit (251) before they cause a wrap around.
3982332c443SRobert Peterson  *
3992332c443SRobert Peterson  * Also, we need to reserve blocks for revoke journal entries and one for an
4002332c443SRobert Peterson  * overall header for the lot.
4012332c443SRobert Peterson  *
4022332c443SRobert Peterson  * Returns: the number of blocks reserved
4032332c443SRobert Peterson  */
4042332c443SRobert Peterson static unsigned int calc_reserved(struct gfs2_sbd *sdp)
4052332c443SRobert Peterson {
4062332c443SRobert Peterson 	unsigned int reserved = 0;
4072332c443SRobert Peterson 	unsigned int mbuf_limit, metabufhdrs_needed;
4082332c443SRobert Peterson 	unsigned int dbuf_limit, databufhdrs_needed;
4092332c443SRobert Peterson 	unsigned int revokes = 0;
4102332c443SRobert Peterson 
4112332c443SRobert Peterson 	mbuf_limit = buf_limit(sdp);
4122332c443SRobert Peterson 	metabufhdrs_needed = (sdp->sd_log_commited_buf +
4132332c443SRobert Peterson 			      (mbuf_limit - 1)) / mbuf_limit;
4142332c443SRobert Peterson 	dbuf_limit = databuf_limit(sdp);
4152332c443SRobert Peterson 	databufhdrs_needed = (sdp->sd_log_commited_databuf +
4162332c443SRobert Peterson 			      (dbuf_limit - 1)) / dbuf_limit;
4172332c443SRobert Peterson 
4182332c443SRobert Peterson 	if (sdp->sd_log_commited_revoke)
4192332c443SRobert Peterson 		revokes = gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
4202332c443SRobert Peterson 					  sizeof(u64));
4212332c443SRobert Peterson 
4222332c443SRobert Peterson 	reserved = sdp->sd_log_commited_buf + metabufhdrs_needed +
4232332c443SRobert Peterson 		sdp->sd_log_commited_databuf + databufhdrs_needed +
4242332c443SRobert Peterson 		revokes;
4252332c443SRobert Peterson 	/* One for the overall header */
4262332c443SRobert Peterson 	if (reserved)
4272332c443SRobert Peterson 		reserved++;
4282332c443SRobert Peterson 	return reserved;
4292332c443SRobert Peterson }
4302332c443SRobert Peterson 
431b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
432b3b94faaSDavid Teigland {
433b3b94faaSDavid Teigland 	struct gfs2_ail *ai;
434b3b94faaSDavid Teigland 	unsigned int tail;
435b3b94faaSDavid Teigland 
436b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
437b3b94faaSDavid Teigland 
438faa31ce8SSteven Whitehouse 	if (list_empty(&sdp->sd_ail1_list)) {
439b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
440faa31ce8SSteven Whitehouse 	} else {
441faa31ce8SSteven Whitehouse 		ai = list_entry(sdp->sd_ail1_list.prev, struct gfs2_ail, ai_list);
442b3b94faaSDavid Teigland 		tail = ai->ai_first;
443b3b94faaSDavid Teigland 	}
444b3b94faaSDavid Teigland 
445b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
446b3b94faaSDavid Teigland 
447b3b94faaSDavid Teigland 	return tail;
448b3b94faaSDavid Teigland }
449b3b94faaSDavid Teigland 
45016615be1SSteven Whitehouse void gfs2_log_incr_head(struct gfs2_sbd *sdp)
451b3b94faaSDavid Teigland {
452b3b94faaSDavid Teigland 	if (sdp->sd_log_flush_head == sdp->sd_log_tail)
45316615be1SSteven Whitehouse 		BUG_ON(sdp->sd_log_flush_head != sdp->sd_log_head);
454b3b94faaSDavid Teigland 
455b3b94faaSDavid Teigland 	if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
456b3b94faaSDavid Teigland 		sdp->sd_log_flush_head = 0;
457b3b94faaSDavid Teigland 		sdp->sd_log_flush_wrapped = 1;
458b3b94faaSDavid Teigland 	}
459b3b94faaSDavid Teigland }
460b3b94faaSDavid Teigland 
461b3b94faaSDavid Teigland /**
46216615be1SSteven Whitehouse  * gfs2_log_write_endio - End of I/O for a log buffer
46316615be1SSteven Whitehouse  * @bh: The buffer head
46416615be1SSteven Whitehouse  * @uptodate: I/O Status
46516615be1SSteven Whitehouse  *
46616615be1SSteven Whitehouse  */
46716615be1SSteven Whitehouse 
46816615be1SSteven Whitehouse static void gfs2_log_write_endio(struct buffer_head *bh, int uptodate)
46916615be1SSteven Whitehouse {
47016615be1SSteven Whitehouse 	struct gfs2_sbd *sdp = bh->b_private;
47116615be1SSteven Whitehouse 	bh->b_private = NULL;
47216615be1SSteven Whitehouse 
47316615be1SSteven Whitehouse 	end_buffer_write_sync(bh, uptodate);
47416615be1SSteven Whitehouse 	if (atomic_dec_and_test(&sdp->sd_log_in_flight))
47516615be1SSteven Whitehouse 		wake_up(&sdp->sd_log_flush_wait);
47616615be1SSteven Whitehouse }
47716615be1SSteven Whitehouse 
47816615be1SSteven Whitehouse /**
479b3b94faaSDavid Teigland  * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
480b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
481b3b94faaSDavid Teigland  *
482b3b94faaSDavid Teigland  * Returns: the buffer_head
483b3b94faaSDavid Teigland  */
484b3b94faaSDavid Teigland 
485b3b94faaSDavid Teigland struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
486b3b94faaSDavid Teigland {
487cd915493SSteven Whitehouse 	u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
488b3b94faaSDavid Teigland 	struct buffer_head *bh;
489b3b94faaSDavid Teigland 
49016615be1SSteven Whitehouse 	bh = sb_getblk(sdp->sd_vfs, blkno);
491b3b94faaSDavid Teigland 	lock_buffer(bh);
492b3b94faaSDavid Teigland 	memset(bh->b_data, 0, bh->b_size);
493b3b94faaSDavid Teigland 	set_buffer_uptodate(bh);
494b3b94faaSDavid Teigland 	clear_buffer_dirty(bh);
49516615be1SSteven Whitehouse 	gfs2_log_incr_head(sdp);
49616615be1SSteven Whitehouse 	atomic_inc(&sdp->sd_log_in_flight);
49716615be1SSteven Whitehouse 	bh->b_private = sdp;
49816615be1SSteven Whitehouse 	bh->b_end_io = gfs2_log_write_endio;
499b3b94faaSDavid Teigland 
500b3b94faaSDavid Teigland 	return bh;
501b3b94faaSDavid Teigland }
502b3b94faaSDavid Teigland 
503b3b94faaSDavid Teigland /**
50416615be1SSteven Whitehouse  * gfs2_fake_write_endio -
50516615be1SSteven Whitehouse  * @bh: The buffer head
50616615be1SSteven Whitehouse  * @uptodate: The I/O Status
50716615be1SSteven Whitehouse  *
50816615be1SSteven Whitehouse  */
50916615be1SSteven Whitehouse 
51016615be1SSteven Whitehouse static void gfs2_fake_write_endio(struct buffer_head *bh, int uptodate)
51116615be1SSteven Whitehouse {
51216615be1SSteven Whitehouse 	struct buffer_head *real_bh = bh->b_private;
5135a60c532SSteven Whitehouse 	struct gfs2_bufdata *bd = real_bh->b_private;
5145a60c532SSteven Whitehouse 	struct gfs2_sbd *sdp = bd->bd_gl->gl_sbd;
51516615be1SSteven Whitehouse 
51616615be1SSteven Whitehouse 	end_buffer_write_sync(bh, uptodate);
51716615be1SSteven Whitehouse 	free_buffer_head(bh);
51816615be1SSteven Whitehouse 	unlock_buffer(real_bh);
51916615be1SSteven Whitehouse 	brelse(real_bh);
52016615be1SSteven Whitehouse 	if (atomic_dec_and_test(&sdp->sd_log_in_flight))
52116615be1SSteven Whitehouse 		wake_up(&sdp->sd_log_flush_wait);
52216615be1SSteven Whitehouse }
52316615be1SSteven Whitehouse 
52416615be1SSteven Whitehouse /**
525b3b94faaSDavid Teigland  * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
526b3b94faaSDavid Teigland  * @sdp: the filesystem
527b3b94faaSDavid Teigland  * @data: the data the buffer_head should point to
528b3b94faaSDavid Teigland  *
529b3b94faaSDavid Teigland  * Returns: the log buffer descriptor
530b3b94faaSDavid Teigland  */
531b3b94faaSDavid Teigland 
532b3b94faaSDavid Teigland struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
533b3b94faaSDavid Teigland 				      struct buffer_head *real)
534b3b94faaSDavid Teigland {
535cd915493SSteven Whitehouse 	u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
536b3b94faaSDavid Teigland 	struct buffer_head *bh;
537b3b94faaSDavid Teigland 
53816615be1SSteven Whitehouse 	bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
539b3b94faaSDavid Teigland 	atomic_set(&bh->b_count, 1);
54016615be1SSteven Whitehouse 	bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate) | (1 << BH_Lock);
54118ec7d5cSSteven Whitehouse 	set_bh_page(bh, real->b_page, bh_offset(real));
542b3b94faaSDavid Teigland 	bh->b_blocknr = blkno;
543b3b94faaSDavid Teigland 	bh->b_size = sdp->sd_sb.sb_bsize;
544b3b94faaSDavid Teigland 	bh->b_bdev = sdp->sd_vfs->s_bdev;
54516615be1SSteven Whitehouse 	bh->b_private = real;
54616615be1SSteven Whitehouse 	bh->b_end_io = gfs2_fake_write_endio;
547b3b94faaSDavid Teigland 
54816615be1SSteven Whitehouse 	gfs2_log_incr_head(sdp);
54916615be1SSteven Whitehouse 	atomic_inc(&sdp->sd_log_in_flight);
550b3b94faaSDavid Teigland 
551b3b94faaSDavid Teigland 	return bh;
552b3b94faaSDavid Teigland }
553b3b94faaSDavid Teigland 
5542332c443SRobert Peterson static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail)
555b3b94faaSDavid Teigland {
556b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
557b3b94faaSDavid Teigland 
558b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
559b3b94faaSDavid Teigland 
560b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
561fd041f0bSSteven Whitehouse 	atomic_add(dist, &sdp->sd_log_blks_free);
562fd041f0bSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <= sdp->sd_jdesc->jd_blocks);
563b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
564b3b94faaSDavid Teigland 
565b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
566b3b94faaSDavid Teigland }
567b3b94faaSDavid Teigland 
568b3b94faaSDavid Teigland /**
569b3b94faaSDavid Teigland  * log_write_header - Get and initialize a journal header buffer
570b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
571b3b94faaSDavid Teigland  *
572b3b94faaSDavid Teigland  * Returns: the initialized log buffer descriptor
573b3b94faaSDavid Teigland  */
574b3b94faaSDavid Teigland 
575cd915493SSteven Whitehouse static void log_write_header(struct gfs2_sbd *sdp, u32 flags, int pull)
576b3b94faaSDavid Teigland {
577cd915493SSteven Whitehouse 	u64 blkno = log_bmap(sdp, sdp->sd_log_flush_head);
578b3b94faaSDavid Teigland 	struct buffer_head *bh;
579b3b94faaSDavid Teigland 	struct gfs2_log_header *lh;
580b3b94faaSDavid Teigland 	unsigned int tail;
581cd915493SSteven Whitehouse 	u32 hash;
582b3b94faaSDavid Teigland 
583b3b94faaSDavid Teigland 	bh = sb_getblk(sdp->sd_vfs, blkno);
584b3b94faaSDavid Teigland 	lock_buffer(bh);
585b3b94faaSDavid Teigland 	memset(bh->b_data, 0, bh->b_size);
586b3b94faaSDavid Teigland 	set_buffer_uptodate(bh);
587b3b94faaSDavid Teigland 	clear_buffer_dirty(bh);
588b3b94faaSDavid Teigland 
589b3b94faaSDavid Teigland 	gfs2_ail1_empty(sdp, 0);
590b3b94faaSDavid Teigland 	tail = current_tail(sdp);
591b3b94faaSDavid Teigland 
592b3b94faaSDavid Teigland 	lh = (struct gfs2_log_header *)bh->b_data;
593b3b94faaSDavid Teigland 	memset(lh, 0, sizeof(struct gfs2_log_header));
594b3b94faaSDavid Teigland 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
595e3167dedSSteven Whitehouse 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
596e3167dedSSteven Whitehouse 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
597e0f2bf78SSteven Whitehouse 	lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
598e0f2bf78SSteven Whitehouse 	lh->lh_flags = cpu_to_be32(flags);
599e0f2bf78SSteven Whitehouse 	lh->lh_tail = cpu_to_be32(tail);
600e0f2bf78SSteven Whitehouse 	lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
601b3b94faaSDavid Teigland 	hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
602b3b94faaSDavid Teigland 	lh->lh_hash = cpu_to_be32(hash);
603b3b94faaSDavid Teigland 
604254db57fSSteven Whitehouse 	bh->b_end_io = end_buffer_write_sync;
605254db57fSSteven Whitehouse 	if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
606254db57fSSteven Whitehouse 		goto skip_barrier;
607254db57fSSteven Whitehouse 	get_bh(bh);
608254db57fSSteven Whitehouse 	submit_bh(WRITE_BARRIER | (1 << BIO_RW_META), bh);
609254db57fSSteven Whitehouse 	wait_on_buffer(bh);
610254db57fSSteven Whitehouse 	if (buffer_eopnotsupp(bh)) {
611254db57fSSteven Whitehouse 		clear_buffer_eopnotsupp(bh);
612254db57fSSteven Whitehouse 		set_buffer_uptodate(bh);
613254db57fSSteven Whitehouse 		set_bit(SDF_NOBARRIERS, &sdp->sd_flags);
614254db57fSSteven Whitehouse 		lock_buffer(bh);
615254db57fSSteven Whitehouse skip_barrier:
616254db57fSSteven Whitehouse 		get_bh(bh);
617254db57fSSteven Whitehouse 		submit_bh(WRITE_SYNC | (1 << BIO_RW_META), bh);
618254db57fSSteven Whitehouse 		wait_on_buffer(bh);
619254db57fSSteven Whitehouse 	}
620254db57fSSteven Whitehouse 	if (!buffer_uptodate(bh))
621b3b94faaSDavid Teigland 		gfs2_io_error_bh(sdp, bh);
622b3b94faaSDavid Teigland 	brelse(bh);
623b3b94faaSDavid Teigland 
624b3b94faaSDavid Teigland 	if (sdp->sd_log_tail != tail)
6252332c443SRobert Peterson 		log_pull_tail(sdp, tail);
626b3b94faaSDavid Teigland 	else
627b3b94faaSDavid Teigland 		gfs2_assert_withdraw(sdp, !pull);
628b3b94faaSDavid Teigland 
629b3b94faaSDavid Teigland 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
63016615be1SSteven Whitehouse 	gfs2_log_incr_head(sdp);
631b3b94faaSDavid Teigland }
632b3b94faaSDavid Teigland 
633b3b94faaSDavid Teigland static void log_flush_commit(struct gfs2_sbd *sdp)
634b3b94faaSDavid Teigland {
63516615be1SSteven Whitehouse 	DEFINE_WAIT(wait);
636b3b94faaSDavid Teigland 
63716615be1SSteven Whitehouse 	if (atomic_read(&sdp->sd_log_in_flight)) {
63816615be1SSteven Whitehouse 		do {
63916615be1SSteven Whitehouse 			prepare_to_wait(&sdp->sd_log_flush_wait, &wait,
64016615be1SSteven Whitehouse 					TASK_UNINTERRUPTIBLE);
64116615be1SSteven Whitehouse 			if (atomic_read(&sdp->sd_log_in_flight))
64216615be1SSteven Whitehouse 				io_schedule();
64316615be1SSteven Whitehouse 		} while(atomic_read(&sdp->sd_log_in_flight));
64416615be1SSteven Whitehouse 		finish_wait(&sdp->sd_log_flush_wait, &wait);
645b3b94faaSDavid Teigland 	}
646b3b94faaSDavid Teigland 
647b3b94faaSDavid Teigland 	log_write_header(sdp, 0, 0);
648b3b94faaSDavid Teigland }
649b3b94faaSDavid Teigland 
650d7b616e2SSteven Whitehouse static void gfs2_ordered_write(struct gfs2_sbd *sdp)
651d7b616e2SSteven Whitehouse {
652d7b616e2SSteven Whitehouse 	struct gfs2_bufdata *bd;
653d7b616e2SSteven Whitehouse 	struct buffer_head *bh;
654d7b616e2SSteven Whitehouse 	LIST_HEAD(written);
655d7b616e2SSteven Whitehouse 
656d7b616e2SSteven Whitehouse 	gfs2_log_lock(sdp);
657d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
658d7b616e2SSteven Whitehouse 		bd = list_entry(sdp->sd_log_le_ordered.next, struct gfs2_bufdata, bd_le.le_list);
659d7b616e2SSteven Whitehouse 		list_move(&bd->bd_le.le_list, &written);
660d7b616e2SSteven Whitehouse 		bh = bd->bd_bh;
661d7b616e2SSteven Whitehouse 		if (!buffer_dirty(bh))
662d7b616e2SSteven Whitehouse 			continue;
663d7b616e2SSteven Whitehouse 		get_bh(bh);
664d7b616e2SSteven Whitehouse 		gfs2_log_unlock(sdp);
665d7b616e2SSteven Whitehouse 		lock_buffer(bh);
666b8e7cbb6SSteven Whitehouse 		if (buffer_mapped(bh) && test_clear_buffer_dirty(bh)) {
667d7b616e2SSteven Whitehouse 			bh->b_end_io = end_buffer_write_sync;
668d7b616e2SSteven Whitehouse 			submit_bh(WRITE, bh);
669d7b616e2SSteven Whitehouse 		} else {
670d7b616e2SSteven Whitehouse 			unlock_buffer(bh);
671d7b616e2SSteven Whitehouse 			brelse(bh);
672d7b616e2SSteven Whitehouse 		}
673d7b616e2SSteven Whitehouse 		gfs2_log_lock(sdp);
674d7b616e2SSteven Whitehouse 	}
675d7b616e2SSteven Whitehouse 	list_splice(&written, &sdp->sd_log_le_ordered);
676d7b616e2SSteven Whitehouse 	gfs2_log_unlock(sdp);
677d7b616e2SSteven Whitehouse }
678d7b616e2SSteven Whitehouse 
679d7b616e2SSteven Whitehouse static void gfs2_ordered_wait(struct gfs2_sbd *sdp)
680d7b616e2SSteven Whitehouse {
681d7b616e2SSteven Whitehouse 	struct gfs2_bufdata *bd;
682d7b616e2SSteven Whitehouse 	struct buffer_head *bh;
683d7b616e2SSteven Whitehouse 
684d7b616e2SSteven Whitehouse 	gfs2_log_lock(sdp);
685d7b616e2SSteven Whitehouse 	while (!list_empty(&sdp->sd_log_le_ordered)) {
686d7b616e2SSteven Whitehouse 		bd = list_entry(sdp->sd_log_le_ordered.prev, struct gfs2_bufdata, bd_le.le_list);
687d7b616e2SSteven Whitehouse 		bh = bd->bd_bh;
688d7b616e2SSteven Whitehouse 		if (buffer_locked(bh)) {
689d7b616e2SSteven Whitehouse 			get_bh(bh);
690d7b616e2SSteven Whitehouse 			gfs2_log_unlock(sdp);
691d7b616e2SSteven Whitehouse 			wait_on_buffer(bh);
692d7b616e2SSteven Whitehouse 			brelse(bh);
693d7b616e2SSteven Whitehouse 			gfs2_log_lock(sdp);
694d7b616e2SSteven Whitehouse 			continue;
695d7b616e2SSteven Whitehouse 		}
696d7b616e2SSteven Whitehouse 		list_del_init(&bd->bd_le.le_list);
697d7b616e2SSteven Whitehouse 	}
698d7b616e2SSteven Whitehouse 	gfs2_log_unlock(sdp);
699d7b616e2SSteven Whitehouse }
700d7b616e2SSteven Whitehouse 
701b3b94faaSDavid Teigland /**
702b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
703b3b94faaSDavid Teigland  * @sdp: the filesystem
704b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
705b3b94faaSDavid Teigland  *
706b3b94faaSDavid Teigland  */
707b3b94faaSDavid Teigland 
7082bcd610dSSteven Whitehouse void __gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
709b3b94faaSDavid Teigland {
710b3b94faaSDavid Teigland 	struct gfs2_ail *ai;
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 	}
719f55ab26aSSteven Whitehouse 
720b09e593dSSteven Whitehouse 	ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
721b09e593dSSteven Whitehouse 	INIT_LIST_HEAD(&ai->ai_ail1_list);
722b09e593dSSteven Whitehouse 	INIT_LIST_HEAD(&ai->ai_ail2_list);
723b3b94faaSDavid Teigland 
72416615be1SSteven Whitehouse 	if (sdp->sd_log_num_buf != sdp->sd_log_commited_buf) {
72516615be1SSteven Whitehouse 		printk(KERN_INFO "GFS2: log buf %u %u\n", sdp->sd_log_num_buf,
72616615be1SSteven Whitehouse 		       sdp->sd_log_commited_buf);
72716615be1SSteven Whitehouse 		gfs2_assert_withdraw(sdp, 0);
72816615be1SSteven Whitehouse 	}
72916615be1SSteven Whitehouse 	if (sdp->sd_log_num_databuf != sdp->sd_log_commited_databuf) {
73016615be1SSteven Whitehouse 		printk(KERN_INFO "GFS2: log databuf %u %u\n",
73116615be1SSteven Whitehouse 		       sdp->sd_log_num_databuf, sdp->sd_log_commited_databuf);
73216615be1SSteven Whitehouse 		gfs2_assert_withdraw(sdp, 0);
73316615be1SSteven Whitehouse 	}
734b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
735b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
736b3b94faaSDavid Teigland 
737b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
738b3b94faaSDavid Teigland 	sdp->sd_log_flush_wrapped = 0;
739b3b94faaSDavid Teigland 	ai->ai_first = sdp->sd_log_flush_head;
740b3b94faaSDavid Teigland 
741d7b616e2SSteven Whitehouse 	gfs2_ordered_write(sdp);
742b3b94faaSDavid Teigland 	lops_before_commit(sdp);
743d7b616e2SSteven Whitehouse 	gfs2_ordered_wait(sdp);
744d7b616e2SSteven Whitehouse 
74516615be1SSteven Whitehouse 	if (sdp->sd_log_head != sdp->sd_log_flush_head)
746b3b94faaSDavid Teigland 		log_flush_commit(sdp);
7472332c443SRobert Peterson 	else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle){
7482332c443SRobert Peterson 		gfs2_log_lock(sdp);
749fd041f0bSSteven Whitehouse 		atomic_dec(&sdp->sd_log_blks_free); /* Adjust for unreserved buffer */
7502332c443SRobert Peterson 		gfs2_log_unlock(sdp);
751b3b94faaSDavid Teigland 		log_write_header(sdp, 0, PULL);
7522332c443SRobert Peterson 	}
753b3b94faaSDavid Teigland 	lops_after_commit(sdp, ai);
754fe1a698fSSteven Whitehouse 
755fe1a698fSSteven Whitehouse 	gfs2_log_lock(sdp);
756b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
757faa31ce8SSteven Whitehouse 	sdp->sd_log_blks_reserved = 0;
758faa31ce8SSteven Whitehouse 	sdp->sd_log_commited_buf = 0;
7592332c443SRobert Peterson 	sdp->sd_log_commited_databuf = 0;
760b3b94faaSDavid Teigland 	sdp->sd_log_commited_revoke = 0;
761b3b94faaSDavid Teigland 
762b3b94faaSDavid Teigland 	if (!list_empty(&ai->ai_ail1_list)) {
763b3b94faaSDavid Teigland 		list_add(&ai->ai_list, &sdp->sd_ail1_list);
764b3b94faaSDavid Teigland 		ai = NULL;
765b3b94faaSDavid Teigland 	}
766b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
767b3b94faaSDavid Teigland 
768b3b94faaSDavid Teigland 	sdp->sd_vfs->s_dirt = 0;
769484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
770b3b94faaSDavid Teigland 
771b3b94faaSDavid Teigland 	kfree(ai);
772b3b94faaSDavid Teigland }
773b3b94faaSDavid Teigland 
774b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
775b3b94faaSDavid Teigland {
7762332c443SRobert Peterson 	unsigned int reserved;
777ac39aaddSSteven Whitehouse 	unsigned int unused;
778b3b94faaSDavid Teigland 
779b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
780b3b94faaSDavid Teigland 
781b3b94faaSDavid Teigland 	sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
7822332c443SRobert Peterson 	sdp->sd_log_commited_databuf += tr->tr_num_databuf_new -
7832332c443SRobert Peterson 		tr->tr_num_databuf_rm;
7842332c443SRobert Peterson 	gfs2_assert_withdraw(sdp, (((int)sdp->sd_log_commited_buf) >= 0) ||
7852332c443SRobert Peterson 			     (((int)sdp->sd_log_commited_databuf) >= 0));
786b3b94faaSDavid Teigland 	sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
787b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
7882332c443SRobert Peterson 	reserved = calc_reserved(sdp);
78962be1f71SRoel Kluin 	gfs2_assert_withdraw(sdp, sdp->sd_log_blks_reserved + tr->tr_reserved >= reserved);
790ac39aaddSSteven Whitehouse 	unused = sdp->sd_log_blks_reserved - reserved + tr->tr_reserved;
791ac39aaddSSteven Whitehouse 	atomic_add(unused, &sdp->sd_log_blks_free);
792fd041f0bSSteven Whitehouse 	gfs2_assert_withdraw(sdp, atomic_read(&sdp->sd_log_blks_free) <=
7932332c443SRobert Peterson 			     sdp->sd_jdesc->jd_blocks);
794b3b94faaSDavid Teigland 	sdp->sd_log_blks_reserved = reserved;
795b3b94faaSDavid Teigland 
796b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
797b3b94faaSDavid Teigland }
798b3b94faaSDavid Teigland 
799d0109bfaSBob Peterson static void buf_lo_incore_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
800d0109bfaSBob Peterson {
801d0109bfaSBob Peterson 	struct list_head *head = &tr->tr_list_buf;
802d0109bfaSBob Peterson 	struct gfs2_bufdata *bd;
803d0109bfaSBob Peterson 
804d0109bfaSBob Peterson 	gfs2_log_lock(sdp);
805d0109bfaSBob Peterson 	while (!list_empty(head)) {
806d0109bfaSBob Peterson 		bd = list_entry(head->next, struct gfs2_bufdata, bd_list_tr);
807d0109bfaSBob Peterson 		list_del_init(&bd->bd_list_tr);
808d0109bfaSBob Peterson 		tr->tr_num_buf--;
809d0109bfaSBob Peterson 	}
810d0109bfaSBob Peterson 	gfs2_log_unlock(sdp);
811d0109bfaSBob Peterson 	gfs2_assert_warn(sdp, !tr->tr_num_buf);
812d0109bfaSBob Peterson }
813d0109bfaSBob Peterson 
814b3b94faaSDavid Teigland /**
815b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
816b3b94faaSDavid Teigland  * @sdp: the filesystem
817b3b94faaSDavid Teigland  * @tr: the transaction
818b3b94faaSDavid Teigland  *
819b3b94faaSDavid Teigland  * Returns: errno
820b3b94faaSDavid Teigland  */
821b3b94faaSDavid Teigland 
822b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
823b3b94faaSDavid Teigland {
824b3b94faaSDavid Teigland 	log_refund(sdp, tr);
825d0109bfaSBob Peterson 	buf_lo_incore_commit(sdp, tr);
826b3b94faaSDavid Teigland 
827b3b94faaSDavid Teigland 	sdp->sd_vfs->s_dirt = 1;
828484adff8SSteven Whitehouse 	up_read(&sdp->sd_log_flush_lock);
829b3b94faaSDavid Teigland 
830b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
831b004157aSSteven Whitehouse 	if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks))
832b004157aSSteven Whitehouse 		wake_up_process(sdp->sd_logd_process);
833b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
834faa31ce8SSteven Whitehouse }
835b3b94faaSDavid Teigland 
836b3b94faaSDavid Teigland /**
837b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
838b3b94faaSDavid Teigland  * @sdp: the filesystem
839b3b94faaSDavid Teigland  *
840b3b94faaSDavid Teigland  */
841b3b94faaSDavid Teigland 
842b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp)
843b3b94faaSDavid Teigland {
844484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
845b3b94faaSDavid Teigland 
846b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
847b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
848b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
849b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
850b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
851b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
852b3b94faaSDavid Teigland 
853b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
854b3b94faaSDavid Teigland 	sdp->sd_log_flush_wrapped = 0;
855b3b94faaSDavid Teigland 
8562332c443SRobert Peterson 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT,
8572332c443SRobert Peterson 			 (sdp->sd_log_tail == current_tail(sdp)) ? 0 : PULL);
858b3b94faaSDavid Teigland 
859fd041f0bSSteven Whitehouse 	gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) == sdp->sd_jdesc->jd_blocks);
860a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
861a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
862b3b94faaSDavid Teigland 
863b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
864b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
865b3b94faaSDavid Teigland 
866484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
867b3b94faaSDavid Teigland }
868b3b94faaSDavid Teigland 
869a25311c8SSteven Whitehouse 
870a25311c8SSteven Whitehouse /**
871a25311c8SSteven Whitehouse  * gfs2_meta_syncfs - sync all the buffers in a filesystem
872a25311c8SSteven Whitehouse  * @sdp: the filesystem
873a25311c8SSteven Whitehouse  *
874a25311c8SSteven Whitehouse  */
875a25311c8SSteven Whitehouse 
876a25311c8SSteven Whitehouse void gfs2_meta_syncfs(struct gfs2_sbd *sdp)
877a25311c8SSteven Whitehouse {
878a25311c8SSteven Whitehouse 	gfs2_log_flush(sdp, NULL);
879a25311c8SSteven Whitehouse 	for (;;) {
880a25311c8SSteven Whitehouse 		gfs2_ail1_start(sdp, DIO_ALL);
881a25311c8SSteven Whitehouse 		if (gfs2_ail1_empty(sdp, DIO_ALL))
882a25311c8SSteven Whitehouse 			break;
883a25311c8SSteven Whitehouse 		msleep(10);
884a25311c8SSteven Whitehouse 	}
885a25311c8SSteven Whitehouse }
886a25311c8SSteven Whitehouse 
887ec69b188SSteven Whitehouse 
888ec69b188SSteven Whitehouse /**
889ec69b188SSteven Whitehouse  * gfs2_logd - Update log tail as Active Items get flushed to in-place blocks
890ec69b188SSteven Whitehouse  * @sdp: Pointer to GFS2 superblock
891ec69b188SSteven Whitehouse  *
892ec69b188SSteven Whitehouse  * Also, periodically check to make sure that we're using the most recent
893ec69b188SSteven Whitehouse  * journal index.
894ec69b188SSteven Whitehouse  */
895ec69b188SSteven Whitehouse 
896ec69b188SSteven Whitehouse int gfs2_logd(void *data)
897ec69b188SSteven Whitehouse {
898ec69b188SSteven Whitehouse 	struct gfs2_sbd *sdp = data;
899ec69b188SSteven Whitehouse 	unsigned long t;
900ec69b188SSteven Whitehouse 	int need_flush;
901ec69b188SSteven Whitehouse 
902ec69b188SSteven Whitehouse 	while (!kthread_should_stop()) {
903ec69b188SSteven Whitehouse 		/* Advance the log tail */
904ec69b188SSteven Whitehouse 
905ec69b188SSteven Whitehouse 		t = sdp->sd_log_flush_time +
906ec69b188SSteven Whitehouse 		    gfs2_tune_get(sdp, gt_log_flush_secs) * HZ;
907ec69b188SSteven Whitehouse 
908ec69b188SSteven Whitehouse 		gfs2_ail1_empty(sdp, DIO_ALL);
909ec69b188SSteven Whitehouse 		gfs2_log_lock(sdp);
910ec69b188SSteven Whitehouse 		need_flush = sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks);
911ec69b188SSteven Whitehouse 		gfs2_log_unlock(sdp);
912ec69b188SSteven Whitehouse 		if (need_flush || time_after_eq(jiffies, t)) {
913ec69b188SSteven Whitehouse 			gfs2_log_flush(sdp, NULL);
914ec69b188SSteven Whitehouse 			sdp->sd_log_flush_time = jiffies;
915ec69b188SSteven Whitehouse 		}
916ec69b188SSteven Whitehouse 
917ec69b188SSteven Whitehouse 		t = gfs2_tune_get(sdp, gt_logd_secs) * HZ;
918ec69b188SSteven Whitehouse 		if (freezing(current))
919ec69b188SSteven Whitehouse 			refrigerator();
920ec69b188SSteven Whitehouse 		schedule_timeout_interruptible(t);
921ec69b188SSteven Whitehouse 	}
922ec69b188SSteven Whitehouse 
923ec69b188SSteven Whitehouse 	return 0;
924ec69b188SSteven Whitehouse }
925ec69b188SSteven Whitehouse 
926