xref: /openbmc/linux/fs/gfs2/recovery.c (revision 7276b3b0c77101f8b3f4e45e89a29cf9045e831a)
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
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>
18b3b94faaSDavid Teigland 
19b3b94faaSDavid Teigland #include "gfs2.h"
205c676f6dSSteven Whitehouse #include "incore.h"
21b3b94faaSDavid Teigland #include "bmap.h"
22b3b94faaSDavid Teigland #include "glock.h"
23b3b94faaSDavid Teigland #include "glops.h"
24b3b94faaSDavid Teigland #include "lm.h"
25b3b94faaSDavid Teigland #include "lops.h"
26b3b94faaSDavid Teigland #include "meta_io.h"
27b3b94faaSDavid Teigland #include "recovery.h"
28b3b94faaSDavid Teigland #include "super.h"
295c676f6dSSteven Whitehouse #include "util.h"
3071b86f56SSteven Whitehouse #include "dir.h"
31b3b94faaSDavid Teigland 
32b3b94faaSDavid Teigland int gfs2_replay_read_block(struct gfs2_jdesc *jd, unsigned int blk,
33b3b94faaSDavid Teigland 			   struct buffer_head **bh)
34b3b94faaSDavid Teigland {
35feaa7bbaSSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
365c676f6dSSteven Whitehouse 	struct gfs2_glock *gl = ip->i_gl;
37b3b94faaSDavid Teigland 	int new = 0;
38cd915493SSteven Whitehouse 	u64 dblock;
39cd915493SSteven Whitehouse 	u32 extlen;
40b3b94faaSDavid Teigland 	int error;
41b3b94faaSDavid Teigland 
42feaa7bbaSSteven Whitehouse 	error = gfs2_extent_map(&ip->i_inode, blk, &new, &dblock, &extlen);
43b3b94faaSDavid Teigland 	if (error)
44b3b94faaSDavid Teigland 		return error;
45b3b94faaSDavid Teigland 	if (!dblock) {
465c676f6dSSteven Whitehouse 		gfs2_consist_inode(ip);
47b3b94faaSDavid Teigland 		return -EIO;
48b3b94faaSDavid Teigland 	}
49b3b94faaSDavid Teigland 
50*7276b3b0SSteven Whitehouse 	*bh = gfs2_meta_ra(gl, dblock, extlen);
51b3b94faaSDavid Teigland 
52b3b94faaSDavid Teigland 	return error;
53b3b94faaSDavid Teigland }
54b3b94faaSDavid Teigland 
55cd915493SSteven Whitehouse int gfs2_revoke_add(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
56b3b94faaSDavid Teigland {
57b3b94faaSDavid Teigland 	struct list_head *head = &sdp->sd_revoke_list;
58b3b94faaSDavid Teigland 	struct gfs2_revoke_replay *rr;
59b3b94faaSDavid Teigland 	int found = 0;
60b3b94faaSDavid Teigland 
61b3b94faaSDavid Teigland 	list_for_each_entry(rr, head, rr_list) {
62b3b94faaSDavid Teigland 		if (rr->rr_blkno == blkno) {
63b3b94faaSDavid Teigland 			found = 1;
64b3b94faaSDavid Teigland 			break;
65b3b94faaSDavid Teigland 		}
66b3b94faaSDavid Teigland 	}
67b3b94faaSDavid Teigland 
68b3b94faaSDavid Teigland 	if (found) {
69b3b94faaSDavid Teigland 		rr->rr_where = where;
70b3b94faaSDavid Teigland 		return 0;
71b3b94faaSDavid Teigland 	}
72b3b94faaSDavid Teigland 
73b3b94faaSDavid Teigland 	rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_KERNEL);
74b3b94faaSDavid Teigland 	if (!rr)
75b3b94faaSDavid Teigland 		return -ENOMEM;
76b3b94faaSDavid Teigland 
77b3b94faaSDavid Teigland 	rr->rr_blkno = blkno;
78b3b94faaSDavid Teigland 	rr->rr_where = where;
79b3b94faaSDavid Teigland 	list_add(&rr->rr_list, head);
80b3b94faaSDavid Teigland 
81b3b94faaSDavid Teigland 	return 1;
82b3b94faaSDavid Teigland }
83b3b94faaSDavid Teigland 
84cd915493SSteven Whitehouse int gfs2_revoke_check(struct gfs2_sbd *sdp, u64 blkno, unsigned int where)
85b3b94faaSDavid Teigland {
86b3b94faaSDavid Teigland 	struct gfs2_revoke_replay *rr;
87b3b94faaSDavid Teigland 	int wrap, a, b, revoke;
88b3b94faaSDavid Teigland 	int found = 0;
89b3b94faaSDavid Teigland 
90b3b94faaSDavid Teigland 	list_for_each_entry(rr, &sdp->sd_revoke_list, rr_list) {
91b3b94faaSDavid Teigland 		if (rr->rr_blkno == blkno) {
92b3b94faaSDavid Teigland 			found = 1;
93b3b94faaSDavid Teigland 			break;
94b3b94faaSDavid Teigland 		}
95b3b94faaSDavid Teigland 	}
96b3b94faaSDavid Teigland 
97b3b94faaSDavid Teigland 	if (!found)
98b3b94faaSDavid Teigland 		return 0;
99b3b94faaSDavid Teigland 
100b3b94faaSDavid Teigland 	wrap = (rr->rr_where < sdp->sd_replay_tail);
101b3b94faaSDavid Teigland 	a = (sdp->sd_replay_tail < where);
102b3b94faaSDavid Teigland 	b = (where < rr->rr_where);
103b3b94faaSDavid Teigland 	revoke = (wrap) ? (a || b) : (a && b);
104b3b94faaSDavid Teigland 
105b3b94faaSDavid Teigland 	return revoke;
106b3b94faaSDavid Teigland }
107b3b94faaSDavid Teigland 
108b3b94faaSDavid Teigland void gfs2_revoke_clean(struct gfs2_sbd *sdp)
109b3b94faaSDavid Teigland {
110b3b94faaSDavid Teigland 	struct list_head *head = &sdp->sd_revoke_list;
111b3b94faaSDavid Teigland 	struct gfs2_revoke_replay *rr;
112b3b94faaSDavid Teigland 
113b3b94faaSDavid Teigland 	while (!list_empty(head)) {
114b3b94faaSDavid Teigland 		rr = list_entry(head->next, struct gfs2_revoke_replay, rr_list);
115b3b94faaSDavid Teigland 		list_del(&rr->rr_list);
116b3b94faaSDavid Teigland 		kfree(rr);
117b3b94faaSDavid Teigland 	}
118b3b94faaSDavid Teigland }
119b3b94faaSDavid Teigland 
120b3b94faaSDavid Teigland /**
121b3b94faaSDavid Teigland  * get_log_header - read the log header for a given segment
122b3b94faaSDavid Teigland  * @jd: the journal
123b3b94faaSDavid Teigland  * @blk: the block to look at
124b3b94faaSDavid Teigland  * @lh: the log header to return
125b3b94faaSDavid Teigland  *
126b3b94faaSDavid Teigland  * Read the log header for a given segement in a given journal.  Do a few
127b3b94faaSDavid Teigland  * sanity checks on it.
128b3b94faaSDavid Teigland  *
129b3b94faaSDavid Teigland  * Returns: 0 on success,
130b3b94faaSDavid Teigland  *          1 if the header was invalid or incomplete,
131b3b94faaSDavid Teigland  *          errno on error
132b3b94faaSDavid Teigland  */
133b3b94faaSDavid Teigland 
134b3b94faaSDavid Teigland static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
135b3b94faaSDavid Teigland 			  struct gfs2_log_header *head)
136b3b94faaSDavid Teigland {
137b3b94faaSDavid Teigland 	struct buffer_head *bh;
138b3b94faaSDavid Teigland 	struct gfs2_log_header lh;
139cd915493SSteven Whitehouse 	u32 hash;
140b3b94faaSDavid Teigland 	int error;
141b3b94faaSDavid Teigland 
142b3b94faaSDavid Teigland 	error = gfs2_replay_read_block(jd, blk, &bh);
143b3b94faaSDavid Teigland 	if (error)
144b3b94faaSDavid Teigland 		return error;
145b3b94faaSDavid Teigland 
146b3b94faaSDavid Teigland 	memcpy(&lh, bh->b_data, sizeof(struct gfs2_log_header));
147b3b94faaSDavid Teigland 	lh.lh_hash = 0;
148b3b94faaSDavid Teigland 	hash = gfs2_disk_hash((char *)&lh, sizeof(struct gfs2_log_header));
149b3b94faaSDavid Teigland 	gfs2_log_header_in(&lh, bh->b_data);
150b3b94faaSDavid Teigland 
151b3b94faaSDavid Teigland 	brelse(bh);
152b3b94faaSDavid Teigland 
153b3b94faaSDavid Teigland 	if (lh.lh_header.mh_magic != GFS2_MAGIC ||
154b3b94faaSDavid Teigland 	    lh.lh_header.mh_type != GFS2_METATYPE_LH ||
15559a1cc6bSSteven Whitehouse 	    lh.lh_blkno != blk || lh.lh_hash != hash)
156b3b94faaSDavid Teigland 		return 1;
157b3b94faaSDavid Teigland 
158b3b94faaSDavid Teigland 	*head = lh;
159b3b94faaSDavid Teigland 
160b3b94faaSDavid Teigland 	return 0;
161b3b94faaSDavid Teigland }
162b3b94faaSDavid Teigland 
163b3b94faaSDavid Teigland /**
164b3b94faaSDavid Teigland  * find_good_lh - find a good log header
165b3b94faaSDavid Teigland  * @jd: the journal
166b3b94faaSDavid Teigland  * @blk: the segment to start searching from
167b3b94faaSDavid Teigland  * @lh: the log header to fill in
168b3b94faaSDavid Teigland  * @forward: if true search forward in the log, else search backward
169b3b94faaSDavid Teigland  *
170b3b94faaSDavid Teigland  * Call get_log_header() to get a log header for a segment, but if the
171b3b94faaSDavid Teigland  * segment is bad, either scan forward or backward until we find a good one.
172b3b94faaSDavid Teigland  *
173b3b94faaSDavid Teigland  * Returns: errno
174b3b94faaSDavid Teigland  */
175b3b94faaSDavid Teigland 
176b3b94faaSDavid Teigland static int find_good_lh(struct gfs2_jdesc *jd, unsigned int *blk,
177b3b94faaSDavid Teigland 			struct gfs2_log_header *head)
178b3b94faaSDavid Teigland {
179b3b94faaSDavid Teigland 	unsigned int orig_blk = *blk;
180b3b94faaSDavid Teigland 	int error;
181b3b94faaSDavid Teigland 
182b3b94faaSDavid Teigland 	for (;;) {
183b3b94faaSDavid Teigland 		error = get_log_header(jd, *blk, head);
184b3b94faaSDavid Teigland 		if (error <= 0)
185b3b94faaSDavid Teigland 			return error;
186b3b94faaSDavid Teigland 
187b3b94faaSDavid Teigland 		if (++*blk == jd->jd_blocks)
188b3b94faaSDavid Teigland 			*blk = 0;
189b3b94faaSDavid Teigland 
190b3b94faaSDavid Teigland 		if (*blk == orig_blk) {
191feaa7bbaSSteven Whitehouse 			gfs2_consist_inode(GFS2_I(jd->jd_inode));
192b3b94faaSDavid Teigland 			return -EIO;
193b3b94faaSDavid Teigland 		}
194b3b94faaSDavid Teigland 	}
195b3b94faaSDavid Teigland }
196b3b94faaSDavid Teigland 
197b3b94faaSDavid Teigland /**
198b3b94faaSDavid Teigland  * jhead_scan - make sure we've found the head of the log
199b3b94faaSDavid Teigland  * @jd: the journal
200b3b94faaSDavid Teigland  * @head: this is filled in with the log descriptor of the head
201b3b94faaSDavid Teigland  *
202b3b94faaSDavid Teigland  * At this point, seg and lh should be either the head of the log or just
203b3b94faaSDavid Teigland  * before.  Scan forward until we find the head.
204b3b94faaSDavid Teigland  *
205b3b94faaSDavid Teigland  * Returns: errno
206b3b94faaSDavid Teigland  */
207b3b94faaSDavid Teigland 
208b3b94faaSDavid Teigland static int jhead_scan(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
209b3b94faaSDavid Teigland {
210b3b94faaSDavid Teigland 	unsigned int blk = head->lh_blkno;
211b3b94faaSDavid Teigland 	struct gfs2_log_header lh;
212b3b94faaSDavid Teigland 	int error;
213b3b94faaSDavid Teigland 
214b3b94faaSDavid Teigland 	for (;;) {
215b3b94faaSDavid Teigland 		if (++blk == jd->jd_blocks)
216b3b94faaSDavid Teigland 			blk = 0;
217b3b94faaSDavid Teigland 
218b3b94faaSDavid Teigland 		error = get_log_header(jd, blk, &lh);
219b3b94faaSDavid Teigland 		if (error < 0)
220b3b94faaSDavid Teigland 			return error;
221b3b94faaSDavid Teigland 		if (error == 1)
222b3b94faaSDavid Teigland 			continue;
223b3b94faaSDavid Teigland 
224b3b94faaSDavid Teigland 		if (lh.lh_sequence == head->lh_sequence) {
225feaa7bbaSSteven Whitehouse 			gfs2_consist_inode(GFS2_I(jd->jd_inode));
226b3b94faaSDavid Teigland 			return -EIO;
227b3b94faaSDavid Teigland 		}
228b3b94faaSDavid Teigland 		if (lh.lh_sequence < head->lh_sequence)
229b3b94faaSDavid Teigland 			break;
230b3b94faaSDavid Teigland 
231b3b94faaSDavid Teigland 		*head = lh;
232b3b94faaSDavid Teigland 	}
233b3b94faaSDavid Teigland 
234b3b94faaSDavid Teigland 	return 0;
235b3b94faaSDavid Teigland }
236b3b94faaSDavid Teigland 
237b3b94faaSDavid Teigland /**
238b3b94faaSDavid Teigland  * gfs2_find_jhead - find the head of a log
239b3b94faaSDavid Teigland  * @jd: the journal
240b3b94faaSDavid Teigland  * @head: the log descriptor for the head of the log is returned here
241b3b94faaSDavid Teigland  *
242b3b94faaSDavid Teigland  * Do a binary search of a journal and find the valid log entry with the
243b3b94faaSDavid Teigland  * highest sequence number.  (i.e. the log head)
244b3b94faaSDavid Teigland  *
245b3b94faaSDavid Teigland  * Returns: errno
246b3b94faaSDavid Teigland  */
247b3b94faaSDavid Teigland 
248b3b94faaSDavid Teigland int gfs2_find_jhead(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
249b3b94faaSDavid Teigland {
250b3b94faaSDavid Teigland 	struct gfs2_log_header lh_1, lh_m;
251cd915493SSteven Whitehouse 	u32 blk_1, blk_2, blk_m;
252b3b94faaSDavid Teigland 	int error;
253b3b94faaSDavid Teigland 
254b3b94faaSDavid Teigland 	blk_1 = 0;
255b3b94faaSDavid Teigland 	blk_2 = jd->jd_blocks - 1;
256b3b94faaSDavid Teigland 
257b3b94faaSDavid Teigland 	for (;;) {
258b3b94faaSDavid Teigland 		blk_m = (blk_1 + blk_2) / 2;
259b3b94faaSDavid Teigland 
260b3b94faaSDavid Teigland 		error = find_good_lh(jd, &blk_1, &lh_1);
261b3b94faaSDavid Teigland 		if (error)
262b3b94faaSDavid Teigland 			return error;
263b3b94faaSDavid Teigland 
264b3b94faaSDavid Teigland 		error = find_good_lh(jd, &blk_m, &lh_m);
265b3b94faaSDavid Teigland 		if (error)
266b3b94faaSDavid Teigland 			return error;
267b3b94faaSDavid Teigland 
268b3b94faaSDavid Teigland 		if (blk_1 == blk_m || blk_m == blk_2)
269b3b94faaSDavid Teigland 			break;
270b3b94faaSDavid Teigland 
271b3b94faaSDavid Teigland 		if (lh_1.lh_sequence <= lh_m.lh_sequence)
272b3b94faaSDavid Teigland 			blk_1 = blk_m;
273b3b94faaSDavid Teigland 		else
274b3b94faaSDavid Teigland 			blk_2 = blk_m;
275b3b94faaSDavid Teigland 	}
276b3b94faaSDavid Teigland 
277b3b94faaSDavid Teigland 	error = jhead_scan(jd, &lh_1);
278b3b94faaSDavid Teigland 	if (error)
279b3b94faaSDavid Teigland 		return error;
280b3b94faaSDavid Teigland 
281b3b94faaSDavid Teigland 	*head = lh_1;
282b3b94faaSDavid Teigland 
283b3b94faaSDavid Teigland 	return error;
284b3b94faaSDavid Teigland }
285b3b94faaSDavid Teigland 
286b3b94faaSDavid Teigland /**
287b3b94faaSDavid Teigland  * foreach_descriptor - go through the active part of the log
288b3b94faaSDavid Teigland  * @jd: the journal
289b3b94faaSDavid Teigland  * @start: the first log header in the active region
290b3b94faaSDavid Teigland  * @end: the last log header (don't process the contents of this entry))
291b3b94faaSDavid Teigland  *
292b3b94faaSDavid Teigland  * Call a given function once for every log descriptor in the active
293b3b94faaSDavid Teigland  * portion of the log.
294b3b94faaSDavid Teigland  *
295b3b94faaSDavid Teigland  * Returns: errno
296b3b94faaSDavid Teigland  */
297b3b94faaSDavid Teigland 
298b3b94faaSDavid Teigland static int foreach_descriptor(struct gfs2_jdesc *jd, unsigned int start,
299b3b94faaSDavid Teigland 			      unsigned int end, int pass)
300b3b94faaSDavid Teigland {
301feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
302b3b94faaSDavid Teigland 	struct buffer_head *bh;
303b3b94faaSDavid Teigland 	struct gfs2_log_descriptor *ld;
304b3b94faaSDavid Teigland 	int error = 0;
305b3b94faaSDavid Teigland 	u32 length;
306b3b94faaSDavid Teigland 	__be64 *ptr;
307b3b94faaSDavid Teigland 	unsigned int offset = sizeof(struct gfs2_log_descriptor);
308a67cdbd4SSteven Whitehouse 	offset += sizeof(__be64) - 1;
309b3b94faaSDavid Teigland 	offset &= ~(sizeof(__be64) - 1);
310b3b94faaSDavid Teigland 
311b3b94faaSDavid Teigland 	while (start != end) {
312b3b94faaSDavid Teigland 		error = gfs2_replay_read_block(jd, start, &bh);
313b3b94faaSDavid Teigland 		if (error)
314b3b94faaSDavid Teigland 			return error;
315b3b94faaSDavid Teigland 		if (gfs2_meta_check(sdp, bh)) {
316b3b94faaSDavid Teigland 			brelse(bh);
317b3b94faaSDavid Teigland 			return -EIO;
318b3b94faaSDavid Teigland 		}
319b3b94faaSDavid Teigland 		ld = (struct gfs2_log_descriptor *)bh->b_data;
320b3b94faaSDavid Teigland 		length = be32_to_cpu(ld->ld_length);
321b3b94faaSDavid Teigland 
322e3167dedSSteven Whitehouse 		if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
323b3b94faaSDavid Teigland 			struct gfs2_log_header lh;
324b3b94faaSDavid Teigland 			error = get_log_header(jd, start, &lh);
325b3b94faaSDavid Teigland 			if (!error) {
326b3b94faaSDavid Teigland 				gfs2_replay_incr_blk(sdp, &start);
32788721877SRussell Cattelan 				brelse(bh);
328b3b94faaSDavid Teigland 				continue;
329b3b94faaSDavid Teigland 			}
330b3b94faaSDavid Teigland 			if (error == 1) {
331feaa7bbaSSteven Whitehouse 				gfs2_consist_inode(GFS2_I(jd->jd_inode));
332b3b94faaSDavid Teigland 				error = -EIO;
333b3b94faaSDavid Teigland 			}
334b3b94faaSDavid Teigland 			brelse(bh);
335b3b94faaSDavid Teigland 			return error;
336b3b94faaSDavid Teigland 		} else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
337b3b94faaSDavid Teigland 			brelse(bh);
338b3b94faaSDavid Teigland 			return -EIO;
339b3b94faaSDavid Teigland 		}
340b3b94faaSDavid Teigland 		ptr = (__be64 *)(bh->b_data + offset);
341b3b94faaSDavid Teigland 		error = lops_scan_elements(jd, start, ld, ptr, pass);
342b3b94faaSDavid Teigland 		if (error) {
343b3b94faaSDavid Teigland 			brelse(bh);
344b3b94faaSDavid Teigland 			return error;
345b3b94faaSDavid Teigland 		}
346b3b94faaSDavid Teigland 
347b3b94faaSDavid Teigland 		while (length--)
348b3b94faaSDavid Teigland 			gfs2_replay_incr_blk(sdp, &start);
349b3b94faaSDavid Teigland 
350b3b94faaSDavid Teigland 		brelse(bh);
351b3b94faaSDavid Teigland 	}
352b3b94faaSDavid Teigland 
353b3b94faaSDavid Teigland 	return 0;
354b3b94faaSDavid Teigland }
355b3b94faaSDavid Teigland 
356b3b94faaSDavid Teigland /**
357b3b94faaSDavid Teigland  * clean_journal - mark a dirty journal as being clean
358b3b94faaSDavid Teigland  * @sdp: the filesystem
359b3b94faaSDavid Teigland  * @jd: the journal
360b3b94faaSDavid Teigland  * @gl: the journal's glock
361b3b94faaSDavid Teigland  * @head: the head journal to start from
362b3b94faaSDavid Teigland  *
363b3b94faaSDavid Teigland  * Returns: errno
364b3b94faaSDavid Teigland  */
365b3b94faaSDavid Teigland 
366b3b94faaSDavid Teigland static int clean_journal(struct gfs2_jdesc *jd, struct gfs2_log_header *head)
367b3b94faaSDavid Teigland {
368feaa7bbaSSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
369feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
370b3b94faaSDavid Teigland 	unsigned int lblock;
371b3b94faaSDavid Teigland 	struct gfs2_log_header *lh;
372cd915493SSteven Whitehouse 	u32 hash;
373b3b94faaSDavid Teigland 	struct buffer_head *bh;
374b3b94faaSDavid Teigland 	int error;
3757a6bbacbSSteven Whitehouse 	struct buffer_head bh_map;
376b3b94faaSDavid Teigland 
377b3b94faaSDavid Teigland 	lblock = head->lh_blkno;
378b3b94faaSDavid Teigland 	gfs2_replay_incr_blk(sdp, &lblock);
3797a6bbacbSSteven Whitehouse 	error = gfs2_block_map(&ip->i_inode, lblock, 0, &bh_map, 1);
380b3b94faaSDavid Teigland 	if (error)
381b3b94faaSDavid Teigland 		return error;
3827a6bbacbSSteven Whitehouse 	if (!bh_map.b_blocknr) {
383b3b94faaSDavid Teigland 		gfs2_consist_inode(ip);
384b3b94faaSDavid Teigland 		return -EIO;
385b3b94faaSDavid Teigland 	}
386b3b94faaSDavid Teigland 
3877a6bbacbSSteven Whitehouse 	bh = sb_getblk(sdp->sd_vfs, bh_map.b_blocknr);
388b3b94faaSDavid Teigland 	lock_buffer(bh);
389b3b94faaSDavid Teigland 	memset(bh->b_data, 0, bh->b_size);
390b3b94faaSDavid Teigland 	set_buffer_uptodate(bh);
391b3b94faaSDavid Teigland 	clear_buffer_dirty(bh);
392b3b94faaSDavid Teigland 	unlock_buffer(bh);
393b3b94faaSDavid Teigland 
394b3b94faaSDavid Teigland 	lh = (struct gfs2_log_header *)bh->b_data;
395b3b94faaSDavid Teigland 	memset(lh, 0, sizeof(struct gfs2_log_header));
396b3b94faaSDavid Teigland 	lh->lh_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
39788721877SRussell Cattelan 	lh->lh_header.mh_type = cpu_to_be32(GFS2_METATYPE_LH);
398e3167dedSSteven Whitehouse 	lh->lh_header.mh_format = cpu_to_be32(GFS2_FORMAT_LH);
399b3b94faaSDavid Teigland 	lh->lh_sequence = cpu_to_be64(head->lh_sequence + 1);
400b3b94faaSDavid Teigland 	lh->lh_flags = cpu_to_be32(GFS2_LOG_HEAD_UNMOUNT);
401b3b94faaSDavid Teigland 	lh->lh_blkno = cpu_to_be32(lblock);
402b3b94faaSDavid Teigland 	hash = gfs2_disk_hash((const char *)lh, sizeof(struct gfs2_log_header));
403b3b94faaSDavid Teigland 	lh->lh_hash = cpu_to_be32(hash);
404b3b94faaSDavid Teigland 
405b3b94faaSDavid Teigland 	set_buffer_dirty(bh);
406b3b94faaSDavid Teigland 	if (sync_dirty_buffer(bh))
407b3b94faaSDavid Teigland 		gfs2_io_error_bh(sdp, bh);
408b3b94faaSDavid Teigland 	brelse(bh);
409b3b94faaSDavid Teigland 
410b3b94faaSDavid Teigland 	return error;
411b3b94faaSDavid Teigland }
412b3b94faaSDavid Teigland 
413b3b94faaSDavid Teigland /**
414b3b94faaSDavid Teigland  * gfs2_recover_journal - recovery a given journal
415b3b94faaSDavid Teigland  * @jd: the struct gfs2_jdesc describing the journal
416b3b94faaSDavid Teigland  *
417b3b94faaSDavid Teigland  * Acquire the journal's lock, check to see if the journal is clean, and
418b3b94faaSDavid Teigland  * do recovery if necessary.
419b3b94faaSDavid Teigland  *
420b3b94faaSDavid Teigland  * Returns: errno
421b3b94faaSDavid Teigland  */
422b3b94faaSDavid Teigland 
423c63e31c2SDavid Teigland int gfs2_recover_journal(struct gfs2_jdesc *jd)
424b3b94faaSDavid Teigland {
425feaa7bbaSSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
426feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
427b3b94faaSDavid Teigland 	struct gfs2_log_header head;
428b3b94faaSDavid Teigland 	struct gfs2_holder j_gh, ji_gh, t_gh;
429b3b94faaSDavid Teigland 	unsigned long t;
430b3b94faaSDavid Teigland 	int ro = 0;
431b3b94faaSDavid Teigland 	unsigned int pass;
432b3b94faaSDavid Teigland 	int error;
433b3b94faaSDavid Teigland 
4345965b1f4SSteven Whitehouse 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
4355965b1f4SSteven Whitehouse 		fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
4365965b1f4SSteven Whitehouse 			jd->jd_jid);
437b3b94faaSDavid Teigland 
438b3b94faaSDavid Teigland 		/* Aquire the journal lock so we can do recovery */
439b3b94faaSDavid Teigland 
440c63e31c2SDavid Teigland 		error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
441b3b94faaSDavid Teigland 					  LM_ST_EXCLUSIVE,
442c63e31c2SDavid Teigland 					  LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
443c63e31c2SDavid Teigland 					  &j_gh);
444b3b94faaSDavid Teigland 		switch (error) {
445b3b94faaSDavid Teigland 		case 0:
446b3b94faaSDavid Teigland 			break;
447b3b94faaSDavid Teigland 
448b3b94faaSDavid Teigland 		case GLR_TRYFAILED:
449b3b94faaSDavid Teigland 			fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
450b3b94faaSDavid Teigland 			error = 0;
451b3b94faaSDavid Teigland 
452b3b94faaSDavid Teigland 		default:
453b3b94faaSDavid Teigland 			goto fail;
454b3b94faaSDavid Teigland 		};
455b3b94faaSDavid Teigland 
4565c676f6dSSteven Whitehouse 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
457b3b94faaSDavid Teigland 					   LM_FLAG_NOEXP, &ji_gh);
458b3b94faaSDavid Teigland 		if (error)
459b3b94faaSDavid Teigland 			goto fail_gunlock_j;
4605965b1f4SSteven Whitehouse 	} else {
4615965b1f4SSteven Whitehouse 		fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
4625965b1f4SSteven Whitehouse 	}
463b3b94faaSDavid Teigland 
464b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
465b3b94faaSDavid Teigland 
466b3b94faaSDavid Teigland 	error = gfs2_jdesc_check(jd);
467b3b94faaSDavid Teigland 	if (error)
468b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
469b3b94faaSDavid Teigland 
470b3b94faaSDavid Teigland 	error = gfs2_find_jhead(jd, &head);
471b3b94faaSDavid Teigland 	if (error)
472b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
473b3b94faaSDavid Teigland 
474b3b94faaSDavid Teigland 	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
475b3b94faaSDavid Teigland 		fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
476b3b94faaSDavid Teigland 			jd->jd_jid);
477b3b94faaSDavid Teigland 
478b3b94faaSDavid Teigland 		t = jiffies;
479b3b94faaSDavid Teigland 
480b3b94faaSDavid Teigland 		/* Acquire a shared hold on the transaction lock */
481b3b94faaSDavid Teigland 
48259a1cc6bSSteven Whitehouse 		error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
4835965b1f4SSteven Whitehouse 					   LM_FLAG_NOEXP | LM_FLAG_PRIORITY |
48459a1cc6bSSteven Whitehouse 					   GL_NOCANCEL | GL_NOCACHE, &t_gh);
485b3b94faaSDavid Teigland 		if (error)
486b3b94faaSDavid Teigland 			goto fail_gunlock_ji;
487b3b94faaSDavid Teigland 
488b3b94faaSDavid Teigland 		if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
489b3b94faaSDavid Teigland 			if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
490b3b94faaSDavid Teigland 				ro = 1;
491b3b94faaSDavid Teigland 		} else {
492b3b94faaSDavid Teigland 			if (sdp->sd_vfs->s_flags & MS_RDONLY)
493b3b94faaSDavid Teigland 				ro = 1;
494b3b94faaSDavid Teigland 		}
495b3b94faaSDavid Teigland 
496b3b94faaSDavid Teigland 		if (ro) {
497b3b94faaSDavid Teigland 			fs_warn(sdp, "jid=%u: Can't replay: read-only FS\n",
498b3b94faaSDavid Teigland 				jd->jd_jid);
499b3b94faaSDavid Teigland 			error = -EROFS;
500b3b94faaSDavid Teigland 			goto fail_gunlock_tr;
501b3b94faaSDavid Teigland 		}
502b3b94faaSDavid Teigland 
503b3b94faaSDavid Teigland 		fs_info(sdp, "jid=%u: Replaying journal...\n", jd->jd_jid);
504b3b94faaSDavid Teigland 
505b3b94faaSDavid Teigland 		for (pass = 0; pass < 2; pass++) {
506b3b94faaSDavid Teigland 			lops_before_scan(jd, &head, pass);
507b3b94faaSDavid Teigland 			error = foreach_descriptor(jd, head.lh_tail,
508b3b94faaSDavid Teigland 						   head.lh_blkno, pass);
509b3b94faaSDavid Teigland 			lops_after_scan(jd, error, pass);
510b3b94faaSDavid Teigland 			if (error)
511b3b94faaSDavid Teigland 				goto fail_gunlock_tr;
512b3b94faaSDavid Teigland 		}
513b3b94faaSDavid Teigland 
514b3b94faaSDavid Teigland 		error = clean_journal(jd, &head);
515b3b94faaSDavid Teigland 		if (error)
516b3b94faaSDavid Teigland 			goto fail_gunlock_tr;
517b3b94faaSDavid Teigland 
518b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&t_gh);
5195c676f6dSSteven Whitehouse 		t = DIV_ROUND_UP(jiffies - t, HZ);
520b3b94faaSDavid Teigland 		fs_info(sdp, "jid=%u: Journal replayed in %lus\n",
521b3b94faaSDavid Teigland 			jd->jd_jid, t);
522b3b94faaSDavid Teigland 	}
523b3b94faaSDavid Teigland 
5245965b1f4SSteven Whitehouse 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
525b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&ji_gh);
526b3b94faaSDavid Teigland 
527b3b94faaSDavid Teigland 	gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
528b3b94faaSDavid Teigland 
5295965b1f4SSteven Whitehouse 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid)
530b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
531b3b94faaSDavid Teigland 
532b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
533b3b94faaSDavid Teigland 	return 0;
534b3b94faaSDavid Teigland 
535b3b94faaSDavid Teigland fail_gunlock_tr:
536b3b94faaSDavid Teigland 	gfs2_glock_dq_uninit(&t_gh);
537b3b94faaSDavid Teigland fail_gunlock_ji:
5385965b1f4SSteven Whitehouse 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
539b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&ji_gh);
540b3b94faaSDavid Teigland fail_gunlock_j:
541b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
5425965b1f4SSteven Whitehouse 	}
543b3b94faaSDavid Teigland 
544b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
545b3b94faaSDavid Teigland 
546b3b94faaSDavid Teigland fail:
547b3b94faaSDavid Teigland 	gfs2_lm_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
548b3b94faaSDavid Teigland 	return error;
549b3b94faaSDavid Teigland }
550b3b94faaSDavid Teigland 
551b3b94faaSDavid Teigland /**
552b3b94faaSDavid Teigland  * gfs2_check_journals - Recover any dirty journals
553b3b94faaSDavid Teigland  * @sdp: the filesystem
554b3b94faaSDavid Teigland  *
555b3b94faaSDavid Teigland  */
556b3b94faaSDavid Teigland 
557b3b94faaSDavid Teigland void gfs2_check_journals(struct gfs2_sbd *sdp)
558b3b94faaSDavid Teigland {
559b3b94faaSDavid Teigland 	struct gfs2_jdesc *jd;
560b3b94faaSDavid Teigland 
561b3b94faaSDavid Teigland 	for (;;) {
562b3b94faaSDavid Teigland 		jd = gfs2_jdesc_find_dirty(sdp);
563b3b94faaSDavid Teigland 		if (!jd)
564b3b94faaSDavid Teigland 			break;
565b3b94faaSDavid Teigland 
566b3b94faaSDavid Teigland 		if (jd != sdp->sd_jdesc)
567c63e31c2SDavid Teigland 			gfs2_recover_journal(jd);
568b3b94faaSDavid Teigland 	}
569b3b94faaSDavid Teigland }
570b3b94faaSDavid Teigland 
571