xref: /openbmc/linux/fs/gfs2/log.c (revision e0f2bf780a970f7aae9263db2e14149132671cf2)
1b3b94faaSDavid Teigland /*
2b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
33a8a9a10SSteven Whitehouse  * Copyright (C) 2004-2006 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
7b3b94faaSDavid Teigland  * of the GNU General Public License v.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>
17b3b94faaSDavid Teigland 
18b3b94faaSDavid Teigland #include "gfs2.h"
195c676f6dSSteven Whitehouse #include "lm_interface.h"
205c676f6dSSteven Whitehouse #include "incore.h"
21b3b94faaSDavid Teigland #include "bmap.h"
22b3b94faaSDavid Teigland #include "glock.h"
23b3b94faaSDavid Teigland #include "log.h"
24b3b94faaSDavid Teigland #include "lops.h"
25b3b94faaSDavid Teigland #include "meta_io.h"
265c676f6dSSteven Whitehouse #include "util.h"
2771b86f56SSteven Whitehouse #include "dir.h"
28b3b94faaSDavid Teigland 
29b3b94faaSDavid Teigland #define PULL 1
30b3b94faaSDavid Teigland 
31b3b94faaSDavid Teigland /**
32b3b94faaSDavid Teigland  * gfs2_struct2blk - compute stuff
33b3b94faaSDavid Teigland  * @sdp: the filesystem
34b3b94faaSDavid Teigland  * @nstruct: the number of structures
35b3b94faaSDavid Teigland  * @ssize: the size of the structures
36b3b94faaSDavid Teigland  *
37b3b94faaSDavid Teigland  * Compute the number of log descriptor blocks needed to hold a certain number
38b3b94faaSDavid Teigland  * of structures of a certain size.
39b3b94faaSDavid Teigland  *
40b3b94faaSDavid Teigland  * Returns: the number of blocks needed (minimum is always 1)
41b3b94faaSDavid Teigland  */
42b3b94faaSDavid Teigland 
43b3b94faaSDavid Teigland unsigned int gfs2_struct2blk(struct gfs2_sbd *sdp, unsigned int nstruct,
44b3b94faaSDavid Teigland 			     unsigned int ssize)
45b3b94faaSDavid Teigland {
46b3b94faaSDavid Teigland 	unsigned int blks;
47b3b94faaSDavid Teigland 	unsigned int first, second;
48b3b94faaSDavid Teigland 
49b3b94faaSDavid Teigland 	blks = 1;
50568f4c96SSteven Whitehouse 	first = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) /
51568f4c96SSteven Whitehouse 		ssize;
52b3b94faaSDavid Teigland 
53b3b94faaSDavid Teigland 	if (nstruct > first) {
54568f4c96SSteven Whitehouse 		second = (sdp->sd_sb.sb_bsize -
55568f4c96SSteven Whitehouse 			  sizeof(struct gfs2_meta_header)) / ssize;
565c676f6dSSteven Whitehouse 		blks += DIV_ROUND_UP(nstruct - first, second);
57b3b94faaSDavid Teigland 	}
58b3b94faaSDavid Teigland 
59b3b94faaSDavid Teigland 	return blks;
60b3b94faaSDavid Teigland }
61b3b94faaSDavid Teigland 
62b3b94faaSDavid Teigland void gfs2_ail1_start(struct gfs2_sbd *sdp, int flags)
63b3b94faaSDavid Teigland {
64b3b94faaSDavid Teigland 	struct list_head *head = &sdp->sd_ail1_list;
65b3b94faaSDavid Teigland 	uint64_t sync_gen;
66b3b94faaSDavid Teigland 	struct list_head *first, *tmp;
67b3b94faaSDavid Teigland 	struct gfs2_ail *first_ai, *ai;
68b3b94faaSDavid Teigland 
69b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
70b3b94faaSDavid Teigland 	if (list_empty(head)) {
71b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
72b3b94faaSDavid Teigland 		return;
73b3b94faaSDavid Teigland 	}
74b3b94faaSDavid Teigland 	sync_gen = sdp->sd_ail_sync_gen++;
75b3b94faaSDavid Teigland 
76b3b94faaSDavid Teigland 	first = head->prev;
77b3b94faaSDavid Teigland 	first_ai = list_entry(first, struct gfs2_ail, ai_list);
78b3b94faaSDavid Teigland 	first_ai->ai_sync_gen = sync_gen;
79b3b94faaSDavid Teigland 	gfs2_ail1_start_one(sdp, first_ai);
80b3b94faaSDavid Teigland 
81b3b94faaSDavid Teigland 	if (flags & DIO_ALL)
82b3b94faaSDavid Teigland 		first = NULL;
83b3b94faaSDavid Teigland 
84b3b94faaSDavid Teigland 	for (;;) {
85484adff8SSteven Whitehouse 		if (first && (head->prev != first ||
86b3b94faaSDavid Teigland 			      gfs2_ail1_empty_one(sdp, first_ai, 0)))
87b3b94faaSDavid Teigland 			break;
88b3b94faaSDavid Teigland 
89b3b94faaSDavid Teigland 		for (tmp = head->prev; tmp != head; tmp = tmp->prev) {
90b3b94faaSDavid Teigland 			ai = list_entry(tmp, struct gfs2_ail, ai_list);
91b3b94faaSDavid Teigland 			if (ai->ai_sync_gen >= sync_gen)
92b3b94faaSDavid Teigland 				continue;
93b3b94faaSDavid Teigland 			ai->ai_sync_gen = sync_gen;
94b3b94faaSDavid Teigland 			gfs2_ail1_start_one(sdp, ai);
95b3b94faaSDavid Teigland 			break;
96b3b94faaSDavid Teigland 		}
97b3b94faaSDavid Teigland 
98b3b94faaSDavid Teigland 		if (tmp == head)
99b3b94faaSDavid Teigland 			break;
100b3b94faaSDavid Teigland 	}
101b3b94faaSDavid Teigland 
102b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
103b3b94faaSDavid Teigland }
104b3b94faaSDavid Teigland 
105b3b94faaSDavid Teigland int gfs2_ail1_empty(struct gfs2_sbd *sdp, int flags)
106b3b94faaSDavid Teigland {
107b3b94faaSDavid Teigland 	struct gfs2_ail *ai, *s;
108b3b94faaSDavid Teigland 	int ret;
109b3b94faaSDavid Teigland 
110b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
111b3b94faaSDavid Teigland 
112b3b94faaSDavid Teigland 	list_for_each_entry_safe_reverse(ai, s, &sdp->sd_ail1_list, ai_list) {
113b3b94faaSDavid Teigland 		if (gfs2_ail1_empty_one(sdp, ai, flags))
114b3b94faaSDavid Teigland 			list_move(&ai->ai_list, &sdp->sd_ail2_list);
115b3b94faaSDavid Teigland 		else if (!(flags & DIO_ALL))
116b3b94faaSDavid Teigland 			break;
117b3b94faaSDavid Teigland 	}
118b3b94faaSDavid Teigland 
119b3b94faaSDavid Teigland 	ret = list_empty(&sdp->sd_ail1_list);
120b3b94faaSDavid Teigland 
121b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
122b3b94faaSDavid Teigland 
123b3b94faaSDavid Teigland 	return ret;
124b3b94faaSDavid Teigland }
125b3b94faaSDavid Teigland 
126b3b94faaSDavid Teigland static void ail2_empty(struct gfs2_sbd *sdp, unsigned int new_tail)
127b3b94faaSDavid Teigland {
128b3b94faaSDavid Teigland 	struct gfs2_ail *ai, *safe;
129b3b94faaSDavid Teigland 	unsigned int old_tail = sdp->sd_log_tail;
130b3b94faaSDavid Teigland 	int wrap = (new_tail < old_tail);
131b3b94faaSDavid Teigland 	int a, b, rm;
132b3b94faaSDavid Teigland 
133b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
134b3b94faaSDavid Teigland 
135b3b94faaSDavid Teigland 	list_for_each_entry_safe(ai, safe, &sdp->sd_ail2_list, ai_list) {
136b3b94faaSDavid Teigland 		a = (old_tail <= ai->ai_first);
137b3b94faaSDavid Teigland 		b = (ai->ai_first < new_tail);
138b3b94faaSDavid Teigland 		rm = (wrap) ? (a || b) : (a && b);
139b3b94faaSDavid Teigland 		if (!rm)
140b3b94faaSDavid Teigland 			continue;
141b3b94faaSDavid Teigland 
142b3b94faaSDavid Teigland 		gfs2_ail2_empty_one(sdp, ai);
143b3b94faaSDavid Teigland 		list_del(&ai->ai_list);
144b3b94faaSDavid Teigland 		gfs2_assert_warn(sdp, list_empty(&ai->ai_ail1_list));
145b3b94faaSDavid Teigland 		gfs2_assert_warn(sdp, list_empty(&ai->ai_ail2_list));
146b3b94faaSDavid Teigland 		kfree(ai);
147b3b94faaSDavid Teigland 	}
148b3b94faaSDavid Teigland 
149b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
150b3b94faaSDavid Teigland }
151b3b94faaSDavid Teigland 
152b3b94faaSDavid Teigland /**
153b3b94faaSDavid Teigland  * gfs2_log_reserve - Make a log reservation
154b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
155b3b94faaSDavid Teigland  * @blks: The number of blocks to reserve
156b3b94faaSDavid Teigland  *
157b3b94faaSDavid Teigland  * Returns: errno
158b3b94faaSDavid Teigland  */
159b3b94faaSDavid Teigland 
160b3b94faaSDavid Teigland int gfs2_log_reserve(struct gfs2_sbd *sdp, unsigned int blks)
161b3b94faaSDavid Teigland {
162b3b94faaSDavid Teigland 	unsigned int try = 0;
163b3b94faaSDavid Teigland 
164b3b94faaSDavid Teigland 	if (gfs2_assert_warn(sdp, blks) ||
165b3b94faaSDavid Teigland 	    gfs2_assert_warn(sdp, blks <= sdp->sd_jdesc->jd_blocks))
166b3b94faaSDavid Teigland 		return -EINVAL;
167b3b94faaSDavid Teigland 
16871b86f56SSteven Whitehouse 	mutex_lock(&sdp->sd_log_reserve_mutex);
169b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
170484adff8SSteven Whitehouse 	while(sdp->sd_log_blks_free <= blks) {
171b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
172b3b94faaSDavid Teigland 		gfs2_ail1_empty(sdp, 0);
173b09e593dSSteven Whitehouse 		gfs2_log_flush(sdp, NULL);
174b3b94faaSDavid Teigland 
175b3b94faaSDavid Teigland 		if (try++)
176b3b94faaSDavid Teigland 			gfs2_ail1_start(sdp, 0);
177484adff8SSteven Whitehouse 		gfs2_log_lock(sdp);
178b3b94faaSDavid Teigland 	}
179484adff8SSteven Whitehouse 	sdp->sd_log_blks_free -= blks;
180b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "reserved %u blocks (%u left)\n", blks, sdp->sd_log_blks_free); */
181484adff8SSteven Whitehouse 	gfs2_log_unlock(sdp);
182484adff8SSteven Whitehouse 	mutex_unlock(&sdp->sd_log_reserve_mutex);
183484adff8SSteven Whitehouse 
184484adff8SSteven Whitehouse 	down_read(&sdp->sd_log_flush_lock);
185b3b94faaSDavid Teigland 
186b3b94faaSDavid Teigland 	return 0;
187b3b94faaSDavid Teigland }
188b3b94faaSDavid Teigland 
189b3b94faaSDavid Teigland /**
190b3b94faaSDavid Teigland  * gfs2_log_release - Release a given number of log blocks
191b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
192b3b94faaSDavid Teigland  * @blks: The number of blocks
193b3b94faaSDavid Teigland  *
194b3b94faaSDavid Teigland  */
195b3b94faaSDavid Teigland 
196b3b94faaSDavid Teigland void gfs2_log_release(struct gfs2_sbd *sdp, unsigned int blks)
197b3b94faaSDavid Teigland {
198b3b94faaSDavid Teigland 
199b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
200b3b94faaSDavid Teigland 	sdp->sd_log_blks_free += blks;
201b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "released %u blocks (%u left)\n", blks, sdp->sd_log_blks_free); */
202b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
203b3b94faaSDavid Teigland 			     sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
204b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
205ed386507SSteven Whitehouse 	up_read(&sdp->sd_log_flush_lock);
206b3b94faaSDavid Teigland }
207b3b94faaSDavid Teigland 
208b3b94faaSDavid Teigland static uint64_t log_bmap(struct gfs2_sbd *sdp, unsigned int lbn)
209b3b94faaSDavid Teigland {
210b3b94faaSDavid Teigland 	int new = 0;
211b3b94faaSDavid Teigland 	uint64_t dbn;
212b3b94faaSDavid Teigland 	int error;
213fd88de56SSteven Whitehouse 	int bdy;
214b3b94faaSDavid Teigland 
215fd88de56SSteven Whitehouse 	error = gfs2_block_map(sdp->sd_jdesc->jd_inode, lbn, &new, &dbn, &bdy);
216feaa7bbaSSteven Whitehouse 	if (!(!error && dbn)) {
217fd4de2d4SSteven Whitehouse 		printk(KERN_INFO "error=%d, dbn=%llu lbn=%u", error, (unsigned long long)dbn, lbn);
218feaa7bbaSSteven Whitehouse 	}
219b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !error && dbn);
220b3b94faaSDavid Teigland 
221b3b94faaSDavid Teigland 	return dbn;
222b3b94faaSDavid Teigland }
223b3b94faaSDavid Teigland 
224b3b94faaSDavid Teigland /**
225b3b94faaSDavid Teigland  * log_distance - Compute distance between two journal blocks
226b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
227b3b94faaSDavid Teigland  * @newer: The most recent journal block of the pair
228b3b94faaSDavid Teigland  * @older: The older journal block of the pair
229b3b94faaSDavid Teigland  *
230b3b94faaSDavid Teigland  *   Compute the distance (in the journal direction) between two
231b3b94faaSDavid Teigland  *   blocks in the journal
232b3b94faaSDavid Teigland  *
233b3b94faaSDavid Teigland  * Returns: the distance in blocks
234b3b94faaSDavid Teigland  */
235b3b94faaSDavid Teigland 
236b3b94faaSDavid Teigland static inline unsigned int log_distance(struct gfs2_sbd *sdp,
237b3b94faaSDavid Teigland 					unsigned int newer,
238b3b94faaSDavid Teigland 					unsigned int older)
239b3b94faaSDavid Teigland {
240b3b94faaSDavid Teigland 	int dist;
241b3b94faaSDavid Teigland 
242b3b94faaSDavid Teigland 	dist = newer - older;
243b3b94faaSDavid Teigland 	if (dist < 0)
244b3b94faaSDavid Teigland 		dist += sdp->sd_jdesc->jd_blocks;
245b3b94faaSDavid Teigland 
246b3b94faaSDavid Teigland 	return dist;
247b3b94faaSDavid Teigland }
248b3b94faaSDavid Teigland 
249b3b94faaSDavid Teigland static unsigned int current_tail(struct gfs2_sbd *sdp)
250b3b94faaSDavid Teigland {
251b3b94faaSDavid Teigland 	struct gfs2_ail *ai;
252b3b94faaSDavid Teigland 	unsigned int tail;
253b3b94faaSDavid Teigland 
254b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
255b3b94faaSDavid Teigland 
256b3b94faaSDavid Teigland 	if (list_empty(&sdp->sd_ail1_list))
257b3b94faaSDavid Teigland 		tail = sdp->sd_log_head;
258b3b94faaSDavid Teigland 	else {
259b3b94faaSDavid Teigland 		ai = list_entry(sdp->sd_ail1_list.prev,
260b3b94faaSDavid Teigland 				struct gfs2_ail, ai_list);
261b3b94faaSDavid Teigland 		tail = ai->ai_first;
262b3b94faaSDavid Teigland 	}
263b3b94faaSDavid Teigland 
264b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
265b3b94faaSDavid Teigland 
266b3b94faaSDavid Teigland 	return tail;
267b3b94faaSDavid Teigland }
268b3b94faaSDavid Teigland 
269b3b94faaSDavid Teigland static inline void log_incr_head(struct gfs2_sbd *sdp)
270b3b94faaSDavid Teigland {
271b3b94faaSDavid Teigland 	if (sdp->sd_log_flush_head == sdp->sd_log_tail)
272b3b94faaSDavid Teigland 		gfs2_assert_withdraw(sdp,
273b3b94faaSDavid Teigland 				sdp->sd_log_flush_head == sdp->sd_log_head);
274b3b94faaSDavid Teigland 
275b3b94faaSDavid Teigland 	if (++sdp->sd_log_flush_head == sdp->sd_jdesc->jd_blocks) {
276b3b94faaSDavid Teigland 		sdp->sd_log_flush_head = 0;
277b3b94faaSDavid Teigland 		sdp->sd_log_flush_wrapped = 1;
278b3b94faaSDavid Teigland 	}
279b3b94faaSDavid Teigland }
280b3b94faaSDavid Teigland 
281b3b94faaSDavid Teigland /**
282b3b94faaSDavid Teigland  * gfs2_log_get_buf - Get and initialize a buffer to use for log control data
283b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
284b3b94faaSDavid Teigland  *
285b3b94faaSDavid Teigland  * Returns: the buffer_head
286b3b94faaSDavid Teigland  */
287b3b94faaSDavid Teigland 
288b3b94faaSDavid Teigland struct buffer_head *gfs2_log_get_buf(struct gfs2_sbd *sdp)
289b3b94faaSDavid Teigland {
290b3b94faaSDavid Teigland 	uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
291b3b94faaSDavid Teigland 	struct gfs2_log_buf *lb;
292b3b94faaSDavid Teigland 	struct buffer_head *bh;
293b3b94faaSDavid Teigland 
2944f3df041SSteven Whitehouse 	lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
295b3b94faaSDavid Teigland 	list_add(&lb->lb_list, &sdp->sd_log_flush_list);
296b3b94faaSDavid Teigland 
297b3b94faaSDavid Teigland 	bh = lb->lb_bh = sb_getblk(sdp->sd_vfs, blkno);
298b3b94faaSDavid Teigland 	lock_buffer(bh);
299b3b94faaSDavid Teigland 	memset(bh->b_data, 0, bh->b_size);
300b3b94faaSDavid Teigland 	set_buffer_uptodate(bh);
301b3b94faaSDavid Teigland 	clear_buffer_dirty(bh);
302b3b94faaSDavid Teigland 	unlock_buffer(bh);
303b3b94faaSDavid Teigland 
304b3b94faaSDavid Teigland 	log_incr_head(sdp);
305b3b94faaSDavid Teigland 
306b3b94faaSDavid Teigland 	return bh;
307b3b94faaSDavid Teigland }
308b3b94faaSDavid Teigland 
309b3b94faaSDavid Teigland /**
310b3b94faaSDavid Teigland  * gfs2_log_fake_buf - Build a fake buffer head to write metadata buffer to log
311b3b94faaSDavid Teigland  * @sdp: the filesystem
312b3b94faaSDavid Teigland  * @data: the data the buffer_head should point to
313b3b94faaSDavid Teigland  *
314b3b94faaSDavid Teigland  * Returns: the log buffer descriptor
315b3b94faaSDavid Teigland  */
316b3b94faaSDavid Teigland 
317b3b94faaSDavid Teigland struct buffer_head *gfs2_log_fake_buf(struct gfs2_sbd *sdp,
318b3b94faaSDavid Teigland 				      struct buffer_head *real)
319b3b94faaSDavid Teigland {
320b3b94faaSDavid Teigland 	uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
321b3b94faaSDavid Teigland 	struct gfs2_log_buf *lb;
322b3b94faaSDavid Teigland 	struct buffer_head *bh;
323b3b94faaSDavid Teigland 
3244f3df041SSteven Whitehouse 	lb = kzalloc(sizeof(struct gfs2_log_buf), GFP_NOFS | __GFP_NOFAIL);
325b3b94faaSDavid Teigland 	list_add(&lb->lb_list, &sdp->sd_log_flush_list);
326b3b94faaSDavid Teigland 	lb->lb_real = real;
327b3b94faaSDavid Teigland 
328b3b94faaSDavid Teigland 	bh = lb->lb_bh = alloc_buffer_head(GFP_NOFS | __GFP_NOFAIL);
329b3b94faaSDavid Teigland 	atomic_set(&bh->b_count, 1);
330b3b94faaSDavid Teigland 	bh->b_state = (1 << BH_Mapped) | (1 << BH_Uptodate);
33118ec7d5cSSteven Whitehouse 	set_bh_page(bh, real->b_page, bh_offset(real));
332b3b94faaSDavid Teigland 	bh->b_blocknr = blkno;
333b3b94faaSDavid Teigland 	bh->b_size = sdp->sd_sb.sb_bsize;
334b3b94faaSDavid Teigland 	bh->b_bdev = sdp->sd_vfs->s_bdev;
335b3b94faaSDavid Teigland 
336b3b94faaSDavid Teigland 	log_incr_head(sdp);
337b3b94faaSDavid Teigland 
338b3b94faaSDavid Teigland 	return bh;
339b3b94faaSDavid Teigland }
340b3b94faaSDavid Teigland 
341b3b94faaSDavid Teigland static void log_pull_tail(struct gfs2_sbd *sdp, unsigned int new_tail, int pull)
342b3b94faaSDavid Teigland {
343b3b94faaSDavid Teigland 	unsigned int dist = log_distance(sdp, new_tail, sdp->sd_log_tail);
344b3b94faaSDavid Teigland 
345b3b94faaSDavid Teigland 	ail2_empty(sdp, new_tail);
346b3b94faaSDavid Teigland 
347b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
348b3b94faaSDavid Teigland 	sdp->sd_log_blks_free += dist - ((pull) ? 1 : 0);
349b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "pull tail refunding %u blocks (%u left) pull=%d\n", dist - ((pull) ? 1 : 0), sdp->sd_log_blks_free, pull); */
350b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
351b3b94faaSDavid Teigland 			     sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks);
352b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
353b3b94faaSDavid Teigland 
354b3b94faaSDavid Teigland 	sdp->sd_log_tail = new_tail;
355b3b94faaSDavid Teigland }
356b3b94faaSDavid Teigland 
357b3b94faaSDavid Teigland /**
358b3b94faaSDavid Teigland  * log_write_header - Get and initialize a journal header buffer
359b3b94faaSDavid Teigland  * @sdp: The GFS2 superblock
360b3b94faaSDavid Teigland  *
361b3b94faaSDavid Teigland  * Returns: the initialized log buffer descriptor
362b3b94faaSDavid Teigland  */
363b3b94faaSDavid Teigland 
364b3b94faaSDavid Teigland static void log_write_header(struct gfs2_sbd *sdp, uint32_t flags, int pull)
365b3b94faaSDavid Teigland {
366b3b94faaSDavid Teigland 	uint64_t blkno = log_bmap(sdp, sdp->sd_log_flush_head);
367b3b94faaSDavid Teigland 	struct buffer_head *bh;
368b3b94faaSDavid Teigland 	struct gfs2_log_header *lh;
369b3b94faaSDavid Teigland 	unsigned int tail;
370b3b94faaSDavid Teigland 	uint32_t hash;
371b3b94faaSDavid Teigland 
372b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "log write header start (flags=%08x, pull=%d)\n", flags, pull); */
373b09e593dSSteven Whitehouse 
374b3b94faaSDavid Teigland 	bh = sb_getblk(sdp->sd_vfs, blkno);
375b3b94faaSDavid Teigland 	lock_buffer(bh);
376b3b94faaSDavid Teigland 	memset(bh->b_data, 0, bh->b_size);
377b3b94faaSDavid Teigland 	set_buffer_uptodate(bh);
378b3b94faaSDavid Teigland 	clear_buffer_dirty(bh);
379b3b94faaSDavid Teigland 	unlock_buffer(bh);
380b3b94faaSDavid Teigland 
381b3b94faaSDavid Teigland 	gfs2_ail1_empty(sdp, 0);
382b3b94faaSDavid Teigland 	tail = current_tail(sdp);
383b3b94faaSDavid Teigland 
384b3b94faaSDavid Teigland 	lh = (struct gfs2_log_header *)bh->b_data;
385b3b94faaSDavid Teigland 	memset(lh, 0, sizeof(struct gfs2_log_header));
386b3b94faaSDavid Teigland 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
387e3167dedSSteven Whitehouse 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
388e3167dedSSteven Whitehouse 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
389*e0f2bf78SSteven Whitehouse 	lh->lh_sequence = cpu_to_be64(sdp->sd_log_sequence++);
390*e0f2bf78SSteven Whitehouse 	lh->lh_flags = cpu_to_be32(flags);
391*e0f2bf78SSteven Whitehouse 	lh->lh_tail = cpu_to_be32(tail);
392*e0f2bf78SSteven Whitehouse 	lh->lh_blkno = cpu_to_be32(sdp->sd_log_flush_head);
393b3b94faaSDavid Teigland 	hash = gfs2_disk_hash(bh->b_data, sizeof(struct gfs2_log_header));
394b3b94faaSDavid Teigland 	lh->lh_hash = cpu_to_be32(hash);
395b3b94faaSDavid Teigland 
396b3b94faaSDavid Teigland 	set_buffer_dirty(bh);
397b3b94faaSDavid Teigland 	if (sync_dirty_buffer(bh))
398b3b94faaSDavid Teigland 		gfs2_io_error_bh(sdp, bh);
399b3b94faaSDavid Teigland 	brelse(bh);
400b3b94faaSDavid Teigland 
401b3b94faaSDavid Teigland 	if (sdp->sd_log_tail != tail)
402b3b94faaSDavid Teigland 		log_pull_tail(sdp, tail, pull);
403b3b94faaSDavid Teigland 	else
404b3b94faaSDavid Teigland 		gfs2_assert_withdraw(sdp, !pull);
405b3b94faaSDavid Teigland 
406b3b94faaSDavid Teigland 	sdp->sd_log_idle = (tail == sdp->sd_log_flush_head);
407b3b94faaSDavid Teigland 	log_incr_head(sdp);
408b09e593dSSteven Whitehouse 
409b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "log write header out\n"); */
410b3b94faaSDavid Teigland }
411b3b94faaSDavid Teigland 
412b3b94faaSDavid Teigland static void log_flush_commit(struct gfs2_sbd *sdp)
413b3b94faaSDavid Teigland {
414b3b94faaSDavid Teigland 	struct list_head *head = &sdp->sd_log_flush_list;
415b3b94faaSDavid Teigland 	struct gfs2_log_buf *lb;
416b3b94faaSDavid Teigland 	struct buffer_head *bh;
417f4154ea0SSteven Whitehouse #if 0
418b3b94faaSDavid Teigland 	unsigned int d;
419b3b94faaSDavid Teigland 
420b3b94faaSDavid Teigland 	d = log_distance(sdp, sdp->sd_log_flush_head, sdp->sd_log_head);
421b3b94faaSDavid Teigland 
422b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, d + 1 == sdp->sd_log_blks_reserved);
423f4154ea0SSteven Whitehouse #endif
424b3b94faaSDavid Teigland 
425b3b94faaSDavid Teigland 	while (!list_empty(head)) {
426b3b94faaSDavid Teigland 		lb = list_entry(head->next, struct gfs2_log_buf, lb_list);
427b3b94faaSDavid Teigland 		list_del(&lb->lb_list);
428b3b94faaSDavid Teigland 		bh = lb->lb_bh;
429b3b94faaSDavid Teigland 
430b3b94faaSDavid Teigland 		wait_on_buffer(bh);
431b3b94faaSDavid Teigland 		if (!buffer_uptodate(bh))
432b3b94faaSDavid Teigland 			gfs2_io_error_bh(sdp, bh);
433b3b94faaSDavid Teigland 		if (lb->lb_real) {
434b3b94faaSDavid Teigland 			while (atomic_read(&bh->b_count) != 1)  /* Grrrr... */
435b3b94faaSDavid Teigland 				schedule();
436b3b94faaSDavid Teigland 			free_buffer_head(bh);
437b3b94faaSDavid Teigland 		} else
438b3b94faaSDavid Teigland 			brelse(bh);
439b3b94faaSDavid Teigland 		kfree(lb);
440b3b94faaSDavid Teigland 	}
441b3b94faaSDavid Teigland 
442b3b94faaSDavid Teigland 	log_write_header(sdp, 0, 0);
443b3b94faaSDavid Teigland }
444b3b94faaSDavid Teigland 
445b3b94faaSDavid Teigland /**
446b09e593dSSteven Whitehouse  * gfs2_log_flush - flush incore transaction(s)
447b3b94faaSDavid Teigland  * @sdp: the filesystem
448b3b94faaSDavid Teigland  * @gl: The glock structure to flush.  If NULL, flush the whole incore log
449b3b94faaSDavid Teigland  *
450b3b94faaSDavid Teigland  */
451b3b94faaSDavid Teigland 
452b09e593dSSteven Whitehouse void gfs2_log_flush(struct gfs2_sbd *sdp, struct gfs2_glock *gl)
453b3b94faaSDavid Teigland {
454b3b94faaSDavid Teigland 	struct gfs2_ail *ai;
455b3b94faaSDavid Teigland 
456484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
457f55ab26aSSteven Whitehouse 
458f55ab26aSSteven Whitehouse 	if (gl) {
459f55ab26aSSteven Whitehouse 		gfs2_log_lock(sdp);
460f55ab26aSSteven Whitehouse 		if (list_empty(&gl->gl_le.le_list)) {
461f55ab26aSSteven Whitehouse 			gfs2_log_unlock(sdp);
462484adff8SSteven Whitehouse 			up_write(&sdp->sd_log_flush_lock);
463f55ab26aSSteven Whitehouse 			return;
464f55ab26aSSteven Whitehouse 		}
465f55ab26aSSteven Whitehouse 		gfs2_log_unlock(sdp);
466f55ab26aSSteven Whitehouse 	}
467f55ab26aSSteven Whitehouse 
468b09e593dSSteven Whitehouse 	ai = kzalloc(sizeof(struct gfs2_ail), GFP_NOFS | __GFP_NOFAIL);
469b09e593dSSteven Whitehouse 	INIT_LIST_HEAD(&ai->ai_ail1_list);
470b09e593dSSteven Whitehouse 	INIT_LIST_HEAD(&ai->ai_ail2_list);
471b3b94faaSDavid Teigland 
472b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
473b3b94faaSDavid Teigland 			sdp->sd_log_num_buf == sdp->sd_log_commited_buf);
474b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
475b3b94faaSDavid Teigland 			sdp->sd_log_num_revoke == sdp->sd_log_commited_revoke);
476b3b94faaSDavid Teigland 
477b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
478b3b94faaSDavid Teigland 	sdp->sd_log_flush_wrapped = 0;
479b3b94faaSDavid Teigland 	ai->ai_first = sdp->sd_log_flush_head;
480b3b94faaSDavid Teigland 
481b3b94faaSDavid Teigland 	lops_before_commit(sdp);
482b3b94faaSDavid Teigland 	if (!list_empty(&sdp->sd_log_flush_list))
483b3b94faaSDavid Teigland 		log_flush_commit(sdp);
484b3b94faaSDavid Teigland 	else if (sdp->sd_log_tail != current_tail(sdp) && !sdp->sd_log_idle)
485b3b94faaSDavid Teigland 		log_write_header(sdp, 0, PULL);
486b3b94faaSDavid Teigland 	lops_after_commit(sdp, ai);
487b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
488b09e593dSSteven Whitehouse 
489b09e593dSSteven Whitehouse 	/* printk(KERN_INFO "sd_log_num_hdrs %u\n", sdp->sd_log_num_hdrs); */
490f4154ea0SSteven Whitehouse 	sdp->sd_log_blks_free -= sdp->sd_log_num_hdrs;
491b3b94faaSDavid Teigland 
492b3b94faaSDavid Teigland 	sdp->sd_log_blks_reserved =
493b3b94faaSDavid Teigland 		sdp->sd_log_commited_buf =
494b09e593dSSteven Whitehouse 		sdp->sd_log_num_hdrs =
495b3b94faaSDavid Teigland 		sdp->sd_log_commited_revoke = 0;
496b3b94faaSDavid Teigland 
497b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
498b3b94faaSDavid Teigland 	if (!list_empty(&ai->ai_ail1_list)) {
499b3b94faaSDavid Teigland 		list_add(&ai->ai_list, &sdp->sd_ail1_list);
500b3b94faaSDavid Teigland 		ai = NULL;
501b3b94faaSDavid Teigland 	}
502b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
503b3b94faaSDavid Teigland 
504b3b94faaSDavid Teigland 	sdp->sd_vfs->s_dirt = 0;
505484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
506b3b94faaSDavid Teigland 
507b3b94faaSDavid Teigland 	kfree(ai);
508b3b94faaSDavid Teigland }
509b3b94faaSDavid Teigland 
510b3b94faaSDavid Teigland static void log_refund(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
511b3b94faaSDavid Teigland {
512b3b94faaSDavid Teigland 	unsigned int reserved = 1;
513b3b94faaSDavid Teigland 	unsigned int old;
514b3b94faaSDavid Teigland 
515b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
516b3b94faaSDavid Teigland 
517b3b94faaSDavid Teigland 	sdp->sd_log_commited_buf += tr->tr_num_buf_new - tr->tr_num_buf_rm;
518b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_buf) >= 0);
519b3b94faaSDavid Teigland 	sdp->sd_log_commited_revoke += tr->tr_num_revoke - tr->tr_num_revoke_rm;
520b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, ((int)sdp->sd_log_commited_revoke) >= 0);
521b3b94faaSDavid Teigland 
522b3b94faaSDavid Teigland 	if (sdp->sd_log_commited_buf)
523f4154ea0SSteven Whitehouse 		reserved += sdp->sd_log_commited_buf;
524b3b94faaSDavid Teigland 	if (sdp->sd_log_commited_revoke)
525b3b94faaSDavid Teigland 		reserved += gfs2_struct2blk(sdp, sdp->sd_log_commited_revoke,
526b3b94faaSDavid Teigland 					    sizeof(uint64_t));
527b3b94faaSDavid Teigland 
528b3b94faaSDavid Teigland 	old = sdp->sd_log_blks_free;
529b3b94faaSDavid Teigland 	sdp->sd_log_blks_free += tr->tr_reserved -
530b3b94faaSDavid Teigland 				 (reserved - sdp->sd_log_blks_reserved);
531b3b94faaSDavid Teigland 
532b09e593dSSteven Whitehouse 	gfs2_assert_withdraw(sdp, sdp->sd_log_blks_free >= old);
533b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp,
534f4154ea0SSteven Whitehouse 			     sdp->sd_log_blks_free <= sdp->sd_jdesc->jd_blocks +
535f4154ea0SSteven Whitehouse 			     sdp->sd_log_num_hdrs);
536b3b94faaSDavid Teigland 
537b3b94faaSDavid Teigland 	sdp->sd_log_blks_reserved = reserved;
538b3b94faaSDavid Teigland 
539b3b94faaSDavid Teigland 	gfs2_log_unlock(sdp);
540b3b94faaSDavid Teigland }
541b3b94faaSDavid Teigland 
542b3b94faaSDavid Teigland /**
543b3b94faaSDavid Teigland  * gfs2_log_commit - Commit a transaction to the log
544b3b94faaSDavid Teigland  * @sdp: the filesystem
545b3b94faaSDavid Teigland  * @tr: the transaction
546b3b94faaSDavid Teigland  *
547b3b94faaSDavid Teigland  * Returns: errno
548b3b94faaSDavid Teigland  */
549b3b94faaSDavid Teigland 
550b3b94faaSDavid Teigland void gfs2_log_commit(struct gfs2_sbd *sdp, struct gfs2_trans *tr)
551b3b94faaSDavid Teigland {
552b3b94faaSDavid Teigland 	log_refund(sdp, tr);
553b3b94faaSDavid Teigland 	lops_incore_commit(sdp, tr);
554b3b94faaSDavid Teigland 
555b3b94faaSDavid Teigland 	sdp->sd_vfs->s_dirt = 1;
556484adff8SSteven Whitehouse 	up_read(&sdp->sd_log_flush_lock);
557b3b94faaSDavid Teigland 
558b3b94faaSDavid Teigland 	gfs2_log_lock(sdp);
559b3b94faaSDavid Teigland 	if (sdp->sd_log_num_buf > gfs2_tune_get(sdp, gt_incore_log_blocks)) {
560b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
561b09e593dSSteven Whitehouse 		gfs2_log_flush(sdp, NULL);
562b3b94faaSDavid Teigland 	} else
563b3b94faaSDavid Teigland 		gfs2_log_unlock(sdp);
564b3b94faaSDavid Teigland }
565b3b94faaSDavid Teigland 
566b3b94faaSDavid Teigland /**
567b3b94faaSDavid Teigland  * gfs2_log_shutdown - write a shutdown header into a journal
568b3b94faaSDavid Teigland  * @sdp: the filesystem
569b3b94faaSDavid Teigland  *
570b3b94faaSDavid Teigland  */
571b3b94faaSDavid Teigland 
572b3b94faaSDavid Teigland void gfs2_log_shutdown(struct gfs2_sbd *sdp)
573b3b94faaSDavid Teigland {
574484adff8SSteven Whitehouse 	down_write(&sdp->sd_log_flush_lock);
575b3b94faaSDavid Teigland 
576b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_blks_reserved);
577b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_gl);
578b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_buf);
57918ec7d5cSSteven Whitehouse 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_jdata);
580b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_revoke);
581b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_rg);
582b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_databuf);
583190562bdSSteven Whitehouse 	gfs2_assert_withdraw(sdp, !sdp->sd_log_num_hdrs);
584b3b94faaSDavid Teigland 	gfs2_assert_withdraw(sdp, list_empty(&sdp->sd_ail1_list));
585b3b94faaSDavid Teigland 
586b3b94faaSDavid Teigland 	sdp->sd_log_flush_head = sdp->sd_log_head;
587b3b94faaSDavid Teigland 	sdp->sd_log_flush_wrapped = 0;
588b3b94faaSDavid Teigland 
589b3b94faaSDavid Teigland 	log_write_header(sdp, GFS2_LOG_HEAD_UNMOUNT, 0);
590b3b94faaSDavid Teigland 
591f4154ea0SSteven Whitehouse 	/* printk(KERN_INFO "sd_log_blks_free %u, sd_jdesc->jd_blocks %u\n", sdp->sd_log_blks_free, sdp->sd_jdesc->jd_blocks); */
592a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_blks_free == sdp->sd_jdesc->jd_blocks);
593a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, sdp->sd_log_head == sdp->sd_log_tail);
594a74604beSSteven Whitehouse 	gfs2_assert_warn(sdp, list_empty(&sdp->sd_ail2_list));
595b3b94faaSDavid Teigland 
596b3b94faaSDavid Teigland 	sdp->sd_log_head = sdp->sd_log_flush_head;
597b3b94faaSDavid Teigland 	sdp->sd_log_tail = sdp->sd_log_head;
598b3b94faaSDavid Teigland 
599484adff8SSteven Whitehouse 	up_write(&sdp->sd_log_flush_lock);
600b3b94faaSDavid Teigland }
601b3b94faaSDavid Teigland 
602