xref: /openbmc/linux/fs/gfs2/recovery.c (revision b181f7029bd71238ac2754ce7052dffd69432085)
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 
305c0dc371SAndreas Gruenbacher struct workqueue_struct *gfs2_recovery_wq;
316ecd7c2dSTejun Heo 
gfs2_replay_read_block(struct gfs2_jdesc * jd,unsigned int blk,struct buffer_head ** bh)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;
37cd915493SSteven Whitehouse 	u64 dblock;
38cd915493SSteven Whitehouse 	u32 extlen;
39b3b94faaSDavid Teigland 	int error;
40b3b94faaSDavid Teigland 
419153dac1SAndreas Gruenbacher 	extlen = 32;
429153dac1SAndreas Gruenbacher 	error = gfs2_get_extent(&ip->i_inode, blk, &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 
gfs2_revoke_add(struct gfs2_jdesc * jd,u64 blkno,unsigned int where)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;
58b846f2d7SJakob Koschel 	struct gfs2_revoke_replay *rr = NULL, *iter;
59b3b94faaSDavid Teigland 
60b846f2d7SJakob Koschel 	list_for_each_entry(iter, head, rr_list) {
61b846f2d7SJakob Koschel 		if (iter->rr_blkno == blkno) {
62b846f2d7SJakob Koschel 			rr = iter;
63b3b94faaSDavid Teigland 			break;
64b3b94faaSDavid Teigland 		}
65b3b94faaSDavid Teigland 	}
66b3b94faaSDavid Teigland 
67b846f2d7SJakob Koschel 	if (rr) {
68b3b94faaSDavid Teigland 		rr->rr_where = where;
69b3b94faaSDavid Teigland 		return 0;
70b3b94faaSDavid Teigland 	}
71b3b94faaSDavid Teigland 
7216c5f06fSJosef Bacik 	rr = kmalloc(sizeof(struct gfs2_revoke_replay), GFP_NOFS);
73b3b94faaSDavid Teigland 	if (!rr)
74b3b94faaSDavid Teigland 		return -ENOMEM;
75b3b94faaSDavid Teigland 
76b3b94faaSDavid Teigland 	rr->rr_blkno = blkno;
77b3b94faaSDavid Teigland 	rr->rr_where = where;
78b3b94faaSDavid Teigland 	list_add(&rr->rr_list, head);
79b3b94faaSDavid Teigland 
80b3b94faaSDavid Teigland 	return 1;
81b3b94faaSDavid Teigland }
82b3b94faaSDavid Teigland 
gfs2_revoke_check(struct gfs2_jdesc * jd,u64 blkno,unsigned int where)83a17d758bSBob Peterson int gfs2_revoke_check(struct gfs2_jdesc *jd, u64 blkno, unsigned int where)
84b3b94faaSDavid Teigland {
85b846f2d7SJakob Koschel 	struct gfs2_revoke_replay *rr = NULL, *iter;
86b3b94faaSDavid Teigland 	int wrap, a, b, revoke;
87b3b94faaSDavid Teigland 
88b846f2d7SJakob Koschel 	list_for_each_entry(iter, &jd->jd_revoke_list, rr_list) {
89b846f2d7SJakob Koschel 		if (iter->rr_blkno == blkno) {
90b846f2d7SJakob Koschel 			rr = iter;
91b3b94faaSDavid Teigland 			break;
92b3b94faaSDavid Teigland 		}
93b3b94faaSDavid Teigland 	}
94b3b94faaSDavid Teigland 
95b846f2d7SJakob Koschel 	if (!rr)
96b3b94faaSDavid Teigland 		return 0;
97b3b94faaSDavid Teigland 
98a17d758bSBob Peterson 	wrap = (rr->rr_where < jd->jd_replay_tail);
99a17d758bSBob Peterson 	a = (jd->jd_replay_tail < where);
100b3b94faaSDavid Teigland 	b = (where < rr->rr_where);
101b3b94faaSDavid Teigland 	revoke = (wrap) ? (a || b) : (a && b);
102b3b94faaSDavid Teigland 
103b3b94faaSDavid Teigland 	return revoke;
104b3b94faaSDavid Teigland }
105b3b94faaSDavid Teigland 
gfs2_revoke_clean(struct gfs2_jdesc * jd)106a17d758bSBob Peterson void gfs2_revoke_clean(struct gfs2_jdesc *jd)
107b3b94faaSDavid Teigland {
108a17d758bSBob Peterson 	struct list_head *head = &jd->jd_revoke_list;
109b3b94faaSDavid Teigland 	struct gfs2_revoke_replay *rr;
110b3b94faaSDavid Teigland 
111b3b94faaSDavid Teigland 	while (!list_empty(head)) {
112969183bcSAndreas Gruenbacher 		rr = list_first_entry(head, struct gfs2_revoke_replay, rr_list);
113b3b94faaSDavid Teigland 		list_del(&rr->rr_list);
114b3b94faaSDavid Teigland 		kfree(rr);
115b3b94faaSDavid Teigland 	}
116b3b94faaSDavid Teigland }
117b3b94faaSDavid Teigland 
__get_log_header(struct gfs2_sbd * sdp,const struct gfs2_log_header * lh,unsigned int blkno,struct gfs2_log_header_host * head)11840e0e61eSAbhi Das int __get_log_header(struct gfs2_sbd *sdp, const struct gfs2_log_header *lh,
11940e0e61eSAbhi Das 		     unsigned int blkno, struct gfs2_log_header_host *head)
12040e0e61eSAbhi Das {
12140e0e61eSAbhi Das 	u32 hash, crc;
12240e0e61eSAbhi Das 
12340e0e61eSAbhi Das 	if (lh->lh_header.mh_magic != cpu_to_be32(GFS2_MAGIC) ||
12440e0e61eSAbhi Das 	    lh->lh_header.mh_type != cpu_to_be32(GFS2_METATYPE_LH) ||
12540e0e61eSAbhi Das 	    (blkno && be32_to_cpu(lh->lh_blkno) != blkno))
12640e0e61eSAbhi Das 		return 1;
12740e0e61eSAbhi Das 
12840e0e61eSAbhi Das 	hash = crc32(~0, lh, LH_V1_SIZE - 4);
12940e0e61eSAbhi Das 	hash = ~crc32_le_shift(hash, 4); /* assume lh_hash is zero */
13040e0e61eSAbhi Das 
13140e0e61eSAbhi Das 	if (be32_to_cpu(lh->lh_hash) != hash)
13240e0e61eSAbhi Das 		return 1;
13340e0e61eSAbhi Das 
13440e0e61eSAbhi Das 	crc = crc32c(~0, (void *)lh + LH_V1_SIZE + 4,
13540e0e61eSAbhi Das 		     sdp->sd_sb.sb_bsize - LH_V1_SIZE - 4);
13640e0e61eSAbhi Das 
13740e0e61eSAbhi Das 	if ((lh->lh_crc != 0 && be32_to_cpu(lh->lh_crc) != crc))
13840e0e61eSAbhi Das 		return 1;
13940e0e61eSAbhi Das 
14040e0e61eSAbhi Das 	head->lh_sequence = be64_to_cpu(lh->lh_sequence);
14140e0e61eSAbhi Das 	head->lh_flags = be32_to_cpu(lh->lh_flags);
14240e0e61eSAbhi Das 	head->lh_tail = be32_to_cpu(lh->lh_tail);
14340e0e61eSAbhi Das 	head->lh_blkno = be32_to_cpu(lh->lh_blkno);
14440e0e61eSAbhi Das 
14573092698SAbhi Das 	head->lh_local_total = be64_to_cpu(lh->lh_local_total);
14673092698SAbhi Das 	head->lh_local_free = be64_to_cpu(lh->lh_local_free);
14773092698SAbhi Das 	head->lh_local_dinodes = be64_to_cpu(lh->lh_local_dinodes);
14873092698SAbhi Das 
14940e0e61eSAbhi Das 	return 0;
15040e0e61eSAbhi Das }
151b3b94faaSDavid Teigland /**
152b3b94faaSDavid Teigland  * get_log_header - read the log header for a given segment
153b3b94faaSDavid Teigland  * @jd: the journal
154b3b94faaSDavid Teigland  * @blk: the block to look at
155c551f66cSLee Jones  * @head: the log header to return
156b3b94faaSDavid Teigland  *
157b3b94faaSDavid Teigland  * Read the log header for a given segement in a given journal.  Do a few
158b3b94faaSDavid Teigland  * sanity checks on it.
159b3b94faaSDavid Teigland  *
160b3b94faaSDavid Teigland  * Returns: 0 on success,
161b3b94faaSDavid Teigland  *          1 if the header was invalid or incomplete,
162b3b94faaSDavid Teigland  *          errno on error
163b3b94faaSDavid Teigland  */
164b3b94faaSDavid Teigland 
get_log_header(struct gfs2_jdesc * jd,unsigned int blk,struct gfs2_log_header_host * head)165b3b94faaSDavid Teigland static int get_log_header(struct gfs2_jdesc *jd, unsigned int blk,
16655167622SAl Viro 			  struct gfs2_log_header_host *head)
167b3b94faaSDavid Teigland {
16840e0e61eSAbhi Das 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
169b3b94faaSDavid Teigland 	struct buffer_head *bh;
170b3b94faaSDavid Teigland 	int error;
171b3b94faaSDavid Teigland 
172b3b94faaSDavid Teigland 	error = gfs2_replay_read_block(jd, blk, &bh);
173b3b94faaSDavid Teigland 	if (error)
174b3b94faaSDavid Teigland 		return error;
175b3b94faaSDavid Teigland 
17640e0e61eSAbhi Das 	error = __get_log_header(sdp, (const struct gfs2_log_header *)bh->b_data,
17740e0e61eSAbhi Das 				 blk, head);
178b3b94faaSDavid Teigland 	brelse(bh);
179b3b94faaSDavid Teigland 
1800ff5916aSAndreas Gruenbacher 	return error;
181b3b94faaSDavid Teigland }
182b3b94faaSDavid Teigland 
183b3b94faaSDavid Teigland /**
184b3b94faaSDavid Teigland  * foreach_descriptor - go through the active part of the log
185b3b94faaSDavid Teigland  * @jd: the journal
186b3b94faaSDavid Teigland  * @start: the first log header in the active region
187b3b94faaSDavid Teigland  * @end: the last log header (don't process the contents of this entry))
188c551f66cSLee Jones  * @pass: iteration number (foreach_descriptor() is called in a for() loop)
189b3b94faaSDavid Teigland  *
190b3b94faaSDavid Teigland  * Call a given function once for every log descriptor in the active
191b3b94faaSDavid Teigland  * portion of the log.
192b3b94faaSDavid Teigland  *
193b3b94faaSDavid Teigland  * Returns: errno
194b3b94faaSDavid Teigland  */
195b3b94faaSDavid Teigland 
foreach_descriptor(struct gfs2_jdesc * jd,u32 start,unsigned int end,int pass)1967c70b896SBob Peterson static int foreach_descriptor(struct gfs2_jdesc *jd, u32 start,
197b3b94faaSDavid Teigland 			      unsigned int end, int pass)
198b3b94faaSDavid Teigland {
199feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
200b3b94faaSDavid Teigland 	struct buffer_head *bh;
201b3b94faaSDavid Teigland 	struct gfs2_log_descriptor *ld;
202b3b94faaSDavid Teigland 	int error = 0;
203b3b94faaSDavid Teigland 	u32 length;
204b3b94faaSDavid Teigland 	__be64 *ptr;
205b3b94faaSDavid Teigland 	unsigned int offset = sizeof(struct gfs2_log_descriptor);
206a67cdbd4SSteven Whitehouse 	offset += sizeof(__be64) - 1;
207b3b94faaSDavid Teigland 	offset &= ~(sizeof(__be64) - 1);
208b3b94faaSDavid Teigland 
209b3b94faaSDavid Teigland 	while (start != end) {
210b3b94faaSDavid Teigland 		error = gfs2_replay_read_block(jd, start, &bh);
211b3b94faaSDavid Teigland 		if (error)
212b3b94faaSDavid Teigland 			return error;
213b3b94faaSDavid Teigland 		if (gfs2_meta_check(sdp, bh)) {
214b3b94faaSDavid Teigland 			brelse(bh);
215b3b94faaSDavid Teigland 			return -EIO;
216b3b94faaSDavid Teigland 		}
217b3b94faaSDavid Teigland 		ld = (struct gfs2_log_descriptor *)bh->b_data;
218b3b94faaSDavid Teigland 		length = be32_to_cpu(ld->ld_length);
219b3b94faaSDavid Teigland 
220e3167dedSSteven Whitehouse 		if (be32_to_cpu(ld->ld_header.mh_type) == GFS2_METATYPE_LH) {
22155167622SAl Viro 			struct gfs2_log_header_host lh;
222b3b94faaSDavid Teigland 			error = get_log_header(jd, start, &lh);
223b3b94faaSDavid Teigland 			if (!error) {
224e1cb6be9SBob Peterson 				gfs2_replay_incr_blk(jd, &start);
22588721877SRussell Cattelan 				brelse(bh);
226b3b94faaSDavid Teigland 				continue;
227b3b94faaSDavid Teigland 			}
228b3b94faaSDavid Teigland 			if (error == 1) {
229feaa7bbaSSteven Whitehouse 				gfs2_consist_inode(GFS2_I(jd->jd_inode));
230b3b94faaSDavid Teigland 				error = -EIO;
231b3b94faaSDavid Teigland 			}
232b3b94faaSDavid Teigland 			brelse(bh);
233b3b94faaSDavid Teigland 			return error;
234b3b94faaSDavid Teigland 		} else if (gfs2_metatype_check(sdp, bh, GFS2_METATYPE_LD)) {
235b3b94faaSDavid Teigland 			brelse(bh);
236b3b94faaSDavid Teigland 			return -EIO;
237b3b94faaSDavid Teigland 		}
238b3b94faaSDavid Teigland 		ptr = (__be64 *)(bh->b_data + offset);
239b3b94faaSDavid Teigland 		error = lops_scan_elements(jd, start, ld, ptr, pass);
240b3b94faaSDavid Teigland 		if (error) {
241b3b94faaSDavid Teigland 			brelse(bh);
242b3b94faaSDavid Teigland 			return error;
243b3b94faaSDavid Teigland 		}
244b3b94faaSDavid Teigland 
245b3b94faaSDavid Teigland 		while (length--)
246e1cb6be9SBob Peterson 			gfs2_replay_incr_blk(jd, &start);
247b3b94faaSDavid Teigland 
248b3b94faaSDavid Teigland 		brelse(bh);
249b3b94faaSDavid Teigland 	}
250b3b94faaSDavid Teigland 
251b3b94faaSDavid Teigland 	return 0;
252b3b94faaSDavid Teigland }
253b3b94faaSDavid Teigland 
254b3b94faaSDavid Teigland /**
255b3b94faaSDavid Teigland  * clean_journal - mark a dirty journal as being clean
256b3b94faaSDavid Teigland  * @jd: the journal
257b3b94faaSDavid Teigland  * @head: the head journal to start from
258b3b94faaSDavid Teigland  *
259b3b94faaSDavid Teigland  * Returns: errno
260b3b94faaSDavid Teigland  */
261b3b94faaSDavid Teigland 
clean_journal(struct gfs2_jdesc * jd,struct gfs2_log_header_host * head)262588bff95SBob Peterson static void clean_journal(struct gfs2_jdesc *jd,
263588bff95SBob Peterson 			  struct gfs2_log_header_host *head)
264b3b94faaSDavid Teigland {
265feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
2667c70b896SBob Peterson 	u32 lblock = head->lh_blkno;
267b3b94faaSDavid Teigland 
2687c70b896SBob Peterson 	gfs2_replay_incr_blk(jd, &lblock);
2697c70b896SBob Peterson 	gfs2_write_log_header(sdp, jd, head->lh_sequence + 1, 0, lblock,
270c1696fb8SBob Peterson 			      GFS2_LOG_HEAD_UNMOUNT | GFS2_LOG_HEAD_RECOVERY,
271c1696fb8SBob Peterson 			      REQ_PREFLUSH | REQ_FUA | REQ_META | REQ_SYNC);
27219ebc050SAndreas Gruenbacher 	if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
27319ebc050SAndreas Gruenbacher 		sdp->sd_log_flush_head = lblock;
27419ebc050SAndreas Gruenbacher 		gfs2_log_incr_head(sdp);
27519ebc050SAndreas Gruenbacher 	}
276b3b94faaSDavid Teigland }
277b3b94faaSDavid Teigland 
278da755fdbSSteven Whitehouse 
gfs2_recovery_done(struct gfs2_sbd * sdp,unsigned int jid,unsigned int message)279f057f6cdSSteven Whitehouse static void gfs2_recovery_done(struct gfs2_sbd *sdp, unsigned int jid,
280da755fdbSSteven Whitehouse                                unsigned int message)
281da755fdbSSteven Whitehouse {
282f057f6cdSSteven Whitehouse 	char env_jid[20];
283f057f6cdSSteven Whitehouse 	char env_status[20];
284f057f6cdSSteven Whitehouse 	char *envp[] = { env_jid, env_status, NULL };
285f057f6cdSSteven Whitehouse 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
286e0c2a9aaSDavid Teigland 
287f057f6cdSSteven Whitehouse         ls->ls_recover_jid_done = jid;
288f057f6cdSSteven Whitehouse         ls->ls_recover_jid_status = message;
2893566c964Salex chen 	sprintf(env_jid, "JID=%u", jid);
290f057f6cdSSteven Whitehouse 	sprintf(env_status, "RECOVERY=%s",
291f057f6cdSSteven Whitehouse 		message == LM_RD_SUCCESS ? "Done" : "Failed");
292f057f6cdSSteven Whitehouse         kobject_uevent_env(&sdp->sd_kobj, KOBJ_CHANGE, envp);
293e0c2a9aaSDavid Teigland 
294e0c2a9aaSDavid Teigland 	if (sdp->sd_lockstruct.ls_ops->lm_recovery_result)
295e0c2a9aaSDavid Teigland 		sdp->sd_lockstruct.ls_ops->lm_recovery_result(sdp, jid, message);
296da755fdbSSteven Whitehouse }
297da755fdbSSteven Whitehouse 
298bedb0f05SAbhi Das /**
299bedb0f05SAbhi Das  * update_statfs_inode - Update the master statfs inode or zero out the local
300bedb0f05SAbhi Das  *			 statfs inode for a given journal.
301bedb0f05SAbhi Das  * @jd: The journal
302bedb0f05SAbhi Das  * @head: If NULL, @inode is the local statfs inode and we need to zero it out.
303bedb0f05SAbhi Das  *	  Otherwise, it @head contains the statfs change info that needs to be
304bedb0f05SAbhi Das  *	  synced to the master statfs inode (pointed to by @inode).
305bedb0f05SAbhi Das  * @inode: statfs inode to update.
306bedb0f05SAbhi Das  */
update_statfs_inode(struct gfs2_jdesc * jd,struct gfs2_log_header_host * head,struct inode * inode)307bedb0f05SAbhi Das static int update_statfs_inode(struct gfs2_jdesc *jd,
308bedb0f05SAbhi Das 			       struct gfs2_log_header_host *head,
309bedb0f05SAbhi Das 			       struct inode *inode)
310bedb0f05SAbhi Das {
311bedb0f05SAbhi Das 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
312bedb0f05SAbhi Das 	struct gfs2_inode *ip;
313bedb0f05SAbhi Das 	struct buffer_head *bh;
314bedb0f05SAbhi Das 	struct gfs2_statfs_change_host sc;
315bedb0f05SAbhi Das 	int error = 0;
316bedb0f05SAbhi Das 
317bedb0f05SAbhi Das 	BUG_ON(!inode);
318bedb0f05SAbhi Das 	ip = GFS2_I(inode);
319bedb0f05SAbhi Das 
320bedb0f05SAbhi Das 	error = gfs2_meta_inode_buffer(ip, &bh);
321bedb0f05SAbhi Das 	if (error)
322bedb0f05SAbhi Das 		goto out;
323bedb0f05SAbhi Das 
324bedb0f05SAbhi Das 	spin_lock(&sdp->sd_statfs_spin);
325bedb0f05SAbhi Das 
326bedb0f05SAbhi Das 	if (head) { /* Update the master statfs inode */
327bedb0f05SAbhi Das 		gfs2_statfs_change_in(&sc, bh->b_data + sizeof(struct gfs2_dinode));
328bedb0f05SAbhi Das 		sc.sc_total += head->lh_local_total;
329bedb0f05SAbhi Das 		sc.sc_free += head->lh_local_free;
330bedb0f05SAbhi Das 		sc.sc_dinodes += head->lh_local_dinodes;
331bedb0f05SAbhi Das 		gfs2_statfs_change_out(&sc, bh->b_data + sizeof(struct gfs2_dinode));
332bedb0f05SAbhi Das 
333bedb0f05SAbhi Das 		fs_info(sdp, "jid=%u: Updated master statfs Total:%lld, "
334bedb0f05SAbhi Das 			"Free:%lld, Dinodes:%lld after change "
335bedb0f05SAbhi Das 			"[%+lld,%+lld,%+lld]\n", jd->jd_jid, sc.sc_total,
336bedb0f05SAbhi Das 			sc.sc_free, sc.sc_dinodes, head->lh_local_total,
337bedb0f05SAbhi Das 			head->lh_local_free, head->lh_local_dinodes);
338bedb0f05SAbhi Das 	} else { /* Zero out the local statfs inode */
339bedb0f05SAbhi Das 		memset(bh->b_data + sizeof(struct gfs2_dinode), 0,
340bedb0f05SAbhi Das 		       sizeof(struct gfs2_statfs_change));
341bedb0f05SAbhi Das 		/* If it's our own journal, reset any in-memory changes too */
342bedb0f05SAbhi Das 		if (jd->jd_jid == sdp->sd_lockstruct.ls_jid) {
343bedb0f05SAbhi Das 			memset(&sdp->sd_statfs_local, 0,
344bedb0f05SAbhi Das 			       sizeof(struct gfs2_statfs_change_host));
345bedb0f05SAbhi Das 		}
346bedb0f05SAbhi Das 	}
347bedb0f05SAbhi Das 	spin_unlock(&sdp->sd_statfs_spin);
348bedb0f05SAbhi Das 
349bedb0f05SAbhi Das 	mark_buffer_dirty(bh);
350bedb0f05SAbhi Das 	brelse(bh);
3514a55752aSBob Peterson 	gfs2_inode_metasync(ip->i_gl);
352bedb0f05SAbhi Das 
353bedb0f05SAbhi Das out:
354bedb0f05SAbhi Das 	return error;
355bedb0f05SAbhi Das }
356bedb0f05SAbhi Das 
357bedb0f05SAbhi Das /**
358bedb0f05SAbhi Das  * recover_local_statfs - Update the master and local statfs changes for this
359bedb0f05SAbhi Das  *			  journal.
360bedb0f05SAbhi Das  *
361bedb0f05SAbhi Das  * Previously, statfs updates would be read in from the local statfs inode and
362bedb0f05SAbhi Das  * synced to the master statfs inode during recovery.
363bedb0f05SAbhi Das  *
364bedb0f05SAbhi Das  * We now use the statfs updates in the journal head to update the master statfs
365bedb0f05SAbhi Das  * inode instead of reading in from the local statfs inode. To preserve backward
366bedb0f05SAbhi Das  * compatibility with kernels that can't do this, we still need to keep the
367bedb0f05SAbhi Das  * local statfs inode up to date by writing changes to it. At some point in the
368bedb0f05SAbhi Das  * future, we can do away with the local statfs inodes altogether and keep the
369bedb0f05SAbhi Das  * statfs changes solely in the journal.
370bedb0f05SAbhi Das  *
371bedb0f05SAbhi Das  * @jd: the journal
372bedb0f05SAbhi Das  * @head: the journal head
373bedb0f05SAbhi Das  *
374bedb0f05SAbhi Das  * Returns: errno
375bedb0f05SAbhi Das  */
recover_local_statfs(struct gfs2_jdesc * jd,struct gfs2_log_header_host * head)376bedb0f05SAbhi Das static void recover_local_statfs(struct gfs2_jdesc *jd,
377bedb0f05SAbhi Das 				 struct gfs2_log_header_host *head)
378bedb0f05SAbhi Das {
379bedb0f05SAbhi Das 	int error;
380bedb0f05SAbhi Das 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
381bedb0f05SAbhi Das 
382bedb0f05SAbhi Das 	if (!head->lh_local_total && !head->lh_local_free
383bedb0f05SAbhi Das 	    && !head->lh_local_dinodes) /* No change */
384bedb0f05SAbhi Das 		goto zero_local;
385bedb0f05SAbhi Das 
386bedb0f05SAbhi Das 	 /* First update the master statfs inode with the changes we
387bedb0f05SAbhi Das 	  * found in the journal. */
388bedb0f05SAbhi Das 	error = update_statfs_inode(jd, head, sdp->sd_statfs_inode);
389bedb0f05SAbhi Das 	if (error)
390bedb0f05SAbhi Das 		goto out;
391bedb0f05SAbhi Das 
392bedb0f05SAbhi Das zero_local:
393bedb0f05SAbhi Das 	/* Zero out the local statfs inode so any changes in there
394bedb0f05SAbhi Das 	 * are not re-recovered. */
395bedb0f05SAbhi Das 	error = update_statfs_inode(jd, NULL,
396bedb0f05SAbhi Das 				    find_local_statfs_inode(sdp, jd->jd_jid));
397bedb0f05SAbhi Das out:
398bedb0f05SAbhi Das 	return;
399bedb0f05SAbhi Das }
400bedb0f05SAbhi Das 
gfs2_recover_func(struct work_struct * work)4016ecd7c2dSTejun Heo void gfs2_recover_func(struct work_struct *work)
402fe64d517SSteven Whitehouse {
403fe64d517SSteven Whitehouse 	struct gfs2_jdesc *jd = container_of(work, struct gfs2_jdesc, jd_work);
404feaa7bbaSSteven Whitehouse 	struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
405feaa7bbaSSteven Whitehouse 	struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
40655167622SAl Viro 	struct gfs2_log_header_host head;
407b77b4a48SAndreas Gruenbacher 	struct gfs2_holder j_gh, ji_gh;
4085e86d9d1SAbhi Das 	ktime_t t_start, t_jlck, t_jhd, t_tlck, t_rep;
409b3b94faaSDavid Teigland 	int ro = 0;
410b3b94faaSDavid Teigland 	unsigned int pass;
4114a772772SBob Peterson 	int error = 0;
412c741c455SSteven Whitehouse 	int jlocked = 0;
413b3b94faaSDavid Teigland 
414*d6b412c5SAndreas Gruenbacher 	if (gfs2_withdrawing_or_withdrawn(sdp)) {
41503678a99SBob Peterson 		fs_err(sdp, "jid=%u: Recovery not attempted due to withdraw.\n",
41603678a99SBob Peterson 		       jd->jd_jid);
41703678a99SBob Peterson 		goto fail;
41803678a99SBob Peterson 	}
4195e86d9d1SAbhi Das 	t_start = ktime_get();
4204a772772SBob Peterson 	if (sdp->sd_args.ar_spectator)
4214a772772SBob Peterson 		goto fail;
4224a772772SBob Peterson 	if (jd->jd_jid != sdp->sd_lockstruct.ls_jid) {
423af1abe11SAndreas Gruenbacher 		fs_info(sdp, "jid=%u: Trying to acquire journal glock...\n",
4245965b1f4SSteven Whitehouse 			jd->jd_jid);
425c741c455SSteven Whitehouse 		jlocked = 1;
426af1abe11SAndreas Gruenbacher 		/* Acquire the journal glock so we can do recovery */
427b3b94faaSDavid Teigland 
428c63e31c2SDavid Teigland 		error = gfs2_glock_nq_num(sdp, jd->jd_jid, &gfs2_journal_glops,
429b3b94faaSDavid Teigland 					  LM_ST_EXCLUSIVE,
430c63e31c2SDavid Teigland 					  LM_FLAG_NOEXP | LM_FLAG_TRY | GL_NOCACHE,
431c63e31c2SDavid Teigland 					  &j_gh);
432b3b94faaSDavid Teigland 		switch (error) {
433b3b94faaSDavid Teigland 		case 0:
434b3b94faaSDavid Teigland 			break;
435b3b94faaSDavid Teigland 
436b3b94faaSDavid Teigland 		case GLR_TRYFAILED:
437b3b94faaSDavid Teigland 			fs_info(sdp, "jid=%u: Busy\n", jd->jd_jid);
438b3b94faaSDavid Teigland 			error = 0;
439e5966cf2SGustavo A. R. Silva 			goto fail;
440b3b94faaSDavid Teigland 
441b3b94faaSDavid Teigland 		default:
442b3b94faaSDavid Teigland 			goto fail;
443098b9c14SAliasgar Surti 		}
444b3b94faaSDavid Teigland 
4455c676f6dSSteven Whitehouse 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
44675be73a8SBob Peterson 					   LM_FLAG_NOEXP | GL_NOCACHE, &ji_gh);
447b3b94faaSDavid Teigland 		if (error)
448b3b94faaSDavid Teigland 			goto fail_gunlock_j;
4495965b1f4SSteven Whitehouse 	} else {
4505965b1f4SSteven Whitehouse 		fs_info(sdp, "jid=%u, already locked for use\n", jd->jd_jid);
4515965b1f4SSteven Whitehouse 	}
452b3b94faaSDavid Teigland 
4535e86d9d1SAbhi Das 	t_jlck = ktime_get();
454b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Looking at journal...\n", jd->jd_jid);
455b3b94faaSDavid Teigland 
456b3b94faaSDavid Teigland 	error = gfs2_jdesc_check(jd);
457b3b94faaSDavid Teigland 	if (error)
458b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
459b3b94faaSDavid Teigland 
460f4686c26SAbhi Das 	error = gfs2_find_jhead(jd, &head, true);
461b3b94faaSDavid Teigland 	if (error)
462b3b94faaSDavid Teigland 		goto fail_gunlock_ji;
4635e86d9d1SAbhi Das 	t_jhd = ktime_get();
46498583b3eSAbhi Das 	fs_info(sdp, "jid=%u: Journal head lookup took %lldms\n", jd->jd_jid,
46598583b3eSAbhi Das 		ktime_ms_delta(t_jhd, t_jlck));
466b3b94faaSDavid Teigland 
467b3b94faaSDavid Teigland 	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
468b77b4a48SAndreas Gruenbacher 		mutex_lock(&sdp->sd_freeze_mutex);
469b3b94faaSDavid Teigland 
4705432af15SAndreas Gruenbacher 		if (test_bit(SDF_FROZEN, &sdp->sd_flags)) {
471b77b4a48SAndreas Gruenbacher 			mutex_unlock(&sdp->sd_freeze_mutex);
472b77b4a48SAndreas Gruenbacher 			fs_warn(sdp, "jid=%u: Can't replay: filesystem "
473b77b4a48SAndreas Gruenbacher 				"is frozen\n", jd->jd_jid);
474b3b94faaSDavid Teigland 			goto fail_gunlock_ji;
475b77b4a48SAndreas Gruenbacher 		}
476b3b94faaSDavid Teigland 
477e8ca5cc5SDavid Teigland 		if (test_bit(SDF_RORECOVERY, &sdp->sd_flags)) {
478e8ca5cc5SDavid Teigland 			ro = 1;
479e8ca5cc5SDavid Teigland 		} else if (test_bit(SDF_JOURNAL_CHECKED, &sdp->sd_flags)) {
480b3b94faaSDavid Teigland 			if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
481b3b94faaSDavid Teigland 				ro = 1;
482b3b94faaSDavid Teigland 		} else {
483bc98a42cSDavid Howells 			if (sb_rdonly(sdp->sd_vfs)) {
4847bc5c414SAbhijith Das 				/* check if device itself is read-only */
4857bc5c414SAbhijith Das 				ro = bdev_read_only(sdp->sd_vfs->s_bdev);
4867bc5c414SAbhijith Das 				if (!ro) {
4877bc5c414SAbhijith Das 					fs_info(sdp, "recovery required on "
4887bc5c414SAbhijith Das 						"read-only filesystem.\n");
4897bc5c414SAbhijith Das 					fs_info(sdp, "write access will be "
4907bc5c414SAbhijith Das 						"enabled during recovery.\n");
4917bc5c414SAbhijith Das 				}
4927bc5c414SAbhijith Das 			}
493b3b94faaSDavid Teigland 		}
494b3b94faaSDavid Teigland 
495b3b94faaSDavid Teigland 		if (ro) {
4967bc5c414SAbhijith Das 			fs_warn(sdp, "jid=%u: Can't replay: read-only block "
4977bc5c414SAbhijith Das 				"device\n", jd->jd_jid);
498b3b94faaSDavid Teigland 			error = -EROFS;
499b77b4a48SAndreas Gruenbacher 			goto fail_gunlock_nofreeze;
500b3b94faaSDavid Teigland 		}
501b3b94faaSDavid Teigland 
5025e86d9d1SAbhi Das 		t_tlck = ktime_get();
50349eb776eSBob Peterson 		fs_info(sdp, "jid=%u: Replaying journal...0x%x to 0x%x\n",
50449eb776eSBob Peterson 			jd->jd_jid, head.lh_tail, head.lh_blkno);
505b3b94faaSDavid Teigland 
506c9ebc4b7SBob Peterson 		/* We take the sd_log_flush_lock here primarily to prevent log
507c9ebc4b7SBob Peterson 		 * flushes and simultaneous journal replays from stomping on
50882218943SBob Peterson 		 * each other wrt jd_log_bio. */
509c953a735SBob Peterson 		down_read(&sdp->sd_log_flush_lock);
510b3b94faaSDavid Teigland 		for (pass = 0; pass < 2; pass++) {
511b3b94faaSDavid Teigland 			lops_before_scan(jd, &head, pass);
512b3b94faaSDavid Teigland 			error = foreach_descriptor(jd, head.lh_tail,
513b3b94faaSDavid Teigland 						   head.lh_blkno, pass);
514b3b94faaSDavid Teigland 			lops_after_scan(jd, error, pass);
515834ec3e1SAndreas Gruenbacher 			if (error) {
516834ec3e1SAndreas Gruenbacher 				up_read(&sdp->sd_log_flush_lock);
517b77b4a48SAndreas Gruenbacher 				goto fail_gunlock_nofreeze;
518b3b94faaSDavid Teigland 			}
519834ec3e1SAndreas Gruenbacher 		}
520b3b94faaSDavid Teigland 
521bedb0f05SAbhi Das 		recover_local_statfs(jd, &head);
522588bff95SBob Peterson 		clean_journal(jd, &head);
523c953a735SBob Peterson 		up_read(&sdp->sd_log_flush_lock);
524b3b94faaSDavid Teigland 
525b77b4a48SAndreas Gruenbacher 		mutex_unlock(&sdp->sd_freeze_mutex);
5265e86d9d1SAbhi Das 		t_rep = ktime_get();
5275e86d9d1SAbhi Das 		fs_info(sdp, "jid=%u: Journal replayed in %lldms [jlck:%lldms, "
5285e86d9d1SAbhi Das 			"jhead:%lldms, tlck:%lldms, replay:%lldms]\n",
5295e86d9d1SAbhi Das 			jd->jd_jid, ktime_ms_delta(t_rep, t_start),
5305e86d9d1SAbhi Das 			ktime_ms_delta(t_jlck, t_start),
5315e86d9d1SAbhi Das 			ktime_ms_delta(t_jhd, t_jlck),
5325e86d9d1SAbhi Das 			ktime_ms_delta(t_tlck, t_jhd),
5335e86d9d1SAbhi Das 			ktime_ms_delta(t_rep, t_tlck));
534b3b94faaSDavid Teigland 	}
535b3b94faaSDavid Teigland 
536f057f6cdSSteven Whitehouse 	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_SUCCESS);
537b3b94faaSDavid Teigland 
538c741c455SSteven Whitehouse 	if (jlocked) {
539c741c455SSteven Whitehouse 		gfs2_glock_dq_uninit(&ji_gh);
540b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
541c741c455SSteven Whitehouse 	}
542b3b94faaSDavid Teigland 
543b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: Done\n", jd->jd_jid);
5446ecd7c2dSTejun Heo 	goto done;
545b3b94faaSDavid Teigland 
546b77b4a48SAndreas Gruenbacher fail_gunlock_nofreeze:
547b77b4a48SAndreas Gruenbacher 	mutex_unlock(&sdp->sd_freeze_mutex);
548b3b94faaSDavid Teigland fail_gunlock_ji:
549c741c455SSteven Whitehouse 	if (jlocked) {
550b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&ji_gh);
551b3b94faaSDavid Teigland fail_gunlock_j:
552b3b94faaSDavid Teigland 		gfs2_glock_dq_uninit(&j_gh);
5535965b1f4SSteven Whitehouse 	}
554b3b94faaSDavid Teigland 
555b3b94faaSDavid Teigland 	fs_info(sdp, "jid=%u: %s\n", jd->jd_jid, (error) ? "Failed" : "Done");
556b3b94faaSDavid Teigland fail:
557376d3778SDavid Teigland 	jd->jd_recover_error = error;
558f057f6cdSSteven Whitehouse 	gfs2_recovery_done(sdp, jd->jd_jid, LM_RD_GAVEUP);
5596ecd7c2dSTejun Heo done:
5606ecd7c2dSTejun Heo 	clear_bit(JDF_RECOVERY, &jd->jd_flags);
5614e857c58SPeter Zijlstra 	smp_mb__after_atomic();
5626ecd7c2dSTejun Heo 	wake_up_bit(&jd->jd_flags, JDF_RECOVERY);
563b3b94faaSDavid Teigland }
564b3b94faaSDavid Teigland 
gfs2_recover_journal(struct gfs2_jdesc * jd,bool wait)5656ecd7c2dSTejun Heo int gfs2_recover_journal(struct gfs2_jdesc *jd, bool wait)
566b3b94faaSDavid Teigland {
567fe64d517SSteven Whitehouse 	int rv;
5686ecd7c2dSTejun Heo 
5696ecd7c2dSTejun Heo 	if (test_and_set_bit(JDF_RECOVERY, &jd->jd_flags))
5706ecd7c2dSTejun Heo 		return -EBUSY;
5716ecd7c2dSTejun Heo 
5726ecd7c2dSTejun Heo 	/* we have JDF_RECOVERY, queue should always succeed */
5735c0dc371SAndreas Gruenbacher 	rv = queue_work(gfs2_recovery_wq, &jd->jd_work);
5746ecd7c2dSTejun Heo 	BUG_ON(!rv);
5756ecd7c2dSTejun Heo 
5766ecd7c2dSTejun Heo 	if (wait)
57774316201SNeilBrown 		wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
5786ecd7c2dSTejun Heo 			    TASK_UNINTERRUPTIBLE);
5796ecd7c2dSTejun Heo 
580376d3778SDavid Teigland 	return wait ? jd->jd_recover_error : 0;
5819ac1b4d9SSteven Whitehouse }
5829ac1b4d9SSteven Whitehouse 
583