xref: /OK3568_Linux_fs/kernel/fs/gfs2/glops.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-2008 Red Hat, Inc.  All rights reserved.
5*4882a593Smuzhiyun  */
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun #include <linux/spinlock.h>
8*4882a593Smuzhiyun #include <linux/completion.h>
9*4882a593Smuzhiyun #include <linux/buffer_head.h>
10*4882a593Smuzhiyun #include <linux/gfs2_ondisk.h>
11*4882a593Smuzhiyun #include <linux/bio.h>
12*4882a593Smuzhiyun #include <linux/posix_acl.h>
13*4882a593Smuzhiyun #include <linux/security.h>
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun #include "gfs2.h"
16*4882a593Smuzhiyun #include "incore.h"
17*4882a593Smuzhiyun #include "bmap.h"
18*4882a593Smuzhiyun #include "glock.h"
19*4882a593Smuzhiyun #include "glops.h"
20*4882a593Smuzhiyun #include "inode.h"
21*4882a593Smuzhiyun #include "log.h"
22*4882a593Smuzhiyun #include "meta_io.h"
23*4882a593Smuzhiyun #include "recovery.h"
24*4882a593Smuzhiyun #include "rgrp.h"
25*4882a593Smuzhiyun #include "util.h"
26*4882a593Smuzhiyun #include "trans.h"
27*4882a593Smuzhiyun #include "dir.h"
28*4882a593Smuzhiyun #include "lops.h"
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun struct workqueue_struct *gfs2_freeze_wq;
31*4882a593Smuzhiyun 
32*4882a593Smuzhiyun extern struct workqueue_struct *gfs2_control_wq;
33*4882a593Smuzhiyun 
gfs2_ail_error(struct gfs2_glock * gl,const struct buffer_head * bh)34*4882a593Smuzhiyun static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
35*4882a593Smuzhiyun {
36*4882a593Smuzhiyun 	fs_err(gl->gl_name.ln_sbd,
37*4882a593Smuzhiyun 	       "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
38*4882a593Smuzhiyun 	       "state 0x%lx\n",
39*4882a593Smuzhiyun 	       bh, (unsigned long long)bh->b_blocknr, bh->b_state,
40*4882a593Smuzhiyun 	       bh->b_page->mapping, bh->b_page->flags);
41*4882a593Smuzhiyun 	fs_err(gl->gl_name.ln_sbd, "AIL glock %u:%llu mapping %p\n",
42*4882a593Smuzhiyun 	       gl->gl_name.ln_type, gl->gl_name.ln_number,
43*4882a593Smuzhiyun 	       gfs2_glock2aspace(gl));
44*4882a593Smuzhiyun 	gfs2_lm(gl->gl_name.ln_sbd, "AIL error\n");
45*4882a593Smuzhiyun 	gfs2_withdraw(gl->gl_name.ln_sbd);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun /**
49*4882a593Smuzhiyun  * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
50*4882a593Smuzhiyun  * @gl: the glock
51*4882a593Smuzhiyun  * @fsync: set when called from fsync (not all buffers will be clean)
52*4882a593Smuzhiyun  *
53*4882a593Smuzhiyun  * None of the buffers should be dirty, locked, or pinned.
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun 
__gfs2_ail_flush(struct gfs2_glock * gl,bool fsync,unsigned int nr_revokes)56*4882a593Smuzhiyun static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
57*4882a593Smuzhiyun 			     unsigned int nr_revokes)
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
60*4882a593Smuzhiyun 	struct list_head *head = &gl->gl_ail_list;
61*4882a593Smuzhiyun 	struct gfs2_bufdata *bd, *tmp;
62*4882a593Smuzhiyun 	struct buffer_head *bh;
63*4882a593Smuzhiyun 	const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
64*4882a593Smuzhiyun 
65*4882a593Smuzhiyun 	gfs2_log_lock(sdp);
66*4882a593Smuzhiyun 	spin_lock(&sdp->sd_ail_lock);
67*4882a593Smuzhiyun 	list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
68*4882a593Smuzhiyun 		if (nr_revokes == 0)
69*4882a593Smuzhiyun 			break;
70*4882a593Smuzhiyun 		bh = bd->bd_bh;
71*4882a593Smuzhiyun 		if (bh->b_state & b_state) {
72*4882a593Smuzhiyun 			if (fsync)
73*4882a593Smuzhiyun 				continue;
74*4882a593Smuzhiyun 			gfs2_ail_error(gl, bh);
75*4882a593Smuzhiyun 		}
76*4882a593Smuzhiyun 		gfs2_trans_add_revoke(sdp, bd);
77*4882a593Smuzhiyun 		nr_revokes--;
78*4882a593Smuzhiyun 	}
79*4882a593Smuzhiyun 	GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
80*4882a593Smuzhiyun 	spin_unlock(&sdp->sd_ail_lock);
81*4882a593Smuzhiyun 	gfs2_log_unlock(sdp);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 
gfs2_ail_empty_gl(struct gfs2_glock * gl)85*4882a593Smuzhiyun static int gfs2_ail_empty_gl(struct gfs2_glock *gl)
86*4882a593Smuzhiyun {
87*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
88*4882a593Smuzhiyun 	struct gfs2_trans tr;
89*4882a593Smuzhiyun 	int ret;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	memset(&tr, 0, sizeof(tr));
92*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tr.tr_buf);
93*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tr.tr_databuf);
94*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tr.tr_ail1_list);
95*4882a593Smuzhiyun 	INIT_LIST_HEAD(&tr.tr_ail2_list);
96*4882a593Smuzhiyun 	tr.tr_revokes = atomic_read(&gl->gl_ail_count);
97*4882a593Smuzhiyun 
98*4882a593Smuzhiyun 	if (!tr.tr_revokes) {
99*4882a593Smuzhiyun 		bool have_revokes;
100*4882a593Smuzhiyun 		bool log_in_flight;
101*4882a593Smuzhiyun 
102*4882a593Smuzhiyun 		/*
103*4882a593Smuzhiyun 		 * We have nothing on the ail, but there could be revokes on
104*4882a593Smuzhiyun 		 * the sdp revoke queue, in which case, we still want to flush
105*4882a593Smuzhiyun 		 * the log and wait for it to finish.
106*4882a593Smuzhiyun 		 *
107*4882a593Smuzhiyun 		 * If the sdp revoke list is empty too, we might still have an
108*4882a593Smuzhiyun 		 * io outstanding for writing revokes, so we should wait for
109*4882a593Smuzhiyun 		 * it before returning.
110*4882a593Smuzhiyun 		 *
111*4882a593Smuzhiyun 		 * If none of these conditions are true, our revokes are all
112*4882a593Smuzhiyun 		 * flushed and we can return.
113*4882a593Smuzhiyun 		 */
114*4882a593Smuzhiyun 		gfs2_log_lock(sdp);
115*4882a593Smuzhiyun 		have_revokes = !list_empty(&sdp->sd_log_revokes);
116*4882a593Smuzhiyun 		log_in_flight = atomic_read(&sdp->sd_log_in_flight);
117*4882a593Smuzhiyun 		gfs2_log_unlock(sdp);
118*4882a593Smuzhiyun 		if (have_revokes)
119*4882a593Smuzhiyun 			goto flush;
120*4882a593Smuzhiyun 		if (log_in_flight)
121*4882a593Smuzhiyun 			log_flush_wait(sdp);
122*4882a593Smuzhiyun 		return 0;
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	/* A shortened, inline version of gfs2_trans_begin()
126*4882a593Smuzhiyun          * tr->alloced is not set since the transaction structure is
127*4882a593Smuzhiyun          * on the stack */
128*4882a593Smuzhiyun 	tr.tr_reserved = 1 + gfs2_struct2blk(sdp, tr.tr_revokes);
129*4882a593Smuzhiyun 	tr.tr_ip = _RET_IP_;
130*4882a593Smuzhiyun 	ret = gfs2_log_reserve(sdp, tr.tr_reserved);
131*4882a593Smuzhiyun 	if (ret < 0)
132*4882a593Smuzhiyun 		return ret;
133*4882a593Smuzhiyun 	WARN_ON_ONCE(current->journal_info);
134*4882a593Smuzhiyun 	current->journal_info = &tr;
135*4882a593Smuzhiyun 
136*4882a593Smuzhiyun 	__gfs2_ail_flush(gl, 0, tr.tr_revokes);
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 	gfs2_trans_end(sdp);
139*4882a593Smuzhiyun flush:
140*4882a593Smuzhiyun 	gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
141*4882a593Smuzhiyun 		       GFS2_LFC_AIL_EMPTY_GL);
142*4882a593Smuzhiyun 	return 0;
143*4882a593Smuzhiyun }
144*4882a593Smuzhiyun 
gfs2_ail_flush(struct gfs2_glock * gl,bool fsync)145*4882a593Smuzhiyun void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
148*4882a593Smuzhiyun 	unsigned int revokes = atomic_read(&gl->gl_ail_count);
149*4882a593Smuzhiyun 	unsigned int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
150*4882a593Smuzhiyun 	int ret;
151*4882a593Smuzhiyun 
152*4882a593Smuzhiyun 	if (!revokes)
153*4882a593Smuzhiyun 		return;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	while (revokes > max_revokes)
156*4882a593Smuzhiyun 		max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 	ret = gfs2_trans_begin(sdp, 0, max_revokes);
159*4882a593Smuzhiyun 	if (ret)
160*4882a593Smuzhiyun 		return;
161*4882a593Smuzhiyun 	__gfs2_ail_flush(gl, fsync, max_revokes);
162*4882a593Smuzhiyun 	gfs2_trans_end(sdp);
163*4882a593Smuzhiyun 	gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
164*4882a593Smuzhiyun 		       GFS2_LFC_AIL_FLUSH);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * gfs2_rgrp_metasync - sync out the metadata of a resource group
169*4882a593Smuzhiyun  * @gl: the glock protecting the resource group
170*4882a593Smuzhiyun  *
171*4882a593Smuzhiyun  */
172*4882a593Smuzhiyun 
gfs2_rgrp_metasync(struct gfs2_glock * gl)173*4882a593Smuzhiyun static int gfs2_rgrp_metasync(struct gfs2_glock *gl)
174*4882a593Smuzhiyun {
175*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
176*4882a593Smuzhiyun 	struct address_space *metamapping = &sdp->sd_aspace;
177*4882a593Smuzhiyun 	struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
178*4882a593Smuzhiyun 	const unsigned bsize = sdp->sd_sb.sb_bsize;
179*4882a593Smuzhiyun 	loff_t start = (rgd->rd_addr * bsize) & PAGE_MASK;
180*4882a593Smuzhiyun 	loff_t end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
181*4882a593Smuzhiyun 	int error;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 	filemap_fdatawrite_range(metamapping, start, end);
184*4882a593Smuzhiyun 	error = filemap_fdatawait_range(metamapping, start, end);
185*4882a593Smuzhiyun 	WARN_ON_ONCE(error && !gfs2_withdrawn(sdp));
186*4882a593Smuzhiyun 	mapping_set_error(metamapping, error);
187*4882a593Smuzhiyun 	if (error)
188*4882a593Smuzhiyun 		gfs2_io_error(sdp);
189*4882a593Smuzhiyun 	return error;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * rgrp_go_sync - sync out the metadata for this glock
194*4882a593Smuzhiyun  * @gl: the glock
195*4882a593Smuzhiyun  *
196*4882a593Smuzhiyun  * Called when demoting or unlocking an EX glock.  We must flush
197*4882a593Smuzhiyun  * to disk all dirty buffers/pages relating to this glock, and must not
198*4882a593Smuzhiyun  * return to caller to demote/unlock the glock until I/O is complete.
199*4882a593Smuzhiyun  */
200*4882a593Smuzhiyun 
rgrp_go_sync(struct gfs2_glock * gl)201*4882a593Smuzhiyun static int rgrp_go_sync(struct gfs2_glock *gl)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
204*4882a593Smuzhiyun 	struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
205*4882a593Smuzhiyun 	int error;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
208*4882a593Smuzhiyun 		return 0;
209*4882a593Smuzhiyun 	GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun 	gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
212*4882a593Smuzhiyun 		       GFS2_LFC_RGRP_GO_SYNC);
213*4882a593Smuzhiyun 	error = gfs2_rgrp_metasync(gl);
214*4882a593Smuzhiyun 	if (!error)
215*4882a593Smuzhiyun 		error = gfs2_ail_empty_gl(gl);
216*4882a593Smuzhiyun 	gfs2_free_clones(rgd);
217*4882a593Smuzhiyun 	return error;
218*4882a593Smuzhiyun }
219*4882a593Smuzhiyun 
220*4882a593Smuzhiyun /**
221*4882a593Smuzhiyun  * rgrp_go_inval - invalidate the metadata for this glock
222*4882a593Smuzhiyun  * @gl: the glock
223*4882a593Smuzhiyun  * @flags:
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * We never used LM_ST_DEFERRED with resource groups, so that we
226*4882a593Smuzhiyun  * should always see the metadata flag set here.
227*4882a593Smuzhiyun  *
228*4882a593Smuzhiyun  */
229*4882a593Smuzhiyun 
rgrp_go_inval(struct gfs2_glock * gl,int flags)230*4882a593Smuzhiyun static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
233*4882a593Smuzhiyun 	struct address_space *mapping = &sdp->sd_aspace;
234*4882a593Smuzhiyun 	struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
235*4882a593Smuzhiyun 	const unsigned bsize = sdp->sd_sb.sb_bsize;
236*4882a593Smuzhiyun 	loff_t start = (rgd->rd_addr * bsize) & PAGE_MASK;
237*4882a593Smuzhiyun 	loff_t end = PAGE_ALIGN((rgd->rd_addr + rgd->rd_length) * bsize) - 1;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	gfs2_rgrp_brelse(rgd);
240*4882a593Smuzhiyun 	WARN_ON_ONCE(!(flags & DIO_METADATA));
241*4882a593Smuzhiyun 	truncate_inode_pages_range(mapping, start, end);
242*4882a593Smuzhiyun 	rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun 
gfs2_rgrp_go_dump(struct seq_file * seq,struct gfs2_glock * gl,const char * fs_id_buf)245*4882a593Smuzhiyun static void gfs2_rgrp_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
246*4882a593Smuzhiyun 			      const char *fs_id_buf)
247*4882a593Smuzhiyun {
248*4882a593Smuzhiyun 	struct gfs2_rgrpd *rgd = gl->gl_object;
249*4882a593Smuzhiyun 
250*4882a593Smuzhiyun 	if (rgd)
251*4882a593Smuzhiyun 		gfs2_rgrp_dump(seq, rgd, fs_id_buf);
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun 
gfs2_glock2inode(struct gfs2_glock * gl)254*4882a593Smuzhiyun static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
255*4882a593Smuzhiyun {
256*4882a593Smuzhiyun 	struct gfs2_inode *ip;
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 	spin_lock(&gl->gl_lockref.lock);
259*4882a593Smuzhiyun 	ip = gl->gl_object;
260*4882a593Smuzhiyun 	if (ip)
261*4882a593Smuzhiyun 		set_bit(GIF_GLOP_PENDING, &ip->i_flags);
262*4882a593Smuzhiyun 	spin_unlock(&gl->gl_lockref.lock);
263*4882a593Smuzhiyun 	return ip;
264*4882a593Smuzhiyun }
265*4882a593Smuzhiyun 
gfs2_glock2rgrp(struct gfs2_glock * gl)266*4882a593Smuzhiyun struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
267*4882a593Smuzhiyun {
268*4882a593Smuzhiyun 	struct gfs2_rgrpd *rgd;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	spin_lock(&gl->gl_lockref.lock);
271*4882a593Smuzhiyun 	rgd = gl->gl_object;
272*4882a593Smuzhiyun 	spin_unlock(&gl->gl_lockref.lock);
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 	return rgd;
275*4882a593Smuzhiyun }
276*4882a593Smuzhiyun 
gfs2_clear_glop_pending(struct gfs2_inode * ip)277*4882a593Smuzhiyun static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
278*4882a593Smuzhiyun {
279*4882a593Smuzhiyun 	if (!ip)
280*4882a593Smuzhiyun 		return;
281*4882a593Smuzhiyun 
282*4882a593Smuzhiyun 	clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
283*4882a593Smuzhiyun 	wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
284*4882a593Smuzhiyun }
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun /**
287*4882a593Smuzhiyun  * gfs2_inode_metasync - sync out the metadata of an inode
288*4882a593Smuzhiyun  * @gl: the glock protecting the inode
289*4882a593Smuzhiyun  *
290*4882a593Smuzhiyun  */
gfs2_inode_metasync(struct gfs2_glock * gl)291*4882a593Smuzhiyun int gfs2_inode_metasync(struct gfs2_glock *gl)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct address_space *metamapping = gfs2_glock2aspace(gl);
294*4882a593Smuzhiyun 	int error;
295*4882a593Smuzhiyun 
296*4882a593Smuzhiyun 	filemap_fdatawrite(metamapping);
297*4882a593Smuzhiyun 	error = filemap_fdatawait(metamapping);
298*4882a593Smuzhiyun 	if (error)
299*4882a593Smuzhiyun 		gfs2_io_error(gl->gl_name.ln_sbd);
300*4882a593Smuzhiyun 	return error;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun 
303*4882a593Smuzhiyun /**
304*4882a593Smuzhiyun  * inode_go_sync - Sync the dirty metadata of an inode
305*4882a593Smuzhiyun  * @gl: the glock protecting the inode
306*4882a593Smuzhiyun  *
307*4882a593Smuzhiyun  */
308*4882a593Smuzhiyun 
inode_go_sync(struct gfs2_glock * gl)309*4882a593Smuzhiyun static int inode_go_sync(struct gfs2_glock *gl)
310*4882a593Smuzhiyun {
311*4882a593Smuzhiyun 	struct gfs2_inode *ip = gfs2_glock2inode(gl);
312*4882a593Smuzhiyun 	int isreg = ip && S_ISREG(ip->i_inode.i_mode);
313*4882a593Smuzhiyun 	struct address_space *metamapping = gfs2_glock2aspace(gl);
314*4882a593Smuzhiyun 	int error = 0, ret;
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 	if (isreg) {
317*4882a593Smuzhiyun 		if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
318*4882a593Smuzhiyun 			unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
319*4882a593Smuzhiyun 		inode_dio_wait(&ip->i_inode);
320*4882a593Smuzhiyun 	}
321*4882a593Smuzhiyun 	if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
322*4882a593Smuzhiyun 		goto out;
323*4882a593Smuzhiyun 
324*4882a593Smuzhiyun 	GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 	gfs2_log_flush(gl->gl_name.ln_sbd, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
327*4882a593Smuzhiyun 		       GFS2_LFC_INODE_GO_SYNC);
328*4882a593Smuzhiyun 	filemap_fdatawrite(metamapping);
329*4882a593Smuzhiyun 	if (isreg) {
330*4882a593Smuzhiyun 		struct address_space *mapping = ip->i_inode.i_mapping;
331*4882a593Smuzhiyun 		filemap_fdatawrite(mapping);
332*4882a593Smuzhiyun 		error = filemap_fdatawait(mapping);
333*4882a593Smuzhiyun 		mapping_set_error(mapping, error);
334*4882a593Smuzhiyun 	}
335*4882a593Smuzhiyun 	ret = gfs2_inode_metasync(gl);
336*4882a593Smuzhiyun 	if (!error)
337*4882a593Smuzhiyun 		error = ret;
338*4882a593Smuzhiyun 	gfs2_ail_empty_gl(gl);
339*4882a593Smuzhiyun 	/*
340*4882a593Smuzhiyun 	 * Writeback of the data mapping may cause the dirty flag to be set
341*4882a593Smuzhiyun 	 * so we have to clear it again here.
342*4882a593Smuzhiyun 	 */
343*4882a593Smuzhiyun 	smp_mb__before_atomic();
344*4882a593Smuzhiyun 	clear_bit(GLF_DIRTY, &gl->gl_flags);
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun out:
347*4882a593Smuzhiyun 	gfs2_clear_glop_pending(ip);
348*4882a593Smuzhiyun 	return error;
349*4882a593Smuzhiyun }
350*4882a593Smuzhiyun 
351*4882a593Smuzhiyun /**
352*4882a593Smuzhiyun  * inode_go_inval - prepare a inode glock to be released
353*4882a593Smuzhiyun  * @gl: the glock
354*4882a593Smuzhiyun  * @flags:
355*4882a593Smuzhiyun  *
356*4882a593Smuzhiyun  * Normally we invalidate everything, but if we are moving into
357*4882a593Smuzhiyun  * LM_ST_DEFERRED from LM_ST_SHARED or LM_ST_EXCLUSIVE then we
358*4882a593Smuzhiyun  * can keep hold of the metadata, since it won't have changed.
359*4882a593Smuzhiyun  *
360*4882a593Smuzhiyun  */
361*4882a593Smuzhiyun 
inode_go_inval(struct gfs2_glock * gl,int flags)362*4882a593Smuzhiyun static void inode_go_inval(struct gfs2_glock *gl, int flags)
363*4882a593Smuzhiyun {
364*4882a593Smuzhiyun 	struct gfs2_inode *ip = gfs2_glock2inode(gl);
365*4882a593Smuzhiyun 
366*4882a593Smuzhiyun 	if (flags & DIO_METADATA) {
367*4882a593Smuzhiyun 		struct address_space *mapping = gfs2_glock2aspace(gl);
368*4882a593Smuzhiyun 		truncate_inode_pages(mapping, 0);
369*4882a593Smuzhiyun 		if (ip) {
370*4882a593Smuzhiyun 			set_bit(GIF_INVALID, &ip->i_flags);
371*4882a593Smuzhiyun 			forget_all_cached_acls(&ip->i_inode);
372*4882a593Smuzhiyun 			security_inode_invalidate_secctx(&ip->i_inode);
373*4882a593Smuzhiyun 			gfs2_dir_hash_inval(ip);
374*4882a593Smuzhiyun 		}
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun 
377*4882a593Smuzhiyun 	if (ip == GFS2_I(gl->gl_name.ln_sbd->sd_rindex)) {
378*4882a593Smuzhiyun 		gfs2_log_flush(gl->gl_name.ln_sbd, NULL,
379*4882a593Smuzhiyun 			       GFS2_LOG_HEAD_FLUSH_NORMAL |
380*4882a593Smuzhiyun 			       GFS2_LFC_INODE_GO_INVAL);
381*4882a593Smuzhiyun 		gl->gl_name.ln_sbd->sd_rindex_uptodate = 0;
382*4882a593Smuzhiyun 	}
383*4882a593Smuzhiyun 	if (ip && S_ISREG(ip->i_inode.i_mode))
384*4882a593Smuzhiyun 		truncate_inode_pages(ip->i_inode.i_mapping, 0);
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	gfs2_clear_glop_pending(ip);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
389*4882a593Smuzhiyun /**
390*4882a593Smuzhiyun  * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
391*4882a593Smuzhiyun  * @gl: the glock
392*4882a593Smuzhiyun  *
393*4882a593Smuzhiyun  * Returns: 1 if it's ok
394*4882a593Smuzhiyun  */
395*4882a593Smuzhiyun 
inode_go_demote_ok(const struct gfs2_glock * gl)396*4882a593Smuzhiyun static int inode_go_demote_ok(const struct gfs2_glock *gl)
397*4882a593Smuzhiyun {
398*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
401*4882a593Smuzhiyun 		return 0;
402*4882a593Smuzhiyun 
403*4882a593Smuzhiyun 	return 1;
404*4882a593Smuzhiyun }
405*4882a593Smuzhiyun 
gfs2_dinode_in(struct gfs2_inode * ip,const void * buf)406*4882a593Smuzhiyun static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
407*4882a593Smuzhiyun {
408*4882a593Smuzhiyun 	const struct gfs2_dinode *str = buf;
409*4882a593Smuzhiyun 	struct timespec64 atime;
410*4882a593Smuzhiyun 	u16 height, depth;
411*4882a593Smuzhiyun 
412*4882a593Smuzhiyun 	if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
413*4882a593Smuzhiyun 		goto corrupt;
414*4882a593Smuzhiyun 	ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
415*4882a593Smuzhiyun 	ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
416*4882a593Smuzhiyun 	ip->i_inode.i_rdev = 0;
417*4882a593Smuzhiyun 	switch (ip->i_inode.i_mode & S_IFMT) {
418*4882a593Smuzhiyun 	case S_IFBLK:
419*4882a593Smuzhiyun 	case S_IFCHR:
420*4882a593Smuzhiyun 		ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
421*4882a593Smuzhiyun 					   be32_to_cpu(str->di_minor));
422*4882a593Smuzhiyun 		break;
423*4882a593Smuzhiyun 	}
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	i_uid_write(&ip->i_inode, be32_to_cpu(str->di_uid));
426*4882a593Smuzhiyun 	i_gid_write(&ip->i_inode, be32_to_cpu(str->di_gid));
427*4882a593Smuzhiyun 	set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
428*4882a593Smuzhiyun 	i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
429*4882a593Smuzhiyun 	gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
430*4882a593Smuzhiyun 	atime.tv_sec = be64_to_cpu(str->di_atime);
431*4882a593Smuzhiyun 	atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
432*4882a593Smuzhiyun 	if (timespec64_compare(&ip->i_inode.i_atime, &atime) < 0)
433*4882a593Smuzhiyun 		ip->i_inode.i_atime = atime;
434*4882a593Smuzhiyun 	ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
435*4882a593Smuzhiyun 	ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
436*4882a593Smuzhiyun 	ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
437*4882a593Smuzhiyun 	ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
438*4882a593Smuzhiyun 
439*4882a593Smuzhiyun 	ip->i_goal = be64_to_cpu(str->di_goal_meta);
440*4882a593Smuzhiyun 	ip->i_generation = be64_to_cpu(str->di_generation);
441*4882a593Smuzhiyun 
442*4882a593Smuzhiyun 	ip->i_diskflags = be32_to_cpu(str->di_flags);
443*4882a593Smuzhiyun 	ip->i_eattr = be64_to_cpu(str->di_eattr);
444*4882a593Smuzhiyun 	/* i_diskflags and i_eattr must be set before gfs2_set_inode_flags() */
445*4882a593Smuzhiyun 	gfs2_set_inode_flags(&ip->i_inode);
446*4882a593Smuzhiyun 	height = be16_to_cpu(str->di_height);
447*4882a593Smuzhiyun 	if (unlikely(height > GFS2_MAX_META_HEIGHT))
448*4882a593Smuzhiyun 		goto corrupt;
449*4882a593Smuzhiyun 	ip->i_height = (u8)height;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	depth = be16_to_cpu(str->di_depth);
452*4882a593Smuzhiyun 	if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
453*4882a593Smuzhiyun 		goto corrupt;
454*4882a593Smuzhiyun 	ip->i_depth = (u8)depth;
455*4882a593Smuzhiyun 	ip->i_entries = be32_to_cpu(str->di_entries);
456*4882a593Smuzhiyun 
457*4882a593Smuzhiyun 	if (S_ISREG(ip->i_inode.i_mode))
458*4882a593Smuzhiyun 		gfs2_set_aops(&ip->i_inode);
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun 	return 0;
461*4882a593Smuzhiyun corrupt:
462*4882a593Smuzhiyun 	gfs2_consist_inode(ip);
463*4882a593Smuzhiyun 	return -EIO;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun /**
467*4882a593Smuzhiyun  * gfs2_inode_refresh - Refresh the incore copy of the dinode
468*4882a593Smuzhiyun  * @ip: The GFS2 inode
469*4882a593Smuzhiyun  *
470*4882a593Smuzhiyun  * Returns: errno
471*4882a593Smuzhiyun  */
472*4882a593Smuzhiyun 
gfs2_inode_refresh(struct gfs2_inode * ip)473*4882a593Smuzhiyun int gfs2_inode_refresh(struct gfs2_inode *ip)
474*4882a593Smuzhiyun {
475*4882a593Smuzhiyun 	struct buffer_head *dibh;
476*4882a593Smuzhiyun 	int error;
477*4882a593Smuzhiyun 
478*4882a593Smuzhiyun 	error = gfs2_meta_inode_buffer(ip, &dibh);
479*4882a593Smuzhiyun 	if (error)
480*4882a593Smuzhiyun 		return error;
481*4882a593Smuzhiyun 
482*4882a593Smuzhiyun 	error = gfs2_dinode_in(ip, dibh->b_data);
483*4882a593Smuzhiyun 	brelse(dibh);
484*4882a593Smuzhiyun 	clear_bit(GIF_INVALID, &ip->i_flags);
485*4882a593Smuzhiyun 
486*4882a593Smuzhiyun 	return error;
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun 
489*4882a593Smuzhiyun /**
490*4882a593Smuzhiyun  * inode_go_lock - operation done after an inode lock is locked by a process
491*4882a593Smuzhiyun  * @gl: the glock
492*4882a593Smuzhiyun  * @flags:
493*4882a593Smuzhiyun  *
494*4882a593Smuzhiyun  * Returns: errno
495*4882a593Smuzhiyun  */
496*4882a593Smuzhiyun 
inode_go_lock(struct gfs2_holder * gh)497*4882a593Smuzhiyun static int inode_go_lock(struct gfs2_holder *gh)
498*4882a593Smuzhiyun {
499*4882a593Smuzhiyun 	struct gfs2_glock *gl = gh->gh_gl;
500*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
501*4882a593Smuzhiyun 	struct gfs2_inode *ip = gl->gl_object;
502*4882a593Smuzhiyun 	int error = 0;
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	if (!ip || (gh->gh_flags & GL_SKIP))
505*4882a593Smuzhiyun 		return 0;
506*4882a593Smuzhiyun 
507*4882a593Smuzhiyun 	if (test_bit(GIF_INVALID, &ip->i_flags)) {
508*4882a593Smuzhiyun 		error = gfs2_inode_refresh(ip);
509*4882a593Smuzhiyun 		if (error)
510*4882a593Smuzhiyun 			return error;
511*4882a593Smuzhiyun 	}
512*4882a593Smuzhiyun 
513*4882a593Smuzhiyun 	if (gh->gh_state != LM_ST_DEFERRED)
514*4882a593Smuzhiyun 		inode_dio_wait(&ip->i_inode);
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
517*4882a593Smuzhiyun 	    (gl->gl_state == LM_ST_EXCLUSIVE) &&
518*4882a593Smuzhiyun 	    (gh->gh_state == LM_ST_EXCLUSIVE)) {
519*4882a593Smuzhiyun 		spin_lock(&sdp->sd_trunc_lock);
520*4882a593Smuzhiyun 		if (list_empty(&ip->i_trunc_list))
521*4882a593Smuzhiyun 			list_add(&ip->i_trunc_list, &sdp->sd_trunc_list);
522*4882a593Smuzhiyun 		spin_unlock(&sdp->sd_trunc_lock);
523*4882a593Smuzhiyun 		wake_up(&sdp->sd_quota_wait);
524*4882a593Smuzhiyun 		return 1;
525*4882a593Smuzhiyun 	}
526*4882a593Smuzhiyun 
527*4882a593Smuzhiyun 	return error;
528*4882a593Smuzhiyun }
529*4882a593Smuzhiyun 
530*4882a593Smuzhiyun /**
531*4882a593Smuzhiyun  * inode_go_dump - print information about an inode
532*4882a593Smuzhiyun  * @seq: The iterator
533*4882a593Smuzhiyun  * @ip: the inode
534*4882a593Smuzhiyun  * @fs_id_buf: file system id (may be empty)
535*4882a593Smuzhiyun  *
536*4882a593Smuzhiyun  */
537*4882a593Smuzhiyun 
inode_go_dump(struct seq_file * seq,struct gfs2_glock * gl,const char * fs_id_buf)538*4882a593Smuzhiyun static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
539*4882a593Smuzhiyun 			  const char *fs_id_buf)
540*4882a593Smuzhiyun {
541*4882a593Smuzhiyun 	struct gfs2_inode *ip = gl->gl_object;
542*4882a593Smuzhiyun 	struct inode *inode = &ip->i_inode;
543*4882a593Smuzhiyun 	unsigned long nrpages;
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	if (ip == NULL)
546*4882a593Smuzhiyun 		return;
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	xa_lock_irq(&inode->i_data.i_pages);
549*4882a593Smuzhiyun 	nrpages = inode->i_data.nrpages;
550*4882a593Smuzhiyun 	xa_unlock_irq(&inode->i_data.i_pages);
551*4882a593Smuzhiyun 
552*4882a593Smuzhiyun 	gfs2_print_dbg(seq, "%s I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu "
553*4882a593Smuzhiyun 		       "p:%lu\n", fs_id_buf,
554*4882a593Smuzhiyun 		  (unsigned long long)ip->i_no_formal_ino,
555*4882a593Smuzhiyun 		  (unsigned long long)ip->i_no_addr,
556*4882a593Smuzhiyun 		  IF2DT(ip->i_inode.i_mode), ip->i_flags,
557*4882a593Smuzhiyun 		  (unsigned int)ip->i_diskflags,
558*4882a593Smuzhiyun 		  (unsigned long long)i_size_read(inode), nrpages);
559*4882a593Smuzhiyun }
560*4882a593Smuzhiyun 
561*4882a593Smuzhiyun /**
562*4882a593Smuzhiyun  * freeze_go_sync - promote/demote the freeze glock
563*4882a593Smuzhiyun  * @gl: the glock
564*4882a593Smuzhiyun  * @state: the requested state
565*4882a593Smuzhiyun  * @flags:
566*4882a593Smuzhiyun  *
567*4882a593Smuzhiyun  */
568*4882a593Smuzhiyun 
freeze_go_sync(struct gfs2_glock * gl)569*4882a593Smuzhiyun static int freeze_go_sync(struct gfs2_glock *gl)
570*4882a593Smuzhiyun {
571*4882a593Smuzhiyun 	int error = 0;
572*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	/*
575*4882a593Smuzhiyun 	 * We need to check gl_state == LM_ST_SHARED here and not gl_req ==
576*4882a593Smuzhiyun 	 * LM_ST_EXCLUSIVE. That's because when any node does a freeze,
577*4882a593Smuzhiyun 	 * all the nodes should have the freeze glock in SH mode and they all
578*4882a593Smuzhiyun 	 * call do_xmote: One for EX and the others for UN. They ALL must
579*4882a593Smuzhiyun 	 * freeze locally, and they ALL must queue freeze work. The freeze_work
580*4882a593Smuzhiyun 	 * calls freeze_func, which tries to reacquire the freeze glock in SH,
581*4882a593Smuzhiyun 	 * effectively waiting for the thaw on the node who holds it in EX.
582*4882a593Smuzhiyun 	 * Once thawed, the work func acquires the freeze glock in
583*4882a593Smuzhiyun 	 * SH and everybody goes back to thawed.
584*4882a593Smuzhiyun 	 */
585*4882a593Smuzhiyun 	if (gl->gl_state == LM_ST_SHARED && !gfs2_withdrawn(sdp) &&
586*4882a593Smuzhiyun 	    !test_bit(SDF_NORECOVERY, &sdp->sd_flags)) {
587*4882a593Smuzhiyun 		atomic_set(&sdp->sd_freeze_state, SFS_STARTING_FREEZE);
588*4882a593Smuzhiyun 		error = freeze_super(sdp->sd_vfs);
589*4882a593Smuzhiyun 		if (error) {
590*4882a593Smuzhiyun 			fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
591*4882a593Smuzhiyun 				error);
592*4882a593Smuzhiyun 			if (gfs2_withdrawn(sdp)) {
593*4882a593Smuzhiyun 				atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
594*4882a593Smuzhiyun 				return 0;
595*4882a593Smuzhiyun 			}
596*4882a593Smuzhiyun 			gfs2_assert_withdraw(sdp, 0);
597*4882a593Smuzhiyun 		}
598*4882a593Smuzhiyun 		queue_work(gfs2_freeze_wq, &sdp->sd_freeze_work);
599*4882a593Smuzhiyun 		if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))
600*4882a593Smuzhiyun 			gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
601*4882a593Smuzhiyun 				       GFS2_LFC_FREEZE_GO_SYNC);
602*4882a593Smuzhiyun 		else /* read-only mounts */
603*4882a593Smuzhiyun 			atomic_set(&sdp->sd_freeze_state, SFS_FROZEN);
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 	return 0;
606*4882a593Smuzhiyun }
607*4882a593Smuzhiyun 
608*4882a593Smuzhiyun /**
609*4882a593Smuzhiyun  * freeze_go_xmote_bh - After promoting/demoting the freeze glock
610*4882a593Smuzhiyun  * @gl: the glock
611*4882a593Smuzhiyun  *
612*4882a593Smuzhiyun  */
613*4882a593Smuzhiyun 
freeze_go_xmote_bh(struct gfs2_glock * gl,struct gfs2_holder * gh)614*4882a593Smuzhiyun static int freeze_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
615*4882a593Smuzhiyun {
616*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
617*4882a593Smuzhiyun 	struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
618*4882a593Smuzhiyun 	struct gfs2_glock *j_gl = ip->i_gl;
619*4882a593Smuzhiyun 	struct gfs2_log_header_host head;
620*4882a593Smuzhiyun 	int error;
621*4882a593Smuzhiyun 
622*4882a593Smuzhiyun 	if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
623*4882a593Smuzhiyun 		j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
624*4882a593Smuzhiyun 
625*4882a593Smuzhiyun 		error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
626*4882a593Smuzhiyun 		if (gfs2_assert_withdraw_delayed(sdp, !error))
627*4882a593Smuzhiyun 			return error;
628*4882a593Smuzhiyun 		if (gfs2_assert_withdraw_delayed(sdp, head.lh_flags &
629*4882a593Smuzhiyun 						 GFS2_LOG_HEAD_UNMOUNT))
630*4882a593Smuzhiyun 			return -EIO;
631*4882a593Smuzhiyun 		sdp->sd_log_sequence = head.lh_sequence + 1;
632*4882a593Smuzhiyun 		gfs2_log_pointers_init(sdp, head.lh_blkno);
633*4882a593Smuzhiyun 	}
634*4882a593Smuzhiyun 	return 0;
635*4882a593Smuzhiyun }
636*4882a593Smuzhiyun 
637*4882a593Smuzhiyun /**
638*4882a593Smuzhiyun  * trans_go_demote_ok
639*4882a593Smuzhiyun  * @gl: the glock
640*4882a593Smuzhiyun  *
641*4882a593Smuzhiyun  * Always returns 0
642*4882a593Smuzhiyun  */
643*4882a593Smuzhiyun 
freeze_go_demote_ok(const struct gfs2_glock * gl)644*4882a593Smuzhiyun static int freeze_go_demote_ok(const struct gfs2_glock *gl)
645*4882a593Smuzhiyun {
646*4882a593Smuzhiyun 	return 0;
647*4882a593Smuzhiyun }
648*4882a593Smuzhiyun 
649*4882a593Smuzhiyun /**
650*4882a593Smuzhiyun  * iopen_go_callback - schedule the dcache entry for the inode to be deleted
651*4882a593Smuzhiyun  * @gl: the glock
652*4882a593Smuzhiyun  *
653*4882a593Smuzhiyun  * gl_lockref.lock lock is held while calling this
654*4882a593Smuzhiyun  */
iopen_go_callback(struct gfs2_glock * gl,bool remote)655*4882a593Smuzhiyun static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
656*4882a593Smuzhiyun {
657*4882a593Smuzhiyun 	struct gfs2_inode *ip = gl->gl_object;
658*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
659*4882a593Smuzhiyun 
660*4882a593Smuzhiyun 	if (!remote || sb_rdonly(sdp->sd_vfs))
661*4882a593Smuzhiyun 		return;
662*4882a593Smuzhiyun 
663*4882a593Smuzhiyun 	if (gl->gl_demote_state == LM_ST_UNLOCKED &&
664*4882a593Smuzhiyun 	    gl->gl_state == LM_ST_SHARED && ip) {
665*4882a593Smuzhiyun 		gl->gl_lockref.count++;
666*4882a593Smuzhiyun 		if (!queue_delayed_work(gfs2_delete_workqueue,
667*4882a593Smuzhiyun 					&gl->gl_delete, 0))
668*4882a593Smuzhiyun 			gl->gl_lockref.count--;
669*4882a593Smuzhiyun 	}
670*4882a593Smuzhiyun }
671*4882a593Smuzhiyun 
iopen_go_demote_ok(const struct gfs2_glock * gl)672*4882a593Smuzhiyun static int iopen_go_demote_ok(const struct gfs2_glock *gl)
673*4882a593Smuzhiyun {
674*4882a593Smuzhiyun        return !gfs2_delete_work_queued(gl);
675*4882a593Smuzhiyun }
676*4882a593Smuzhiyun 
677*4882a593Smuzhiyun /**
678*4882a593Smuzhiyun  * inode_go_free - wake up anyone waiting for dlm's unlock ast to free it
679*4882a593Smuzhiyun  * @gl: glock being freed
680*4882a593Smuzhiyun  *
681*4882a593Smuzhiyun  * For now, this is only used for the journal inode glock. In withdraw
682*4882a593Smuzhiyun  * situations, we need to wait for the glock to be freed so that we know
683*4882a593Smuzhiyun  * other nodes may proceed with recovery / journal replay.
684*4882a593Smuzhiyun  */
inode_go_free(struct gfs2_glock * gl)685*4882a593Smuzhiyun static void inode_go_free(struct gfs2_glock *gl)
686*4882a593Smuzhiyun {
687*4882a593Smuzhiyun 	/* Note that we cannot reference gl_object because it's already set
688*4882a593Smuzhiyun 	 * to NULL by this point in its lifecycle. */
689*4882a593Smuzhiyun 	if (!test_bit(GLF_FREEING, &gl->gl_flags))
690*4882a593Smuzhiyun 		return;
691*4882a593Smuzhiyun 	clear_bit_unlock(GLF_FREEING, &gl->gl_flags);
692*4882a593Smuzhiyun 	wake_up_bit(&gl->gl_flags, GLF_FREEING);
693*4882a593Smuzhiyun }
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun /**
696*4882a593Smuzhiyun  * nondisk_go_callback - used to signal when a node did a withdraw
697*4882a593Smuzhiyun  * @gl: the nondisk glock
698*4882a593Smuzhiyun  * @remote: true if this came from a different cluster node
699*4882a593Smuzhiyun  *
700*4882a593Smuzhiyun  */
nondisk_go_callback(struct gfs2_glock * gl,bool remote)701*4882a593Smuzhiyun static void nondisk_go_callback(struct gfs2_glock *gl, bool remote)
702*4882a593Smuzhiyun {
703*4882a593Smuzhiyun 	struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
704*4882a593Smuzhiyun 
705*4882a593Smuzhiyun 	/* Ignore the callback unless it's from another node, and it's the
706*4882a593Smuzhiyun 	   live lock. */
707*4882a593Smuzhiyun 	if (!remote || gl->gl_name.ln_number != GFS2_LIVE_LOCK)
708*4882a593Smuzhiyun 		return;
709*4882a593Smuzhiyun 
710*4882a593Smuzhiyun 	/* First order of business is to cancel the demote request. We don't
711*4882a593Smuzhiyun 	 * really want to demote a nondisk glock. At best it's just to inform
712*4882a593Smuzhiyun 	 * us of another node's withdraw. We'll keep it in SH mode. */
713*4882a593Smuzhiyun 	clear_bit(GLF_DEMOTE, &gl->gl_flags);
714*4882a593Smuzhiyun 	clear_bit(GLF_PENDING_DEMOTE, &gl->gl_flags);
715*4882a593Smuzhiyun 
716*4882a593Smuzhiyun 	/* Ignore the unlock if we're withdrawn, unmounting, or in recovery. */
717*4882a593Smuzhiyun 	if (test_bit(SDF_NORECOVERY, &sdp->sd_flags) ||
718*4882a593Smuzhiyun 	    test_bit(SDF_WITHDRAWN, &sdp->sd_flags) ||
719*4882a593Smuzhiyun 	    test_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags))
720*4882a593Smuzhiyun 		return;
721*4882a593Smuzhiyun 
722*4882a593Smuzhiyun 	/* We only care when a node wants us to unlock, because that means
723*4882a593Smuzhiyun 	 * they want a journal recovered. */
724*4882a593Smuzhiyun 	if (gl->gl_demote_state != LM_ST_UNLOCKED)
725*4882a593Smuzhiyun 		return;
726*4882a593Smuzhiyun 
727*4882a593Smuzhiyun 	if (sdp->sd_args.ar_spectator) {
728*4882a593Smuzhiyun 		fs_warn(sdp, "Spectator node cannot recover journals.\n");
729*4882a593Smuzhiyun 		return;
730*4882a593Smuzhiyun 	}
731*4882a593Smuzhiyun 
732*4882a593Smuzhiyun 	fs_warn(sdp, "Some node has withdrawn; checking for recovery.\n");
733*4882a593Smuzhiyun 	set_bit(SDF_REMOTE_WITHDRAW, &sdp->sd_flags);
734*4882a593Smuzhiyun 	/*
735*4882a593Smuzhiyun 	 * We can't call remote_withdraw directly here or gfs2_recover_journal
736*4882a593Smuzhiyun 	 * because this is called from the glock unlock function and the
737*4882a593Smuzhiyun 	 * remote_withdraw needs to enqueue and dequeue the same "live" glock
738*4882a593Smuzhiyun 	 * we were called from. So we queue it to the control work queue in
739*4882a593Smuzhiyun 	 * lock_dlm.
740*4882a593Smuzhiyun 	 */
741*4882a593Smuzhiyun 	queue_delayed_work(gfs2_control_wq, &sdp->sd_control_work, 0);
742*4882a593Smuzhiyun }
743*4882a593Smuzhiyun 
744*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_meta_glops = {
745*4882a593Smuzhiyun 	.go_type = LM_TYPE_META,
746*4882a593Smuzhiyun 	.go_flags = GLOF_NONDISK,
747*4882a593Smuzhiyun };
748*4882a593Smuzhiyun 
749*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_inode_glops = {
750*4882a593Smuzhiyun 	.go_sync = inode_go_sync,
751*4882a593Smuzhiyun 	.go_inval = inode_go_inval,
752*4882a593Smuzhiyun 	.go_demote_ok = inode_go_demote_ok,
753*4882a593Smuzhiyun 	.go_lock = inode_go_lock,
754*4882a593Smuzhiyun 	.go_dump = inode_go_dump,
755*4882a593Smuzhiyun 	.go_type = LM_TYPE_INODE,
756*4882a593Smuzhiyun 	.go_flags = GLOF_ASPACE | GLOF_LRU | GLOF_LVB,
757*4882a593Smuzhiyun 	.go_free = inode_go_free,
758*4882a593Smuzhiyun };
759*4882a593Smuzhiyun 
760*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_rgrp_glops = {
761*4882a593Smuzhiyun 	.go_sync = rgrp_go_sync,
762*4882a593Smuzhiyun 	.go_inval = rgrp_go_inval,
763*4882a593Smuzhiyun 	.go_lock = gfs2_rgrp_go_lock,
764*4882a593Smuzhiyun 	.go_dump = gfs2_rgrp_go_dump,
765*4882a593Smuzhiyun 	.go_type = LM_TYPE_RGRP,
766*4882a593Smuzhiyun 	.go_flags = GLOF_LVB,
767*4882a593Smuzhiyun };
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_freeze_glops = {
770*4882a593Smuzhiyun 	.go_sync = freeze_go_sync,
771*4882a593Smuzhiyun 	.go_xmote_bh = freeze_go_xmote_bh,
772*4882a593Smuzhiyun 	.go_demote_ok = freeze_go_demote_ok,
773*4882a593Smuzhiyun 	.go_type = LM_TYPE_NONDISK,
774*4882a593Smuzhiyun 	.go_flags = GLOF_NONDISK,
775*4882a593Smuzhiyun };
776*4882a593Smuzhiyun 
777*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_iopen_glops = {
778*4882a593Smuzhiyun 	.go_type = LM_TYPE_IOPEN,
779*4882a593Smuzhiyun 	.go_callback = iopen_go_callback,
780*4882a593Smuzhiyun 	.go_demote_ok = iopen_go_demote_ok,
781*4882a593Smuzhiyun 	.go_flags = GLOF_LRU | GLOF_NONDISK,
782*4882a593Smuzhiyun 	.go_subclass = 1,
783*4882a593Smuzhiyun };
784*4882a593Smuzhiyun 
785*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_flock_glops = {
786*4882a593Smuzhiyun 	.go_type = LM_TYPE_FLOCK,
787*4882a593Smuzhiyun 	.go_flags = GLOF_LRU | GLOF_NONDISK,
788*4882a593Smuzhiyun };
789*4882a593Smuzhiyun 
790*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_nondisk_glops = {
791*4882a593Smuzhiyun 	.go_type = LM_TYPE_NONDISK,
792*4882a593Smuzhiyun 	.go_flags = GLOF_NONDISK,
793*4882a593Smuzhiyun 	.go_callback = nondisk_go_callback,
794*4882a593Smuzhiyun };
795*4882a593Smuzhiyun 
796*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_quota_glops = {
797*4882a593Smuzhiyun 	.go_type = LM_TYPE_QUOTA,
798*4882a593Smuzhiyun 	.go_flags = GLOF_LVB | GLOF_LRU | GLOF_NONDISK,
799*4882a593Smuzhiyun };
800*4882a593Smuzhiyun 
801*4882a593Smuzhiyun const struct gfs2_glock_operations gfs2_journal_glops = {
802*4882a593Smuzhiyun 	.go_type = LM_TYPE_JOURNAL,
803*4882a593Smuzhiyun 	.go_flags = GLOF_NONDISK,
804*4882a593Smuzhiyun };
805*4882a593Smuzhiyun 
806*4882a593Smuzhiyun const struct gfs2_glock_operations *gfs2_glops_list[] = {
807*4882a593Smuzhiyun 	[LM_TYPE_META] = &gfs2_meta_glops,
808*4882a593Smuzhiyun 	[LM_TYPE_INODE] = &gfs2_inode_glops,
809*4882a593Smuzhiyun 	[LM_TYPE_RGRP] = &gfs2_rgrp_glops,
810*4882a593Smuzhiyun 	[LM_TYPE_IOPEN] = &gfs2_iopen_glops,
811*4882a593Smuzhiyun 	[LM_TYPE_FLOCK] = &gfs2_flock_glops,
812*4882a593Smuzhiyun 	[LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
813*4882a593Smuzhiyun 	[LM_TYPE_QUOTA] = &gfs2_quota_glops,
814*4882a593Smuzhiyun 	[LM_TYPE_JOURNAL] = &gfs2_journal_glops,
815*4882a593Smuzhiyun };
816*4882a593Smuzhiyun 
817