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