xref: /OK3568_Linux_fs/kernel/fs/gfs2/util.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) Sistina Software, Inc.  1997-2003 All rights reserved.
4*4882a593Smuzhiyun  * Copyright (C) 2004-2006 Red Hat, Inc.  All rights reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/spinlock.h>
10*4882a593Smuzhiyun #include <linux/completion.h>
11*4882a593Smuzhiyun #include <linux/buffer_head.h>
12*4882a593Smuzhiyun #include <linux/crc32.h>
13*4882a593Smuzhiyun #include <linux/gfs2_ondisk.h>
14*4882a593Smuzhiyun #include <linux/delay.h>
15*4882a593Smuzhiyun #include <linux/uaccess.h>
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun #include "gfs2.h"
18*4882a593Smuzhiyun #include "incore.h"
19*4882a593Smuzhiyun #include "glock.h"
20*4882a593Smuzhiyun #include "glops.h"
21*4882a593Smuzhiyun #include "log.h"
22*4882a593Smuzhiyun #include "lops.h"
23*4882a593Smuzhiyun #include "recovery.h"
24*4882a593Smuzhiyun #include "rgrp.h"
25*4882a593Smuzhiyun #include "super.h"
26*4882a593Smuzhiyun #include "util.h"
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun struct kmem_cache *gfs2_glock_cachep __read_mostly;
29*4882a593Smuzhiyun struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly;
30*4882a593Smuzhiyun struct kmem_cache *gfs2_inode_cachep __read_mostly;
31*4882a593Smuzhiyun struct kmem_cache *gfs2_bufdata_cachep __read_mostly;
32*4882a593Smuzhiyun struct kmem_cache *gfs2_rgrpd_cachep __read_mostly;
33*4882a593Smuzhiyun struct kmem_cache *gfs2_quotad_cachep __read_mostly;
34*4882a593Smuzhiyun struct kmem_cache *gfs2_qadata_cachep __read_mostly;
35*4882a593Smuzhiyun struct kmem_cache *gfs2_trans_cachep __read_mostly;
36*4882a593Smuzhiyun mempool_t *gfs2_page_pool __read_mostly;
37*4882a593Smuzhiyun 
gfs2_assert_i(struct gfs2_sbd * sdp)38*4882a593Smuzhiyun void gfs2_assert_i(struct gfs2_sbd *sdp)
39*4882a593Smuzhiyun {
40*4882a593Smuzhiyun 	fs_emerg(sdp, "fatal assertion failed\n");
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun 
43*4882a593Smuzhiyun /**
44*4882a593Smuzhiyun  * check_journal_clean - Make sure a journal is clean for a spectator mount
45*4882a593Smuzhiyun  * @sdp: The GFS2 superblock
46*4882a593Smuzhiyun  * @jd: The journal descriptor
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * Returns: 0 if the journal is clean or locked, else an error
49*4882a593Smuzhiyun  */
check_journal_clean(struct gfs2_sbd * sdp,struct gfs2_jdesc * jd,bool verbose)50*4882a593Smuzhiyun int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd,
51*4882a593Smuzhiyun 			bool verbose)
52*4882a593Smuzhiyun {
53*4882a593Smuzhiyun 	int error;
54*4882a593Smuzhiyun 	struct gfs2_holder j_gh;
55*4882a593Smuzhiyun 	struct gfs2_log_header_host head;
56*4882a593Smuzhiyun 	struct gfs2_inode *ip;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 	ip = GFS2_I(jd->jd_inode);
59*4882a593Smuzhiyun 	error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_NOEXP |
60*4882a593Smuzhiyun 				   GL_EXACT | GL_NOCACHE, &j_gh);
61*4882a593Smuzhiyun 	if (error) {
62*4882a593Smuzhiyun 		if (verbose)
63*4882a593Smuzhiyun 			fs_err(sdp, "Error %d locking journal for spectator "
64*4882a593Smuzhiyun 			       "mount.\n", error);
65*4882a593Smuzhiyun 		return -EPERM;
66*4882a593Smuzhiyun 	}
67*4882a593Smuzhiyun 	error = gfs2_jdesc_check(jd);
68*4882a593Smuzhiyun 	if (error) {
69*4882a593Smuzhiyun 		if (verbose)
70*4882a593Smuzhiyun 			fs_err(sdp, "Error checking journal for spectator "
71*4882a593Smuzhiyun 			       "mount.\n");
72*4882a593Smuzhiyun 		goto out_unlock;
73*4882a593Smuzhiyun 	}
74*4882a593Smuzhiyun 	error = gfs2_find_jhead(jd, &head, false);
75*4882a593Smuzhiyun 	if (error) {
76*4882a593Smuzhiyun 		if (verbose)
77*4882a593Smuzhiyun 			fs_err(sdp, "Error parsing journal for spectator "
78*4882a593Smuzhiyun 			       "mount.\n");
79*4882a593Smuzhiyun 		goto out_unlock;
80*4882a593Smuzhiyun 	}
81*4882a593Smuzhiyun 	if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
82*4882a593Smuzhiyun 		error = -EPERM;
83*4882a593Smuzhiyun 		if (verbose)
84*4882a593Smuzhiyun 			fs_err(sdp, "jid=%u: Journal is dirty, so the first "
85*4882a593Smuzhiyun 			       "mounter must not be a spectator.\n",
86*4882a593Smuzhiyun 			       jd->jd_jid);
87*4882a593Smuzhiyun 	}
88*4882a593Smuzhiyun 
89*4882a593Smuzhiyun out_unlock:
90*4882a593Smuzhiyun 	gfs2_glock_dq_uninit(&j_gh);
91*4882a593Smuzhiyun 	return error;
92*4882a593Smuzhiyun }
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun /**
95*4882a593Smuzhiyun  * gfs2_freeze_lock - hold the freeze glock
96*4882a593Smuzhiyun  * @sdp: the superblock
97*4882a593Smuzhiyun  * @freeze_gh: pointer to the requested holder
98*4882a593Smuzhiyun  * @caller_flags: any additional flags needed by the caller
99*4882a593Smuzhiyun  */
gfs2_freeze_lock(struct gfs2_sbd * sdp,struct gfs2_holder * freeze_gh,int caller_flags)100*4882a593Smuzhiyun int gfs2_freeze_lock(struct gfs2_sbd *sdp, struct gfs2_holder *freeze_gh,
101*4882a593Smuzhiyun 		     int caller_flags)
102*4882a593Smuzhiyun {
103*4882a593Smuzhiyun 	int flags = LM_FLAG_NOEXP | GL_EXACT | caller_flags;
104*4882a593Smuzhiyun 	int error;
105*4882a593Smuzhiyun 
106*4882a593Smuzhiyun 	error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, flags,
107*4882a593Smuzhiyun 				   freeze_gh);
108*4882a593Smuzhiyun 	if (error && error != GLR_TRYFAILED)
109*4882a593Smuzhiyun 		fs_err(sdp, "can't lock the freeze lock: %d\n", error);
110*4882a593Smuzhiyun 	return error;
111*4882a593Smuzhiyun }
112*4882a593Smuzhiyun 
gfs2_freeze_unlock(struct gfs2_holder * freeze_gh)113*4882a593Smuzhiyun void gfs2_freeze_unlock(struct gfs2_holder *freeze_gh)
114*4882a593Smuzhiyun {
115*4882a593Smuzhiyun 	if (gfs2_holder_initialized(freeze_gh))
116*4882a593Smuzhiyun 		gfs2_glock_dq_uninit(freeze_gh);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
signal_our_withdraw(struct gfs2_sbd * sdp)119*4882a593Smuzhiyun static void signal_our_withdraw(struct gfs2_sbd *sdp)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	struct gfs2_glock *live_gl = sdp->sd_live_gh.gh_gl;
122*4882a593Smuzhiyun 	struct inode *inode;
123*4882a593Smuzhiyun 	struct gfs2_inode *ip;
124*4882a593Smuzhiyun 	struct gfs2_glock *i_gl;
125*4882a593Smuzhiyun 	u64 no_formal_ino;
126*4882a593Smuzhiyun 	int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
127*4882a593Smuzhiyun 	int ret = 0;
128*4882a593Smuzhiyun 	int tries;
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun 	if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) || !sdp->sd_jdesc)
131*4882a593Smuzhiyun 		return;
132*4882a593Smuzhiyun 
133*4882a593Smuzhiyun 	inode = sdp->sd_jdesc->jd_inode;
134*4882a593Smuzhiyun 	ip = GFS2_I(inode);
135*4882a593Smuzhiyun 	i_gl = ip->i_gl;
136*4882a593Smuzhiyun 	no_formal_ino = ip->i_no_formal_ino;
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	/* Prevent any glock dq until withdraw recovery is complete */
139*4882a593Smuzhiyun 	set_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
140*4882a593Smuzhiyun 	/*
141*4882a593Smuzhiyun 	 * Don't tell dlm we're bailing until we have no more buffers in the
142*4882a593Smuzhiyun 	 * wind. If journal had an IO error, the log code should just purge
143*4882a593Smuzhiyun 	 * the outstanding buffers rather than submitting new IO. Making the
144*4882a593Smuzhiyun 	 * file system read-only will flush the journal, etc.
145*4882a593Smuzhiyun 	 *
146*4882a593Smuzhiyun 	 * During a normal unmount, gfs2_make_fs_ro calls gfs2_log_shutdown
147*4882a593Smuzhiyun 	 * which clears SDF_JOURNAL_LIVE. In a withdraw, we must not write
148*4882a593Smuzhiyun 	 * any UNMOUNT log header, so we can't call gfs2_log_shutdown, and
149*4882a593Smuzhiyun 	 * therefore we need to clear SDF_JOURNAL_LIVE manually.
150*4882a593Smuzhiyun 	 */
151*4882a593Smuzhiyun 	clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
152*4882a593Smuzhiyun 	if (!sb_rdonly(sdp->sd_vfs)) {
153*4882a593Smuzhiyun 		struct gfs2_holder freeze_gh;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 		gfs2_holder_mark_uninitialized(&freeze_gh);
156*4882a593Smuzhiyun 		if (sdp->sd_freeze_gl &&
157*4882a593Smuzhiyun 		    !gfs2_glock_is_locked_by_me(sdp->sd_freeze_gl)) {
158*4882a593Smuzhiyun 			ret = gfs2_freeze_lock(sdp, &freeze_gh,
159*4882a593Smuzhiyun 				       log_write_allowed ? 0 : LM_FLAG_TRY);
160*4882a593Smuzhiyun 			if (ret == GLR_TRYFAILED)
161*4882a593Smuzhiyun 				ret = 0;
162*4882a593Smuzhiyun 		}
163*4882a593Smuzhiyun 		if (!ret)
164*4882a593Smuzhiyun 			ret = gfs2_make_fs_ro(sdp);
165*4882a593Smuzhiyun 		gfs2_freeze_unlock(&freeze_gh);
166*4882a593Smuzhiyun 	}
167*4882a593Smuzhiyun 
168*4882a593Smuzhiyun 	if (sdp->sd_lockstruct.ls_ops->lm_lock == NULL) { /* lock_nolock */
169*4882a593Smuzhiyun 		if (!ret)
170*4882a593Smuzhiyun 			ret = -EIO;
171*4882a593Smuzhiyun 		clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
172*4882a593Smuzhiyun 		goto skip_recovery;
173*4882a593Smuzhiyun 	}
174*4882a593Smuzhiyun 	/*
175*4882a593Smuzhiyun 	 * Drop the glock for our journal so another node can recover it.
176*4882a593Smuzhiyun 	 */
177*4882a593Smuzhiyun 	if (gfs2_holder_initialized(&sdp->sd_journal_gh)) {
178*4882a593Smuzhiyun 		gfs2_glock_dq_wait(&sdp->sd_journal_gh);
179*4882a593Smuzhiyun 		gfs2_holder_uninit(&sdp->sd_journal_gh);
180*4882a593Smuzhiyun 	}
181*4882a593Smuzhiyun 	sdp->sd_jinode_gh.gh_flags |= GL_NOCACHE;
182*4882a593Smuzhiyun 	gfs2_glock_dq(&sdp->sd_jinode_gh);
183*4882a593Smuzhiyun 	if (test_bit(SDF_FS_FROZEN, &sdp->sd_flags)) {
184*4882a593Smuzhiyun 		/* Make sure gfs2_unfreeze works if partially-frozen */
185*4882a593Smuzhiyun 		flush_workqueue(gfs2_freeze_wq);
186*4882a593Smuzhiyun 		atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
187*4882a593Smuzhiyun 		thaw_super(sdp->sd_vfs);
188*4882a593Smuzhiyun 	} else {
189*4882a593Smuzhiyun 		wait_on_bit(&i_gl->gl_flags, GLF_DEMOTE,
190*4882a593Smuzhiyun 			    TASK_UNINTERRUPTIBLE);
191*4882a593Smuzhiyun 	}
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 	/*
194*4882a593Smuzhiyun 	 * holder_uninit to force glock_put, to force dlm to let go
195*4882a593Smuzhiyun 	 */
196*4882a593Smuzhiyun 	gfs2_holder_uninit(&sdp->sd_jinode_gh);
197*4882a593Smuzhiyun 
198*4882a593Smuzhiyun 	/*
199*4882a593Smuzhiyun 	 * Note: We need to be careful here:
200*4882a593Smuzhiyun 	 * Our iput of jd_inode will evict it. The evict will dequeue its
201*4882a593Smuzhiyun 	 * glock, but the glock dq will wait for the withdraw unless we have
202*4882a593Smuzhiyun 	 * exception code in glock_dq.
203*4882a593Smuzhiyun 	 */
204*4882a593Smuzhiyun 	iput(inode);
205*4882a593Smuzhiyun 	/*
206*4882a593Smuzhiyun 	 * Wait until the journal inode's glock is freed. This allows try locks
207*4882a593Smuzhiyun 	 * on other nodes to be successful, otherwise we remain the owner of
208*4882a593Smuzhiyun 	 * the glock as far as dlm is concerned.
209*4882a593Smuzhiyun 	 */
210*4882a593Smuzhiyun 	if (i_gl->gl_ops->go_free) {
211*4882a593Smuzhiyun 		set_bit(GLF_FREEING, &i_gl->gl_flags);
212*4882a593Smuzhiyun 		wait_on_bit(&i_gl->gl_flags, GLF_FREEING, TASK_UNINTERRUPTIBLE);
213*4882a593Smuzhiyun 	}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	/*
216*4882a593Smuzhiyun 	 * Dequeue the "live" glock, but keep a reference so it's never freed.
217*4882a593Smuzhiyun 	 */
218*4882a593Smuzhiyun 	gfs2_glock_hold(live_gl);
219*4882a593Smuzhiyun 	gfs2_glock_dq_wait(&sdp->sd_live_gh);
220*4882a593Smuzhiyun 	/*
221*4882a593Smuzhiyun 	 * We enqueue the "live" glock in EX so that all other nodes
222*4882a593Smuzhiyun 	 * get a demote request and act on it. We don't really want the
223*4882a593Smuzhiyun 	 * lock in EX, so we send a "try" lock with 1CB to produce a callback.
224*4882a593Smuzhiyun 	 */
225*4882a593Smuzhiyun 	fs_warn(sdp, "Requesting recovery of jid %d.\n",
226*4882a593Smuzhiyun 		sdp->sd_lockstruct.ls_jid);
227*4882a593Smuzhiyun 	gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | LM_FLAG_NOEXP,
228*4882a593Smuzhiyun 			   &sdp->sd_live_gh);
229*4882a593Smuzhiyun 	msleep(GL_GLOCK_MAX_HOLD);
230*4882a593Smuzhiyun 	/*
231*4882a593Smuzhiyun 	 * This will likely fail in a cluster, but succeed standalone:
232*4882a593Smuzhiyun 	 */
233*4882a593Smuzhiyun 	ret = gfs2_glock_nq(&sdp->sd_live_gh);
234*4882a593Smuzhiyun 
235*4882a593Smuzhiyun 	/*
236*4882a593Smuzhiyun 	 * If we actually got the "live" lock in EX mode, there are no other
237*4882a593Smuzhiyun 	 * nodes available to replay our journal. So we try to replay it
238*4882a593Smuzhiyun 	 * ourselves. We hold the "live" glock to prevent other mounters
239*4882a593Smuzhiyun 	 * during recovery, then just dequeue it and reacquire it in our
240*4882a593Smuzhiyun 	 * normal SH mode. Just in case the problem that caused us to
241*4882a593Smuzhiyun 	 * withdraw prevents us from recovering our journal (e.g. io errors
242*4882a593Smuzhiyun 	 * and such) we still check if the journal is clean before proceeding
243*4882a593Smuzhiyun 	 * but we may wait forever until another mounter does the recovery.
244*4882a593Smuzhiyun 	 */
245*4882a593Smuzhiyun 	if (ret == 0) {
246*4882a593Smuzhiyun 		fs_warn(sdp, "No other mounters found. Trying to recover our "
247*4882a593Smuzhiyun 			"own journal jid %d.\n", sdp->sd_lockstruct.ls_jid);
248*4882a593Smuzhiyun 		if (gfs2_recover_journal(sdp->sd_jdesc, 1))
249*4882a593Smuzhiyun 			fs_warn(sdp, "Unable to recover our journal jid %d.\n",
250*4882a593Smuzhiyun 				sdp->sd_lockstruct.ls_jid);
251*4882a593Smuzhiyun 		gfs2_glock_dq_wait(&sdp->sd_live_gh);
252*4882a593Smuzhiyun 		gfs2_holder_reinit(LM_ST_SHARED, LM_FLAG_NOEXP | GL_EXACT,
253*4882a593Smuzhiyun 				   &sdp->sd_live_gh);
254*4882a593Smuzhiyun 		gfs2_glock_nq(&sdp->sd_live_gh);
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	gfs2_glock_queue_put(live_gl); /* drop extra reference we acquired */
258*4882a593Smuzhiyun 	clear_bit(SDF_WITHDRAW_RECOVERY, &sdp->sd_flags);
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	/*
261*4882a593Smuzhiyun 	 * At this point our journal is evicted, so we need to get a new inode
262*4882a593Smuzhiyun 	 * for it. Once done, we need to call gfs2_find_jhead which
263*4882a593Smuzhiyun 	 * calls gfs2_map_journal_extents to map it for us again.
264*4882a593Smuzhiyun 	 *
265*4882a593Smuzhiyun 	 * Note that we don't really want it to look up a FREE block. The
266*4882a593Smuzhiyun 	 * GFS2_BLKST_FREE simply overrides a block check in gfs2_inode_lookup
267*4882a593Smuzhiyun 	 * which would otherwise fail because it requires grabbing an rgrp
268*4882a593Smuzhiyun 	 * glock, which would fail with -EIO because we're withdrawing.
269*4882a593Smuzhiyun 	 */
270*4882a593Smuzhiyun 	inode = gfs2_inode_lookup(sdp->sd_vfs, DT_UNKNOWN,
271*4882a593Smuzhiyun 				  sdp->sd_jdesc->jd_no_addr, no_formal_ino,
272*4882a593Smuzhiyun 				  GFS2_BLKST_FREE);
273*4882a593Smuzhiyun 	if (IS_ERR(inode)) {
274*4882a593Smuzhiyun 		fs_warn(sdp, "Reprocessing of jid %d failed with %ld.\n",
275*4882a593Smuzhiyun 			sdp->sd_lockstruct.ls_jid, PTR_ERR(inode));
276*4882a593Smuzhiyun 		goto skip_recovery;
277*4882a593Smuzhiyun 	}
278*4882a593Smuzhiyun 	sdp->sd_jdesc->jd_inode = inode;
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 	/*
281*4882a593Smuzhiyun 	 * Now wait until recovery is complete.
282*4882a593Smuzhiyun 	 */
283*4882a593Smuzhiyun 	for (tries = 0; tries < 10; tries++) {
284*4882a593Smuzhiyun 		ret = check_journal_clean(sdp, sdp->sd_jdesc, false);
285*4882a593Smuzhiyun 		if (!ret)
286*4882a593Smuzhiyun 			break;
287*4882a593Smuzhiyun 		msleep(HZ);
288*4882a593Smuzhiyun 		fs_warn(sdp, "Waiting for journal recovery jid %d.\n",
289*4882a593Smuzhiyun 			sdp->sd_lockstruct.ls_jid);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun skip_recovery:
292*4882a593Smuzhiyun 	if (!ret)
293*4882a593Smuzhiyun 		fs_warn(sdp, "Journal recovery complete for jid %d.\n",
294*4882a593Smuzhiyun 			sdp->sd_lockstruct.ls_jid);
295*4882a593Smuzhiyun 	else
296*4882a593Smuzhiyun 		fs_warn(sdp, "Journal recovery skipped for %d until next "
297*4882a593Smuzhiyun 			"mount.\n", sdp->sd_lockstruct.ls_jid);
298*4882a593Smuzhiyun 	fs_warn(sdp, "Glock dequeues delayed: %lu\n", sdp->sd_glock_dqs_held);
299*4882a593Smuzhiyun 	sdp->sd_glock_dqs_held = 0;
300*4882a593Smuzhiyun 	wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_RECOVERY);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
gfs2_lm(struct gfs2_sbd * sdp,const char * fmt,...)303*4882a593Smuzhiyun void gfs2_lm(struct gfs2_sbd *sdp, const char *fmt, ...)
304*4882a593Smuzhiyun {
305*4882a593Smuzhiyun 	struct va_format vaf;
306*4882a593Smuzhiyun 	va_list args;
307*4882a593Smuzhiyun 
308*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
309*4882a593Smuzhiyun 	    test_bit(SDF_WITHDRAWN, &sdp->sd_flags))
310*4882a593Smuzhiyun 		return;
311*4882a593Smuzhiyun 
312*4882a593Smuzhiyun 	va_start(args, fmt);
313*4882a593Smuzhiyun 	vaf.fmt = fmt;
314*4882a593Smuzhiyun 	vaf.va = &args;
315*4882a593Smuzhiyun 	fs_err(sdp, "%pV", &vaf);
316*4882a593Smuzhiyun 	va_end(args);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun 
gfs2_withdraw(struct gfs2_sbd * sdp)319*4882a593Smuzhiyun int gfs2_withdraw(struct gfs2_sbd *sdp)
320*4882a593Smuzhiyun {
321*4882a593Smuzhiyun 	struct lm_lockstruct *ls = &sdp->sd_lockstruct;
322*4882a593Smuzhiyun 	const struct lm_lockops *lm = ls->ls_ops;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW &&
325*4882a593Smuzhiyun 	    test_and_set_bit(SDF_WITHDRAWN, &sdp->sd_flags)) {
326*4882a593Smuzhiyun 		if (!test_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags))
327*4882a593Smuzhiyun 			return -1;
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 		wait_on_bit(&sdp->sd_flags, SDF_WITHDRAW_IN_PROG,
330*4882a593Smuzhiyun 			    TASK_UNINTERRUPTIBLE);
331*4882a593Smuzhiyun 		return -1;
332*4882a593Smuzhiyun 	}
333*4882a593Smuzhiyun 
334*4882a593Smuzhiyun 	set_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags);
335*4882a593Smuzhiyun 
336*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) {
337*4882a593Smuzhiyun 		fs_err(sdp, "about to withdraw this file system\n");
338*4882a593Smuzhiyun 		BUG_ON(sdp->sd_args.ar_debug);
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun 		signal_our_withdraw(sdp);
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 		kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE);
343*4882a593Smuzhiyun 
344*4882a593Smuzhiyun 		if (!strcmp(sdp->sd_lockstruct.ls_ops->lm_proto_name, "lock_dlm"))
345*4882a593Smuzhiyun 			wait_for_completion(&sdp->sd_wdack);
346*4882a593Smuzhiyun 
347*4882a593Smuzhiyun 		if (lm->lm_unmount) {
348*4882a593Smuzhiyun 			fs_err(sdp, "telling LM to unmount\n");
349*4882a593Smuzhiyun 			lm->lm_unmount(sdp);
350*4882a593Smuzhiyun 		}
351*4882a593Smuzhiyun 		set_bit(SDF_SKIP_DLM_UNLOCK, &sdp->sd_flags);
352*4882a593Smuzhiyun 		fs_err(sdp, "File system withdrawn\n");
353*4882a593Smuzhiyun 		dump_stack();
354*4882a593Smuzhiyun 		clear_bit(SDF_WITHDRAW_IN_PROG, &sdp->sd_flags);
355*4882a593Smuzhiyun 		smp_mb__after_atomic();
356*4882a593Smuzhiyun 		wake_up_bit(&sdp->sd_flags, SDF_WITHDRAW_IN_PROG);
357*4882a593Smuzhiyun 	}
358*4882a593Smuzhiyun 
359*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
360*4882a593Smuzhiyun 		panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname);
361*4882a593Smuzhiyun 
362*4882a593Smuzhiyun 	return -1;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
365*4882a593Smuzhiyun /**
366*4882a593Smuzhiyun  * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false
367*4882a593Smuzhiyun  */
368*4882a593Smuzhiyun 
gfs2_assert_withdraw_i(struct gfs2_sbd * sdp,char * assertion,const char * function,char * file,unsigned int line,bool delayed)369*4882a593Smuzhiyun void gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion,
370*4882a593Smuzhiyun 			    const char *function, char *file, unsigned int line,
371*4882a593Smuzhiyun 			    bool delayed)
372*4882a593Smuzhiyun {
373*4882a593Smuzhiyun 	if (gfs2_withdrawn(sdp))
374*4882a593Smuzhiyun 		return;
375*4882a593Smuzhiyun 
376*4882a593Smuzhiyun 	fs_err(sdp,
377*4882a593Smuzhiyun 	       "fatal: assertion \"%s\" failed\n"
378*4882a593Smuzhiyun 	       "   function = %s, file = %s, line = %u\n",
379*4882a593Smuzhiyun 	       assertion, function, file, line);
380*4882a593Smuzhiyun 
381*4882a593Smuzhiyun 	/*
382*4882a593Smuzhiyun 	 * If errors=panic was specified on mount, it won't help to delay the
383*4882a593Smuzhiyun 	 * withdraw.
384*4882a593Smuzhiyun 	 */
385*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
386*4882a593Smuzhiyun 		delayed = false;
387*4882a593Smuzhiyun 
388*4882a593Smuzhiyun 	if (delayed)
389*4882a593Smuzhiyun 		gfs2_withdraw_delayed(sdp);
390*4882a593Smuzhiyun 	else
391*4882a593Smuzhiyun 		gfs2_withdraw(sdp);
392*4882a593Smuzhiyun 	dump_stack();
393*4882a593Smuzhiyun }
394*4882a593Smuzhiyun 
395*4882a593Smuzhiyun /**
396*4882a593Smuzhiyun  * gfs2_assert_warn_i - Print a message to the console if @assertion is false
397*4882a593Smuzhiyun  */
398*4882a593Smuzhiyun 
gfs2_assert_warn_i(struct gfs2_sbd * sdp,char * assertion,const char * function,char * file,unsigned int line)399*4882a593Smuzhiyun void gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion,
400*4882a593Smuzhiyun 			const char *function, char *file, unsigned int line)
401*4882a593Smuzhiyun {
402*4882a593Smuzhiyun 	if (time_before(jiffies,
403*4882a593Smuzhiyun 			sdp->sd_last_warning +
404*4882a593Smuzhiyun 			gfs2_tune_get(sdp, gt_complain_secs) * HZ))
405*4882a593Smuzhiyun 		return;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW)
408*4882a593Smuzhiyun 		fs_warn(sdp, "warning: assertion \"%s\" failed at function = %s, file = %s, line = %u\n",
409*4882a593Smuzhiyun 			assertion, function, file, line);
410*4882a593Smuzhiyun 
411*4882a593Smuzhiyun 	if (sdp->sd_args.ar_debug)
412*4882a593Smuzhiyun 		BUG();
413*4882a593Smuzhiyun 	else
414*4882a593Smuzhiyun 		dump_stack();
415*4882a593Smuzhiyun 
416*4882a593Smuzhiyun 	if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC)
417*4882a593Smuzhiyun 		panic("GFS2: fsid=%s: warning: assertion \"%s\" failed\n"
418*4882a593Smuzhiyun 		      "GFS2: fsid=%s:   function = %s, file = %s, line = %u\n",
419*4882a593Smuzhiyun 		      sdp->sd_fsname, assertion,
420*4882a593Smuzhiyun 		      sdp->sd_fsname, function, file, line);
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	sdp->sd_last_warning = jiffies;
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun /**
426*4882a593Smuzhiyun  * gfs2_consist_i - Flag a filesystem consistency error and withdraw
427*4882a593Smuzhiyun  */
428*4882a593Smuzhiyun 
gfs2_consist_i(struct gfs2_sbd * sdp,const char * function,char * file,unsigned int line)429*4882a593Smuzhiyun void gfs2_consist_i(struct gfs2_sbd *sdp, const char *function,
430*4882a593Smuzhiyun 		    char *file, unsigned int line)
431*4882a593Smuzhiyun {
432*4882a593Smuzhiyun 	gfs2_lm(sdp,
433*4882a593Smuzhiyun 		"fatal: filesystem consistency error - function = %s, file = %s, line = %u\n",
434*4882a593Smuzhiyun 		function, file, line);
435*4882a593Smuzhiyun 	gfs2_withdraw(sdp);
436*4882a593Smuzhiyun }
437*4882a593Smuzhiyun 
438*4882a593Smuzhiyun /**
439*4882a593Smuzhiyun  * gfs2_consist_inode_i - Flag an inode consistency error and withdraw
440*4882a593Smuzhiyun  */
441*4882a593Smuzhiyun 
gfs2_consist_inode_i(struct gfs2_inode * ip,const char * function,char * file,unsigned int line)442*4882a593Smuzhiyun void gfs2_consist_inode_i(struct gfs2_inode *ip,
443*4882a593Smuzhiyun 			  const char *function, char *file, unsigned int line)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	gfs2_lm(sdp,
448*4882a593Smuzhiyun 		"fatal: filesystem consistency error\n"
449*4882a593Smuzhiyun 		"  inode = %llu %llu\n"
450*4882a593Smuzhiyun 		"  function = %s, file = %s, line = %u\n",
451*4882a593Smuzhiyun 		(unsigned long long)ip->i_no_formal_ino,
452*4882a593Smuzhiyun 		(unsigned long long)ip->i_no_addr,
453*4882a593Smuzhiyun 		function, file, line);
454*4882a593Smuzhiyun 	gfs2_withdraw(sdp);
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun /**
458*4882a593Smuzhiyun  * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw
459*4882a593Smuzhiyun  */
460*4882a593Smuzhiyun 
gfs2_consist_rgrpd_i(struct gfs2_rgrpd * rgd,const char * function,char * file,unsigned int line)461*4882a593Smuzhiyun void gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd,
462*4882a593Smuzhiyun 			  const char *function, char *file, unsigned int line)
463*4882a593Smuzhiyun {
464*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = rgd->rd_sbd;
465*4882a593Smuzhiyun 	char fs_id_buf[sizeof(sdp->sd_fsname) + 7];
466*4882a593Smuzhiyun 
467*4882a593Smuzhiyun 	sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname);
468*4882a593Smuzhiyun 	gfs2_rgrp_dump(NULL, rgd, fs_id_buf);
469*4882a593Smuzhiyun 	gfs2_lm(sdp,
470*4882a593Smuzhiyun 		"fatal: filesystem consistency error\n"
471*4882a593Smuzhiyun 		"  RG = %llu\n"
472*4882a593Smuzhiyun 		"  function = %s, file = %s, line = %u\n",
473*4882a593Smuzhiyun 		(unsigned long long)rgd->rd_addr,
474*4882a593Smuzhiyun 		function, file, line);
475*4882a593Smuzhiyun 	gfs2_withdraw(sdp);
476*4882a593Smuzhiyun }
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun /**
479*4882a593Smuzhiyun  * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw
480*4882a593Smuzhiyun  * Returns: -1 if this call withdrew the machine,
481*4882a593Smuzhiyun  *          -2 if it was already withdrawn
482*4882a593Smuzhiyun  */
483*4882a593Smuzhiyun 
gfs2_meta_check_ii(struct gfs2_sbd * sdp,struct buffer_head * bh,const char * type,const char * function,char * file,unsigned int line)484*4882a593Smuzhiyun int gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
485*4882a593Smuzhiyun 		       const char *type, const char *function, char *file,
486*4882a593Smuzhiyun 		       unsigned int line)
487*4882a593Smuzhiyun {
488*4882a593Smuzhiyun 	int me;
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun 	gfs2_lm(sdp,
491*4882a593Smuzhiyun 		"fatal: invalid metadata block\n"
492*4882a593Smuzhiyun 		"  bh = %llu (%s)\n"
493*4882a593Smuzhiyun 		"  function = %s, file = %s, line = %u\n",
494*4882a593Smuzhiyun 		(unsigned long long)bh->b_blocknr, type,
495*4882a593Smuzhiyun 		function, file, line);
496*4882a593Smuzhiyun 	me = gfs2_withdraw(sdp);
497*4882a593Smuzhiyun 	return (me) ? -1 : -2;
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun 
500*4882a593Smuzhiyun /**
501*4882a593Smuzhiyun  * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw
502*4882a593Smuzhiyun  * Returns: -1 if this call withdrew the machine,
503*4882a593Smuzhiyun  *          -2 if it was already withdrawn
504*4882a593Smuzhiyun  */
505*4882a593Smuzhiyun 
gfs2_metatype_check_ii(struct gfs2_sbd * sdp,struct buffer_head * bh,u16 type,u16 t,const char * function,char * file,unsigned int line)506*4882a593Smuzhiyun int gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh,
507*4882a593Smuzhiyun 			   u16 type, u16 t, const char *function,
508*4882a593Smuzhiyun 			   char *file, unsigned int line)
509*4882a593Smuzhiyun {
510*4882a593Smuzhiyun 	int me;
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 	gfs2_lm(sdp,
513*4882a593Smuzhiyun 		"fatal: invalid metadata block\n"
514*4882a593Smuzhiyun 		"  bh = %llu (type: exp=%u, found=%u)\n"
515*4882a593Smuzhiyun 		"  function = %s, file = %s, line = %u\n",
516*4882a593Smuzhiyun 		(unsigned long long)bh->b_blocknr, type, t,
517*4882a593Smuzhiyun 		function, file, line);
518*4882a593Smuzhiyun 	me = gfs2_withdraw(sdp);
519*4882a593Smuzhiyun 	return (me) ? -1 : -2;
520*4882a593Smuzhiyun }
521*4882a593Smuzhiyun 
522*4882a593Smuzhiyun /**
523*4882a593Smuzhiyun  * gfs2_io_error_i - Flag an I/O error and withdraw
524*4882a593Smuzhiyun  * Returns: -1 if this call withdrew the machine,
525*4882a593Smuzhiyun  *          0 if it was already withdrawn
526*4882a593Smuzhiyun  */
527*4882a593Smuzhiyun 
gfs2_io_error_i(struct gfs2_sbd * sdp,const char * function,char * file,unsigned int line)528*4882a593Smuzhiyun int gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file,
529*4882a593Smuzhiyun 		    unsigned int line)
530*4882a593Smuzhiyun {
531*4882a593Smuzhiyun 	gfs2_lm(sdp,
532*4882a593Smuzhiyun 		"fatal: I/O error\n"
533*4882a593Smuzhiyun 		"  function = %s, file = %s, line = %u\n",
534*4882a593Smuzhiyun 		function, file, line);
535*4882a593Smuzhiyun 	return gfs2_withdraw(sdp);
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun /**
539*4882a593Smuzhiyun  * gfs2_io_error_bh_i - Flag a buffer I/O error
540*4882a593Smuzhiyun  * @withdraw: withdraw the filesystem
541*4882a593Smuzhiyun  */
542*4882a593Smuzhiyun 
gfs2_io_error_bh_i(struct gfs2_sbd * sdp,struct buffer_head * bh,const char * function,char * file,unsigned int line,bool withdraw)543*4882a593Smuzhiyun void gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh,
544*4882a593Smuzhiyun 			const char *function, char *file, unsigned int line,
545*4882a593Smuzhiyun 			bool withdraw)
546*4882a593Smuzhiyun {
547*4882a593Smuzhiyun 	if (gfs2_withdrawn(sdp))
548*4882a593Smuzhiyun 		return;
549*4882a593Smuzhiyun 
550*4882a593Smuzhiyun 	fs_err(sdp, "fatal: I/O error\n"
551*4882a593Smuzhiyun 	       "  block = %llu\n"
552*4882a593Smuzhiyun 	       "  function = %s, file = %s, line = %u\n",
553*4882a593Smuzhiyun 	       (unsigned long long)bh->b_blocknr, function, file, line);
554*4882a593Smuzhiyun 	if (withdraw)
555*4882a593Smuzhiyun 		gfs2_withdraw(sdp);
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun 
558