xref: /openbmc/linux/fs/gfs2/recovery.c (revision 969183bc68bc27d637d6d29e81d71cf854d0ca61)
17336d0e6SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b3b94faaSDavid Teigland /*
3b3b94faaSDavid Teigland  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
43a8a9a10SSteven Whitehouse  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5b3b94faaSDavid Teigland  */
6b3b94faaSDavid Teigland 
71c2ea8a2SDavid Howells #include <linux/module.h>
8b3b94faaSDavid Teigland #include <linux/slab.h>
9b3b94faaSDavid Teigland #include <linux/spinlock.h>
10b3b94faaSDavid Teigland #include <linux/completion.h>
11b3b94faaSDavid Teigland #include <linux/buffer_head.h>
125c676f6dSSteven Whitehouse #include <linux/gfs2_ondisk.h>
1371b86f56SSteven Whitehouse #include <linux/crc32.h>
14c1696fb8SBob Peterson #include <linux/crc32c.h>
155e86d9d1SAbhi Das #include <linux/ktime.h>
16b3b94faaSDavid Teigland 
17b3b94faaSDavid Teigland #include "gfs2.h"
185c676f6dSSteven Whitehouse #include "incore.h"
19b3b94faaSDavid Teigland #include "bmap.h"
20b3b94faaSDavid Teigland #include "glock.h"
21b3b94faaSDavid Teigland #include "glops.h"
22588bff95SBob Peterson #include "log.h"
23b3b94faaSDavid Teigland #include "lops.h"
24b3b94faaSDavid Teigland #include "meta_io.h"
25b3b94faaSDavid Teigland #include "recovery.h"
26b3b94faaSDavid Teigland #include "super.h"
275c676f6dSSteven Whitehouse #include "util.h"
2871b86f56SSteven Whitehouse #include "dir.h"
29b3b94faaSDavid Teigland 
306ecd7c2dSTejun Heo struct workqueue_struct *gfs_recovery_wq;
316ecd7c2dSTejun Heo 
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 
507276b3b0SSteven Whitehouse 	*bh = gfs2_meta_ra(gl, dblock, extlen);
51b3b94faaSDavid Teigland 
52b3b94faaSDavid Teigland 	return error;
53b3b94faaSDavid Teigland }
54b3b94faaSDavid Teigland 
55a17d758bSBob Peterson int gfs2_revoke_add(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
56b3b94faaSDavid Teigland {
57a17d758bSBob Peterson 	struct list_head *head = &jd->jd_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 
7316c5f06fSJosef Bacik 	rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_NOFS);
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 
84a17d758bSBob Peterson int gfs2_revoke_check(struct gfs2_jdesc *jd, 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 
90a17d758bSBob Peterson 	list_for_each_entry(rr, &jd->jd_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 
100a17d758bSBob Peterson 	wrap = (rr->rr_where < jd->jd_replay_tail);
101a17d758bSBob Peterson 	a = (jd->jd_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 
108a17d758bSBob Peterson void gfs2_revoke_clean(struct gfs2_jdesc *jd)
109b3b94faaSDavid Teigland {
110a17d758bSBob Peterson 	struct list_head *head = &jd->jd_revoke_list;
111b3b94faaSDavid Teigland 	struct gfs2_revoke_replay *rr;
112b3b94faaSDavid Teigland 
113b3b94faaSDavid Teigland 	while (!list_empty(head)) {
114*969183bcSAndreas Gruenbacher 		rr = list_first_entry(head, struct gfs2_revoke_replay, rr_list);
115b3b94faaSDavid Teigland 		list_del(&rr->rr_list);
116b3b94faaSDavid Teigland 		kfree(rr);
117b3b94faaSDavid Teigland 	}
118b3b94faaSDavid Teigland }
119b3b94faaSDavid Teigland 
12040e0e61eSAbhi Das int __get_log_header(struct gfs2_sbd *sdp, const struct gfs2_log_header *lh,
12140e0e61eSAbhi Das 		     unsigned int blkno, struct gfs2_log_header_host *head)
12240e0e61eSAbhi Das {
12340e0e61eSAbhi Das 	u32 hash, crc;
12440e0e61eSAbhi Das 
12540e0e61eSAbhi Das 	if (lh->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
12640e0e61eSAbhi Das 	    lh->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH) ||
12740e0e61eSAbhi Das 	    (blkno && be32_to_cpu(lh->lh_blkno) != blkno))
12840e0e61eSAbhi Das 		return 1;
12940e0e61eSAbhi Das 
13040e0e61eSAbhi Das 	hash = crc32(~0, lh, LH_V1_SIZE - 4);
13140e0e61eSAbhi Das 	hash = ~crc32_le_shift(hash, 4); /* assume lh_hash is zero */
13240e0e61eSAbhi Das 
13340e0e61eSAbhi Das 	if (be32_to_cpu(lh->lh_hash) != hash)
13440e0e61eSAbhi Das 		return 1;
13540e0e61eSAbhi Das 
13640e0e61eSAbhi Das 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
13740e0e61eSAbhi Das 		     sdp->sd_sb.sb_bsize - LH_V1_SIZE - 4);
13840e0e61eSAbhi Das 
13940e0e61eSAbhi Das 	if ((lh->lh_crc != 0 && be32_to_cpu(lh->lh_crc) != crc))
14040e0e61eSAbhi Das 		return 1;
14140e0e61eSAbhi Das 
14240e0e61eSAbhi Das 	head->lh_sequence = be64_to_cpu(lh->lh_sequence);
14340e0e61eSAbhi Das 	head->lh_flags = be32_to_cpu(lh->lh_flags);
14440e0e61eSAbhi Das 	head->lh_tail = be32_to_cpu(lh->lh_tail);
14540e0e61eSAbhi Das 	head->lh_blkno = be32_to_cpu(lh->lh_blkno);
14640e0e61eSAbhi Das 
14740e0e61eSAbhi Das 	return 0;
14840e0e61eSAbhi Das }
149b3b94faaSDavid Teigland /**
150b3b94faaSDavid Teigland  * get_log_header - read the log header for a given segment
151b3b94faaSDavid Teigland  * @jd: the journal
152b3b94faaSDavid Teigland  * @blk: the block to look at
153b3b94faaSDavid Teigland  * @lh: the log header to return
154b3b94faaSDavid Teigland  *
155b3b94faaSDavid Teigland  * Read the log header for a given segement in a given journal.  Do a few
156b3b94faaSDavid Teigland  * sanity checks on it.
157b3b94faaSDavid Teigland  *
158b3b94faaSDavid Teigland  * Returns: 0 on success,
159b3b94faaSDavid Teigland  *          1 if the header was invalid or incomplete,
160b3b94faaSDavid Teigland  *          errno on error
161b3b94faaSDavid Teigland  */
162b3b94faaSDavid Teigland 
163b3b94faaSDavid Teigland static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
16455167622SAl Viro 			  struct gfs2_log_header_host *head)
165b3b94faaSDavid Teigland {
16640e0e61eSAbhi Das 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
167b3b94faaSDavid Teigland 	struct buffer_head *bh;
168b3b94faaSDavid Teigland 	int error;
169b3b94faaSDavid Teigland 
170b3b94faaSDavid Teigland 	error = gfs2_replay_read_block(jd, blk, &bh);
171b3b94faaSDavid Teigland 	if (error)
172b3b94faaSDavid Teigland 		return error;
173b3b94faaSDavid Teigland 
17440e0e61eSAbhi Das 	error = __get_log_header(sdp, (const struct gfs2_log_header *)bh->b_data,
17540e0e61eSAbhi Das 				 blk, head);
176b3b94faaSDavid Teigland 	brelse(bh);
177b3b94faaSDavid Teigland 
1780ff5916aSAndreas Gruenbacher 	return error;
179b3b94faaSDavid Teigland }
180b3b94faaSDavid Teigland 
181b3b94faaSDavid Teigland /**
182b3b94faaSDavid Teigland  * foreach_descriptor - go through the active part of the log
183b3b94faaSDavid Teigland  * @jd: the journal
184b3b94faaSDavid Teigland  * @start: the first log header in the active region
185b3b94faaSDavid Teigland  * @end: the last log header (don't process the contents of this entry))
186b3b94faaSDavid Teigland  *
187b3b94faaSDavid Teigland  * Call a given function once for every log descriptor in the active
188b3b94faaSDavid Teigland  * portion of the log.
189b3b94faaSDavid Teigland  *
190b3b94faaSDavid Teigland  * Returns: errno
191b3b94faaSDavid Teigland  */
192b3b94faaSDavid Teigland 
1937c70b896SBob Peterson static int foreach_descriptor(struct gfs2_jdesc *jd, u32 start,
194b3b94faaSDavid Teigland 			      unsigned int end, int pass)
195b3b94faaSDavid Teigland {
196feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
197b3b94faaSDavid Teigland 	struct buffer_head *bh;
198b3b94faaSDavid Teigland 	struct gfs2_log_descriptor *ld;
199b3b94faaSDavid Teigland 	int error = 0;
200b3b94faaSDavid Teigland 	u32 length;
201b3b94faaSDavid Teigland 	__be64 *ptr;
202b3b94faaSDavid Teigland 	unsigned int offset = sizeof(struct gfs2_log_descriptor);
203a67cdbd4SSteven Whitehouse 	offset += sizeof(__be64) - 1;
204b3b94faaSDavid Teigland 	offset &= ~(sizeof(__be64) - 1);
205b3b94faaSDavid Teigland 
206b3b94faaSDavid Teigland 	while (start != end) {
207b3b94faaSDavid Teigland 		error = gfs2_replay_read_block(jd, start, &bh);
208b3b94faaSDavid Teigland 		if (error)
209b3b94faaSDavid Teigland 			return error;
210b3b94faaSDavid Teigland 		if (gfs2_meta_check(sdp, bh)) {
211b3b94faaSDavid Teigland 			brelse(bh);
212b3b94faaSDavid Teigland 			return -EIO;
213b3b94faaSDavid Teigland 		}
214b3b94faaSDavid Teigland 		ld = (struct gfs2_log_descriptor *)bh->b_data;
215b3b94faaSDavid Teigland 		length = be32_to_cpu(ld->ld_length);
216b3b94faaSDavid Teigland 
217e3167dedSSteven Whitehouse 		if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
21855167622SAl Viro 			struct gfs2_log_header_host lh;
219b3b94faaSDavid Teigland 			error = get_log_header(jd, start, &lh);
220b3b94faaSDavid Teigland 			if (!error) {
221e1cb6be9SBob Peterson 				gfs2_replay_incr_blk(jd, &start);
22288721877SRussell Cattelan 				brelse(bh);
223b3b94faaSDavid Teigland 				continue;
224b3b94faaSDavid Teigland 			}
225b3b94faaSDavid Teigland 			if (error == 1) {
226feaa7bbaSSteven Whitehouse 				gfs2_consist_inode(GFS2_I(jd->jd_inode));
227b3b94faaSDavid Teigland 				error = -EIO;
228b3b94faaSDavid Teigland 			}
229b3b94faaSDavid Teigland 			brelse(bh);
230b3b94faaSDavid Teigland 			return error;
231b3b94faaSDavid Teigland 		} else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
232b3b94faaSDavid Teigland 			brelse(bh);
233b3b94faaSDavid Teigland 			return -EIO;
234b3b94faaSDavid Teigland 		}
235b3b94faaSDavid Teigland 		ptr = (__be64 *)(bh->b_data + offset);
236b3b94faaSDavid Teigland 		error = lops_scan_elements(jd, start, ld, ptr, pass);
237b3b94faaSDavid Teigland 		if (error) {
238b3b94faaSDavid Teigland 			brelse(bh);
239b3b94faaSDavid Teigland 			return error;
240b3b94faaSDavid Teigland 		}
241b3b94faaSDavid Teigland 
242b3b94faaSDavid Teigland 		while (length--)
243e1cb6be9SBob Peterson 			gfs2_replay_incr_blk(jd, &start);
244b3b94faaSDavid Teigland 
245b3b94faaSDavid Teigland 		brelse(bh);
246b3b94faaSDavid Teigland 	}
247b3b94faaSDavid Teigland 
248b3b94faaSDavid Teigland 	return 0;
249b3b94faaSDavid Teigland }
250b3b94faaSDavid Teigland 
251b3b94faaSDavid Teigland /**
252b3b94faaSDavid Teigland  * clean_journal - mark a dirty journal as being clean
253b3b94faaSDavid Teigland  * @jd: the journal
254b3b94faaSDavid Teigland  * @head: the head journal to start from
255b3b94faaSDavid Teigland  *
256b3b94faaSDavid Teigland  * Returns: errno
257b3b94faaSDavid Teigland  */
258b3b94faaSDavid Teigland 
259588bff95SBob Peterson static void clean_journal(struct gfs2_jdesc *jd,
260588bff95SBob Peterson 			  struct gfs2_log_header_host *head)
261b3b94faaSDavid Teigland {
262feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
2637c70b896SBob Peterson 	u32 lblock = head->lh_blkno;
264b3b94faaSDavid Teigland 
2657c70b896SBob Peterson 	gfs2_replay_incr_blk(jd, &lblock);
2667c70b896SBob Peterson 	gfs2_write_log_header(sdp, jd, head->lh_sequence + 1, 0, lblock,
267c1696fb8SBob Peterson 			      GFS2_LOG_HEAD_UNMOUNT | GFS2_LOG_HEAD_RECOVERY,
268c1696fb8SBob Peterson 			      REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC);
26919ebc050SAndreas Gruenbacher 	if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
27019ebc050SAndreas Gruenbacher 		sdp->sd_log_flush_head = lblock;
27119ebc050SAndreas Gruenbacher 		gfs2_log_incr_head(sdp);
27219ebc050SAndreas Gruenbacher 	}
273b3b94faaSDavid Teigland }
274b3b94faaSDavid Teigland 
275da755fdbSSteven Whitehouse 
276f057f6cdSSteven Whitehouse static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
277da755fdbSSteven Whitehouse                                unsigned int message)
278da755fdbSSteven Whitehouse {
279f057f6cdSSteven Whitehouse 	char env_jid[20];
280f057f6cdSSteven Whitehouse 	char env_status[20];
281f057f6cdSSteven Whitehouse 	char *envp[] = { env_jid, env_status, NULL };
282f057f6cdSSteven Whitehouse 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
283e0c2a9aaSDavid Teigland 
284f057f6cdSSteven Whitehouse         ls->ls_recover_jid_done = jid;
285f057f6cdSSteven Whitehouse         ls->ls_recover_jid_status = message;
2863566c964Salex chen 	sprintf(env_jid, "JID=%u", jid);
287f057f6cdSSteven Whitehouse 	sprintf(env_status, "RECOVERY=%s",
288f057f6cdSSteven Whitehouse 		message == LM_RD_SUCCESS ? "Done" : "Failed");
289f057f6cdSSteven Whitehouse         kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
290e0c2a9aaSDavid Teigland 
291e0c2a9aaSDavid Teigland 	if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
292e0c2a9aaSDavid Teigland 		sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
293da755fdbSSteven Whitehouse }
294da755fdbSSteven Whitehouse 
2956ecd7c2dSTejun Heo void gfs2_recover_func(struct work_struct *work)
296fe64d517SSteven Whitehouse {
297fe64d517SSteven Whitehouse 	struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
298feaa7bbaSSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
299feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
30055167622SAl Viro 	struct gfs2_log_header_host head;
30124972557SBenjamin Marzinski 	struct gfs2_holder j_gh, ji_gh, thaw_gh;
3025e86d9d1SAbhi Das 	ktime_t t_start, t_jlck, t_jhd, t_tlck, t_rep;
303b3b94faaSDavid Teigland 	int ro = 0;
304b3b94faaSDavid Teigland 	unsigned int pass;
3054a772772SBob Peterson 	int error = 0;
306c741c455SSteven Whitehouse 	int jlocked = 0;
307b3b94faaSDavid Teigland 
30803678a99SBob Peterson 	if (gfs2_withdrawn(sdp)) {
30903678a99SBob Peterson 		fs_err(sdp, "jid=%u: Recovery not attempted due to withdraw.\n",
31003678a99SBob Peterson 		       jd->jd_jid);
31103678a99SBob Peterson 		goto fail;
31203678a99SBob Peterson 	}
3135e86d9d1SAbhi Das 	t_start = ktime_get();
3144a772772SBob Peterson 	if (sdp->sd_args.ar_spectator)
3154a772772SBob Peterson 		goto fail;
3164a772772SBob Peterson 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
3175965b1f4SSteven Whitehouse 		fs_info(sdp, "jid=%u: Trying to acquire journal lock...\n",
3185965b1f4SSteven Whitehouse 			jd->jd_jid);
319c741c455SSteven Whitehouse 		jlocked = 1;
320c78bad11SJoe Perches 		/* Acquire the journal lock so we can do recovery */
321b3b94faaSDavid Teigland 
322c63e31c2SDavid Teigland 		error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
323b3b94faaSDavid Teigland 					  LM_ST_EXCLUSIVE,
324c63e31c2SDavid Teigland 					  LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
325c63e31c2SDavid Teigland 					  &j_gh);
326b3b94faaSDavid Teigland 		switch (error) {
327b3b94faaSDavid Teigland 		case 0:
328b3b94faaSDavid Teigland 			break;
329b3b94faaSDavid Teigland 
330b3b94faaSDavid Teigland 		case GLR_TRYFAILED:
331b3b94faaSDavid Teigland 			fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
332b3b94faaSDavid Teigland 			error = 0;
333b3b94faaSDavid Teigland 
334b3b94faaSDavid Teigland 		default:
335b3b94faaSDavid Teigland 			goto fail;
336098b9c14SAliasgar Surti 		}
337b3b94faaSDavid Teigland 
3385c676f6dSSteven Whitehouse 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
33975be73a8SBob Peterson 					   LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
340b3b94faaSDavid Teigland 		if (error)
341b3b94faaSDavid Teigland 			goto fail_gunlock_j;
3425965b1f4SSteven Whitehouse 	} else {
3435965b1f4SSteven Whitehouse 		fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
3445965b1f4SSteven Whitehouse 	}
345b3b94faaSDavid Teigland 
3465e86d9d1SAbhi Das 	t_jlck = ktime_get();
347b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
348b3b94faaSDavid Teigland 
349b3b94faaSDavid Teigland 	error = gfs2_jdesc_check(jd);
350b3b94faaSDavid Teigland 	if (error)
351b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
352b3b94faaSDavid Teigland 
353f4686c26SAbhi Das 	error = gfs2_find_jhead(jd, &head, true);
354b3b94faaSDavid Teigland 	if (error)
355b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
3565e86d9d1SAbhi Das 	t_jhd = ktime_get();
35798583b3eSAbhi Das 	fs_info(sdp, "jid=%u: Journal head lookup took %lldms\n", jd->jd_jid,
35898583b3eSAbhi Das 		ktime_ms_delta(t_jhd, t_jlck));
359b3b94faaSDavid Teigland 
360b3b94faaSDavid Teigland 	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
361b3b94faaSDavid Teigland 		fs_info(sdp, "jid=%u: Acquiring the transaction lock...\n",
362b3b94faaSDavid Teigland 			jd->jd_jid);
363b3b94faaSDavid Teigland 
36424972557SBenjamin Marzinski 		/* Acquire a shared hold on the freeze lock */
365b3b94faaSDavid Teigland 
36624972557SBenjamin Marzinski 		error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
36724972557SBenjamin Marzinski 					   LM_FLAG_NOEXP | LM_FLAG_PRIORITY,
36824972557SBenjamin Marzinski 					   &thaw_gh);
369b3b94faaSDavid Teigland 		if (error)
370b3b94faaSDavid Teigland 			goto fail_gunlock_ji;
371b3b94faaSDavid Teigland 
372e8ca5cc5SDavid Teigland 		if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
373e8ca5cc5SDavid Teigland 			ro = 1;
374e8ca5cc5SDavid Teigland 		} else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
375b3b94faaSDavid Teigland 			if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
376b3b94faaSDavid Teigland 				ro = 1;
377b3b94faaSDavid Teigland 		} else {
378bc98a42cSDavid Howells 			if (sb_rdonly(sdp->sd_vfs)) {
3797bc5c414SAbhijith Das 				/* check if device itself is read-only */
3807bc5c414SAbhijith Das 				ro = bdev_read_only(sdp->sd_vfs->s_bdev);
3817bc5c414SAbhijith Das 				if (!ro) {
3827bc5c414SAbhijith Das 					fs_info(sdp, "recovery required on "
3837bc5c414SAbhijith Das 						"read-only filesystem.\n");
3847bc5c414SAbhijith Das 					fs_info(sdp, "write access will be "
3857bc5c414SAbhijith Das 						"enabled during recovery.\n");
3867bc5c414SAbhijith Das 				}
3877bc5c414SAbhijith Das 			}
388b3b94faaSDavid Teigland 		}
389b3b94faaSDavid Teigland 
390b3b94faaSDavid Teigland 		if (ro) {
3917bc5c414SAbhijith Das 			fs_warn(sdp, "jid=%u: Can't replay: read-only block "
3927bc5c414SAbhijith Das 				"device\n", jd->jd_jid);
393b3b94faaSDavid Teigland 			error = -EROFS;
39424972557SBenjamin Marzinski 			goto fail_gunlock_thaw;
395b3b94faaSDavid Teigland 		}
396b3b94faaSDavid Teigland 
3975e86d9d1SAbhi Das 		t_tlck = ktime_get();
39849eb776eSBob Peterson 		fs_info(sdp, "jid=%u: Replaying journal...0x%x to 0x%x\n",
39949eb776eSBob Peterson 			jd->jd_jid, head.lh_tail, head.lh_blkno);
400b3b94faaSDavid Teigland 
401c9ebc4b7SBob Peterson 		/* We take the sd_log_flush_lock here primarily to prevent log
402c9ebc4b7SBob Peterson 		 * flushes and simultaneous journal replays from stomping on
403c9ebc4b7SBob Peterson 		 * each other wrt sd_log_bio. */
404c9ebc4b7SBob Peterson 		down_write(&sdp->sd_log_flush_lock);
405b3b94faaSDavid Teigland 		for (pass = 0; pass < 2; pass++) {
406b3b94faaSDavid Teigland 			lops_before_scan(jd, &head, pass);
407b3b94faaSDavid Teigland 			error = foreach_descriptor(jd, head.lh_tail,
408b3b94faaSDavid Teigland 						   head.lh_blkno, pass);
409b3b94faaSDavid Teigland 			lops_after_scan(jd, error, pass);
410b3b94faaSDavid Teigland 			if (error)
41124972557SBenjamin Marzinski 				goto fail_gunlock_thaw;
412b3b94faaSDavid Teigland 		}
413b3b94faaSDavid Teigland 
414588bff95SBob Peterson 		clean_journal(jd, &head);
415c9ebc4b7SBob Peterson 		up_write(&sdp->sd_log_flush_lock);
416b3b94faaSDavid Teigland 
41724972557SBenjamin Marzinski 		gfs2_glock_dq_uninit(&thaw_gh);
4185e86d9d1SAbhi Das 		t_rep = ktime_get();
4195e86d9d1SAbhi Das 		fs_info(sdp, "jid=%u: Journal replayed in %lldms [jlck:%lldms, "
4205e86d9d1SAbhi Das 			"jhead:%lldms, tlck:%lldms, replay:%lldms]\n",
4215e86d9d1SAbhi Das 			jd->jd_jid, ktime_ms_delta(t_rep, t_start),
4225e86d9d1SAbhi Das 			ktime_ms_delta(t_jlck, t_start),
4235e86d9d1SAbhi Das 			ktime_ms_delta(t_jhd, t_jlck),
4245e86d9d1SAbhi Das 			ktime_ms_delta(t_tlck, t_jhd),
4255e86d9d1SAbhi Das 			ktime_ms_delta(t_rep, t_tlck));
426b3b94faaSDavid Teigland 	}
427b3b94faaSDavid Teigland 
428f057f6cdSSteven Whitehouse 	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
429b3b94faaSDavid Teigland 
430c741c455SSteven Whitehouse 	if (jlocked) {
431c741c455SSteven Whitehouse 		gfs2_glock_dq_uninit(&ji_gh);
432b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
433c741c455SSteven Whitehouse 	}
434b3b94faaSDavid Teigland 
435b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
4366ecd7c2dSTejun Heo 	goto done;
437b3b94faaSDavid Teigland 
43824972557SBenjamin Marzinski fail_gunlock_thaw:
43924972557SBenjamin Marzinski 	gfs2_glock_dq_uninit(&thaw_gh);
440b3b94faaSDavid Teigland fail_gunlock_ji:
441c741c455SSteven Whitehouse 	if (jlocked) {
442b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&ji_gh);
443b3b94faaSDavid Teigland fail_gunlock_j:
444b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
4455965b1f4SSteven Whitehouse 	}
446b3b94faaSDavid Teigland 
447b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
448b3b94faaSDavid Teigland fail:
449376d3778SDavid Teigland 	jd->jd_recover_error = error;
450f057f6cdSSteven Whitehouse 	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
4516ecd7c2dSTejun Heo done:
4526ecd7c2dSTejun Heo 	clear_bit(JDF_RECOVERY, &jd->jd_flags);
4534e857c58SPeter Zijlstra 	smp_mb__after_atomic();
4546ecd7c2dSTejun Heo 	wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
455b3b94faaSDavid Teigland }
456b3b94faaSDavid Teigland 
4576ecd7c2dSTejun Heo int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
458b3b94faaSDavid Teigland {
459fe64d517SSteven Whitehouse 	int rv;
4606ecd7c2dSTejun Heo 
4616ecd7c2dSTejun Heo 	if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
4626ecd7c2dSTejun Heo 		return -EBUSY;
4636ecd7c2dSTejun Heo 
4646ecd7c2dSTejun Heo 	/* we have JDF_RECOVERY, queue should always succeed */
4656ecd7c2dSTejun Heo 	rv = queue_work(gfs_recovery_wq, &jd->jd_work);
4666ecd7c2dSTejun Heo 	BUG_ON(!rv);
4676ecd7c2dSTejun Heo 
4686ecd7c2dSTejun Heo 	if (wait)
46974316201SNeilBrown 		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
4706ecd7c2dSTejun Heo 			    TASK_UNINTERRUPTIBLE);
4716ecd7c2dSTejun Heo 
472376d3778SDavid Teigland 	return wait ? jd->jd_recover_error : 0;
4739ac1b4d9SSteven Whitehouse }
4749ac1b4d9SSteven Whitehouse 
475