blob: 061d22e1ceb6e72018d0dcb54274a9a5886d9d49 [file] [log] [blame]
Thomas Gleixner7336d0e2019-05-31 01:09:56 -07001// SPDX-License-Identifier: GPL-2.0-only
David Teiglandb3b94fa2006-01-16 16:50:04 +00002/*
3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
Bob Petersoncf45b752008-01-31 10:31:39 -06004 * Copyright (C) 2004-2008 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
David Teiglandb3b94fa2006-01-16 16:50:04 +00007#include <linux/spinlock.h>
8#include <linux/completion.h>
9#include <linux/buffer_head.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050010#include <linux/gfs2_ondisk.h>
Steven Whitehouse6802e342008-05-21 17:03:22 +010011#include <linux/bio.h>
Steven Whitehousec65f7fb2009-10-02 11:54:39 +010012#include <linux/posix_acl.h>
Andreas Gruenbacherf39814f62015-12-24 11:09:40 -050013#include <linux/security.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000014
15#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050016#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000017#include "bmap.h"
18#include "glock.h"
19#include "glops.h"
20#include "inode.h"
21#include "log.h"
22#include "meta_io.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include "recovery.h"
24#include "rgrp.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050025#include "util.h"
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040026#include "trans.h"
Steven Whitehouse17d539f2011-06-15 10:29:37 +010027#include "dir.h"
Abhi Dasf4686c22019-05-02 14:17:40 -050028#include "lops.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060030struct workqueue_struct *gfs2_freeze_wq;
31
Steven Whitehouse75549182011-08-02 13:09:36 +010032static void gfs2_ail_error(struct gfs2_glock *gl, const struct buffer_head *bh)
33{
Bob Peterson15562c42015-03-16 11:52:05 -050034 fs_err(gl->gl_name.ln_sbd,
35 "AIL buffer %p: blocknr %llu state 0x%08lx mapping %p page "
36 "state 0x%lx\n",
Steven Whitehouse75549182011-08-02 13:09:36 +010037 bh, (unsigned long long)bh->b_blocknr, bh->b_state,
38 bh->b_page->mapping, bh->b_page->flags);
Bob Peterson15562c42015-03-16 11:52:05 -050039 fs_err(gl->gl_name.ln_sbd, "AIL glock %u:%llu mapping %p\n",
Steven Whitehouse75549182011-08-02 13:09:36 +010040 gl->gl_name.ln_type, gl->gl_name.ln_number,
41 gfs2_glock2aspace(gl));
Bob Peterson15562c42015-03-16 11:52:05 -050042 gfs2_lm_withdraw(gl->gl_name.ln_sbd, "AIL error\n");
Steven Whitehouse75549182011-08-02 13:09:36 +010043}
44
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040045/**
Steven Whitehousedba898b2011-04-14 09:54:02 +010046 * __gfs2_ail_flush - remove all buffers for a given lock from the AIL
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040047 * @gl: the glock
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010048 * @fsync: set when called from fsync (not all buffers will be clean)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040049 *
50 * None of the buffers should be dirty, locked, or pinned.
51 */
52
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -050053static void __gfs2_ail_flush(struct gfs2_glock *gl, bool fsync,
54 unsigned int nr_revokes)
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040055{
Bob Peterson15562c42015-03-16 11:52:05 -050056 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040057 struct list_head *head = &gl->gl_ail_list;
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010058 struct gfs2_bufdata *bd, *tmp;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040059 struct buffer_head *bh;
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010060 const unsigned long b_state = (1UL << BH_Dirty)|(1UL << BH_Pinned)|(1UL << BH_Lock);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040061
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010062 gfs2_log_lock(sdp);
Dave Chinnerd6a079e2011-03-11 11:52:25 +000063 spin_lock(&sdp->sd_ail_lock);
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -050064 list_for_each_entry_safe_reverse(bd, tmp, head, bd_ail_gl_list) {
65 if (nr_revokes == 0)
66 break;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040067 bh = bd->bd_bh;
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010068 if (bh->b_state & b_state) {
69 if (fsync)
70 continue;
Steven Whitehouse75549182011-08-02 13:09:36 +010071 gfs2_ail_error(gl, bh);
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010072 }
Steven Whitehouse1ad38c42007-09-03 11:01:33 +010073 gfs2_trans_add_revoke(sdp, bd);
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -050074 nr_revokes--;
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040075 }
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +010076 GLOCK_BUG_ON(gl, !fsync && atomic_read(&gl->gl_ail_count));
Dave Chinnerd6a079e2011-03-11 11:52:25 +000077 spin_unlock(&sdp->sd_ail_lock);
Steven Whitehouseb5b24d72011-09-07 10:33:25 +010078 gfs2_log_unlock(sdp);
Steven Whitehousedba898b2011-04-14 09:54:02 +010079}
Steven Whitehouseddacfaf2006-10-03 11:10:41 -040080
Steven Whitehousedba898b2011-04-14 09:54:02 +010081
82static void gfs2_ail_empty_gl(struct gfs2_glock *gl)
83{
Bob Peterson15562c42015-03-16 11:52:05 -050084 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousedba898b2011-04-14 09:54:02 +010085 struct gfs2_trans tr;
86
87 memset(&tr, 0, sizeof(tr));
Steven Whitehoused69a3c62014-02-21 15:22:35 +000088 INIT_LIST_HEAD(&tr.tr_buf);
89 INIT_LIST_HEAD(&tr.tr_databuf);
Steven Whitehousedba898b2011-04-14 09:54:02 +010090 tr.tr_revokes = atomic_read(&gl->gl_ail_count);
91
92 if (!tr.tr_revokes)
93 return;
94
Benjamin Marzinski24972552014-05-01 22:26:55 -050095 /* A shortened, inline version of gfs2_trans_begin()
96 * tr->alloced is not set since the transaction structure is
97 * on the stack */
Bob Peterson2e9eeaa2019-12-13 08:10:51 -060098 tr.tr_reserved = 1 + gfs2_struct2blk(sdp, tr.tr_revokes);
Fabian Frederickd29c0af2014-10-03 20:15:36 +020099 tr.tr_ip = _RET_IP_;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600100 if (gfs2_log_reserve(sdp, tr.tr_reserved) < 0)
Benjamin Marzinski24972552014-05-01 22:26:55 -0500101 return;
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +0100102 WARN_ON_ONCE(current->journal_info);
Steven Whitehousedba898b2011-04-14 09:54:02 +0100103 current->journal_info = &tr;
104
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -0500105 __gfs2_ail_flush(gl, 0, tr.tr_revokes);
Steven Whitehousedba898b2011-04-14 09:54:02 +0100106
107 gfs2_trans_end(sdp);
Bob Peterson805c09072018-01-08 10:34:17 -0500108 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
109 GFS2_LFC_AIL_EMPTY_GL);
Steven Whitehousedba898b2011-04-14 09:54:02 +0100110}
111
Steven Whitehouseb5b24d72011-09-07 10:33:25 +0100112void gfs2_ail_flush(struct gfs2_glock *gl, bool fsync)
Steven Whitehousedba898b2011-04-14 09:54:02 +0100113{
Bob Peterson15562c42015-03-16 11:52:05 -0500114 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousedba898b2011-04-14 09:54:02 +0100115 unsigned int revokes = atomic_read(&gl->gl_ail_count);
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -0500116 unsigned int max_revokes = (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_log_descriptor)) / sizeof(u64);
Steven Whitehousedba898b2011-04-14 09:54:02 +0100117 int ret;
118
119 if (!revokes)
120 return;
121
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -0500122 while (revokes > max_revokes)
123 max_revokes += (sdp->sd_sb.sb_bsize - sizeof(struct gfs2_meta_header)) / sizeof(u64);
124
125 ret = gfs2_trans_begin(sdp, 0, max_revokes);
Steven Whitehousedba898b2011-04-14 09:54:02 +0100126 if (ret)
127 return;
Benjamin Marzinski1bc333f2013-07-26 17:09:33 -0500128 __gfs2_ail_flush(gl, fsync, max_revokes);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400129 gfs2_trans_end(sdp);
Bob Peterson805c09072018-01-08 10:34:17 -0500130 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
131 GFS2_LFC_AIL_FLUSH);
Steven Whitehouseddacfaf2006-10-03 11:10:41 -0400132}
Steven Whitehouseba7f7292006-07-26 11:27:10 -0400133
134/**
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000135 * rgrp_go_sync - sync out the metadata for this glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 * @gl: the glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000137 *
138 * Called when demoting or unlocking an EX glock. We must flush
139 * to disk all dirty buffers/pages relating to this glock, and must not
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500140 * return to caller to demote/unlock the glock until I/O is complete.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 */
142
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000143static void rgrp_go_sync(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000144{
Bob Peterson15562c42015-03-16 11:52:05 -0500145 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse70d4ee92013-12-06 16:19:54 +0000146 struct address_space *mapping = &sdp->sd_aspace;
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100147 struct gfs2_rgrpd *rgd;
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000148 int error;
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500149
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500150 spin_lock(&gl->gl_lockref.lock);
Bob Peterson39b0f1e2015-06-05 08:38:57 -0500151 rgd = gl->gl_object;
152 if (rgd)
153 gfs2_rgrp_brelse(rgd);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500154 spin_unlock(&gl->gl_lockref.lock);
Bob Peterson39b0f1e2015-06-05 08:38:57 -0500155
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000156 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
157 return;
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +0100158 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000159
Bob Peterson805c09072018-01-08 10:34:17 -0500160 gfs2_log_flush(sdp, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
161 GFS2_LFC_RGRP_GO_SYNC);
Steven Whitehouse70d4ee92013-12-06 16:19:54 +0000162 filemap_fdatawrite_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
163 error = filemap_fdatawait_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
164 mapping_set_error(mapping, error);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000165 gfs2_ail_empty_gl(gl);
Bob Peterson7c9ca622011-08-31 09:53:19 +0100166
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500167 spin_lock(&gl->gl_lockref.lock);
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100168 rgd = gl->gl_object;
169 if (rgd)
170 gfs2_free_clones(rgd);
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500171 spin_unlock(&gl->gl_lockref.lock);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000172}
173
174/**
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000175 * rgrp_go_inval - invalidate the metadata for this glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000176 * @gl: the glock
177 * @flags:
178 *
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000179 * We never used LM_ST_DEFERRED with resource groups, so that we
180 * should always see the metadata flag set here.
181 *
David Teiglandb3b94fa2006-01-16 16:50:04 +0000182 */
183
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000184static void rgrp_go_inval(struct gfs2_glock *gl, int flags)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000185{
Bob Peterson15562c42015-03-16 11:52:05 -0500186 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse70d4ee92013-12-06 16:19:54 +0000187 struct address_space *mapping = &sdp->sd_aspace;
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500188 struct gfs2_rgrpd *rgd = gfs2_glock2rgrp(gl);
Bob Peterson39b0f1e2015-06-05 08:38:57 -0500189
190 if (rgd)
191 gfs2_rgrp_brelse(rgd);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000192
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +0100193 WARN_ON_ONCE(!(flags & DIO_METADATA));
Steven Whitehouse70d4ee92013-12-06 16:19:54 +0000194 gfs2_assert_withdraw(sdp, !atomic_read(&gl->gl_ail_count));
Steven Whitehouse7005c3e2013-12-06 10:16:14 +0000195 truncate_inode_pages_range(mapping, gl->gl_vm.start, gl->gl_vm.end);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000196
Bob Peterson39b0f1e2015-06-05 08:38:57 -0500197 if (rgd)
Bob Petersoncf45b752008-01-31 10:31:39 -0600198 rgd->rd_flags &= ~GFS2_RDF_UPTODATE;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000199}
200
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500201static struct gfs2_inode *gfs2_glock2inode(struct gfs2_glock *gl)
202{
203 struct gfs2_inode *ip;
204
205 spin_lock(&gl->gl_lockref.lock);
206 ip = gl->gl_object;
207 if (ip)
208 set_bit(GIF_GLOP_PENDING, &ip->i_flags);
209 spin_unlock(&gl->gl_lockref.lock);
210 return ip;
211}
212
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500213struct gfs2_rgrpd *gfs2_glock2rgrp(struct gfs2_glock *gl)
214{
215 struct gfs2_rgrpd *rgd;
216
217 spin_lock(&gl->gl_lockref.lock);
218 rgd = gl->gl_object;
219 spin_unlock(&gl->gl_lockref.lock);
220
221 return rgd;
222}
223
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500224static void gfs2_clear_glop_pending(struct gfs2_inode *ip)
225{
226 if (!ip)
227 return;
228
229 clear_bit_unlock(GIF_GLOP_PENDING, &ip->i_flags);
230 wake_up_bit(&ip->i_flags, GIF_GLOP_PENDING);
231}
232
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233/**
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500234 * inode_go_sync - Sync the dirty data and/or metadata for an inode glock
235 * @gl: the glock protecting the inode
236 *
237 */
238
239static void inode_go_sync(struct gfs2_glock *gl)
240{
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500241 struct gfs2_inode *ip = gfs2_glock2inode(gl);
242 int isreg = ip && S_ISREG(ip->i_inode.i_mode);
Steven Whitehouse009d8512009-12-08 12:12:13 +0000243 struct address_space *metamapping = gfs2_glock2aspace(gl);
Steven Whitehouse3042a2cc2007-11-02 08:39:34 +0000244 int error;
245
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500246 if (isreg) {
Steven Whitehouse582d2f72013-12-19 11:04:14 +0000247 if (test_and_clear_bit(GIF_SW_PAGED, &ip->i_flags))
248 unmap_shared_mapping_range(ip->i_inode.i_mapping, 0, 0);
249 inode_dio_wait(&ip->i_inode);
250 }
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000251 if (!test_and_clear_bit(GLF_DIRTY, &gl->gl_flags))
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500252 goto out;
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500253
Steven Whitehouse8eae1ca02012-10-15 10:57:02 +0100254 GLOCK_BUG_ON(gl, gl->gl_state != LM_ST_EXCLUSIVE);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000255
Bob Peterson805c09072018-01-08 10:34:17 -0500256 gfs2_log_flush(gl->gl_name.ln_sbd, gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
257 GFS2_LFC_INODE_GO_SYNC);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000258 filemap_fdatawrite(metamapping);
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500259 if (isreg) {
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000260 struct address_space *mapping = ip->i_inode.i_mapping;
261 filemap_fdatawrite(mapping);
262 error = filemap_fdatawait(mapping);
263 mapping_set_error(mapping, error);
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500264 }
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000265 error = filemap_fdatawait(metamapping);
266 mapping_set_error(metamapping, error);
267 gfs2_ail_empty_gl(gl);
Steven Whitehouse52fcd112009-04-20 08:58:45 +0100268 /*
269 * Writeback of the data mapping may cause the dirty flag to be set
270 * so we have to clear it again here.
271 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +0100272 smp_mb__before_atomic();
Steven Whitehouse52fcd112009-04-20 08:58:45 +0100273 clear_bit(GLF_DIRTY, &gl->gl_flags);
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500274
275out:
276 gfs2_clear_glop_pending(ip);
Steven Whitehouseb5d32be2007-01-22 12:15:34 -0500277}
278
279/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000280 * inode_go_inval - prepare a inode glock to be released
281 * @gl: the glock
282 * @flags:
Geert Uytterhoeven6b49d1d2014-06-29 12:21:39 +0200283 *
284 * Normally we invalidate everything, but if we are moving into
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000285 * LM_ST_DEFERRED from LM_ST_SHARED or LM_ST_EXCLUSIVE then we
286 * can keep hold of the metadata, since it won't have changed.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 *
288 */
289
290static void inode_go_inval(struct gfs2_glock *gl, int flags)
291{
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500292 struct gfs2_inode *ip = gfs2_glock2inode(gl);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000293
Bob Peterson15562c42015-03-16 11:52:05 -0500294 gfs2_assert_withdraw(gl->gl_name.ln_sbd, !atomic_read(&gl->gl_ail_count));
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000295
296 if (flags & DIO_METADATA) {
Steven Whitehouse009d8512009-12-08 12:12:13 +0000297 struct address_space *mapping = gfs2_glock2aspace(gl);
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000298 truncate_inode_pages(mapping, 0);
Steven Whitehousec65f7fb2009-10-02 11:54:39 +0100299 if (ip) {
Steven Whitehouseb0041572006-11-23 10:51:34 -0500300 set_bit(GIF_INVALID, &ip->i_flags);
Steven Whitehousec65f7fb2009-10-02 11:54:39 +0100301 forget_all_cached_acls(&ip->i_inode);
Andreas Gruenbacherf39814f62015-12-24 11:09:40 -0500302 security_inode_invalidate_secctx(&ip->i_inode);
Steven Whitehouse17d539f2011-06-15 10:29:37 +0100303 gfs2_dir_hash_inval(ip);
Steven Whitehousec65f7fb2009-10-02 11:54:39 +0100304 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305 }
Steven Whitehouseb0041572006-11-23 10:51:34 -0500306
Bob Peterson15562c42015-03-16 11:52:05 -0500307 if (ip == GFS2_I(gl->gl_name.ln_sbd->sd_rindex)) {
Bob Petersonc1696fb2018-01-17 00:01:33 +0100308 gfs2_log_flush(gl->gl_name.ln_sbd, NULL,
Bob Peterson805c09072018-01-08 10:34:17 -0500309 GFS2_LOG_HEAD_FLUSH_NORMAL |
310 GFS2_LFC_INODE_GO_INVAL);
Bob Peterson15562c42015-03-16 11:52:05 -0500311 gl->gl_name.ln_sbd->sd_rindex_uptodate = 0;
Benjamin Marzinski1ce53362011-06-13 14:27:40 -0500312 }
Steven Whitehouse3cc3f712007-10-15 15:40:33 +0100313 if (ip && S_ISREG(ip->i_inode.i_mode))
Steven Whitehouseb0041572006-11-23 10:51:34 -0500314 truncate_inode_pages(ip->i_inode.i_mapping, 0);
Andreas Gruenbacher4fd1a572017-06-30 07:47:15 -0500315
316 gfs2_clear_glop_pending(ip);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000317}
318
319/**
320 * inode_go_demote_ok - Check to see if it's ok to unlock an inode glock
321 * @gl: the glock
322 *
323 * Returns: 1 if it's ok
324 */
325
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000326static int inode_go_demote_ok(const struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327{
Bob Peterson15562c42015-03-16 11:52:05 -0500328 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000329
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000330 if (sdp->sd_jindex == gl->gl_object || sdp->sd_rindex == gl->gl_object)
331 return 0;
Steven Whitehousebc015cb2011-01-19 09:30:01 +0000332
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000333 return 1;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000334}
335
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100336static int gfs2_dinode_in(struct gfs2_inode *ip, const void *buf)
337{
338 const struct gfs2_dinode *str = buf;
Deepa Dinamani95582b02018-05-08 19:36:02 -0700339 struct timespec64 atime;
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100340 u16 height, depth;
341
342 if (unlikely(ip->i_no_addr != be64_to_cpu(str->di_num.no_addr)))
343 goto corrupt;
344 ip->i_no_formal_ino = be64_to_cpu(str->di_num.no_formal_ino);
345 ip->i_inode.i_mode = be32_to_cpu(str->di_mode);
346 ip->i_inode.i_rdev = 0;
347 switch (ip->i_inode.i_mode & S_IFMT) {
348 case S_IFBLK:
349 case S_IFCHR:
350 ip->i_inode.i_rdev = MKDEV(be32_to_cpu(str->di_major),
351 be32_to_cpu(str->di_minor));
352 break;
Aliasgar Surti098b9c12019-10-04 10:55:29 -0500353 }
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100354
Eric W. Biedermand0546422013-01-31 22:08:10 -0800355 i_uid_write(&ip->i_inode, be32_to_cpu(str->di_uid));
356 i_gid_write(&ip->i_inode, be32_to_cpu(str->di_gid));
Andreas Gruenbachereebd2e82017-08-01 11:33:17 -0500357 set_nlink(&ip->i_inode, be32_to_cpu(str->di_nlink));
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100358 i_size_write(&ip->i_inode, be64_to_cpu(str->di_size));
359 gfs2_set_inode_blocks(&ip->i_inode, be64_to_cpu(str->di_blocks));
360 atime.tv_sec = be64_to_cpu(str->di_atime);
361 atime.tv_nsec = be32_to_cpu(str->di_atime_nsec);
Deepa Dinamani95582b02018-05-08 19:36:02 -0700362 if (timespec64_compare(&ip->i_inode.i_atime, &atime) < 0)
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100363 ip->i_inode.i_atime = atime;
364 ip->i_inode.i_mtime.tv_sec = be64_to_cpu(str->di_mtime);
365 ip->i_inode.i_mtime.tv_nsec = be32_to_cpu(str->di_mtime_nsec);
366 ip->i_inode.i_ctime.tv_sec = be64_to_cpu(str->di_ctime);
367 ip->i_inode.i_ctime.tv_nsec = be32_to_cpu(str->di_ctime_nsec);
368
369 ip->i_goal = be64_to_cpu(str->di_goal_meta);
370 ip->i_generation = be64_to_cpu(str->di_generation);
371
372 ip->i_diskflags = be32_to_cpu(str->di_flags);
Steven Whitehouse9964afb2011-06-16 14:06:55 +0100373 ip->i_eattr = be64_to_cpu(str->di_eattr);
374 /* i_diskflags and i_eattr must be set before gfs2_set_inode_flags() */
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100375 gfs2_set_inode_flags(&ip->i_inode);
376 height = be16_to_cpu(str->di_height);
377 if (unlikely(height > GFS2_MAX_META_HEIGHT))
378 goto corrupt;
379 ip->i_height = (u8)height;
380
381 depth = be16_to_cpu(str->di_depth);
382 if (unlikely(depth > GFS2_DIR_MAX_DEPTH))
383 goto corrupt;
384 ip->i_depth = (u8)depth;
385 ip->i_entries = be32_to_cpu(str->di_entries);
386
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100387 if (S_ISREG(ip->i_inode.i_mode))
388 gfs2_set_aops(&ip->i_inode);
389
390 return 0;
391corrupt:
392 gfs2_consist_inode(ip);
393 return -EIO;
394}
395
396/**
397 * gfs2_inode_refresh - Refresh the incore copy of the dinode
398 * @ip: The GFS2 inode
399 *
400 * Returns: errno
401 */
402
403int gfs2_inode_refresh(struct gfs2_inode *ip)
404{
405 struct buffer_head *dibh;
406 int error;
407
408 error = gfs2_meta_inode_buffer(ip, &dibh);
409 if (error)
410 return error;
411
Steven Whitehoused4b2cf12011-05-09 13:49:59 +0100412 error = gfs2_dinode_in(ip, dibh->b_data);
413 brelse(dibh);
414 clear_bit(GIF_INVALID, &ip->i_flags);
415
416 return error;
417}
418
419/**
David Teiglandb3b94fa2006-01-16 16:50:04 +0000420 * inode_go_lock - operation done after an inode lock is locked by a process
421 * @gl: the glock
422 * @flags:
423 *
424 * Returns: errno
425 */
426
427static int inode_go_lock(struct gfs2_holder *gh)
428{
429 struct gfs2_glock *gl = gh->gh_gl;
Bob Peterson15562c42015-03-16 11:52:05 -0500430 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500431 struct gfs2_inode *ip = gl->gl_object;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000432 int error = 0;
433
Bob Peterson091806ed2008-04-29 12:35:48 -0500434 if (!ip || (gh->gh_flags & GL_SKIP))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435 return 0;
436
Steven Whitehousebfded272006-11-01 16:05:38 -0500437 if (test_bit(GIF_INVALID, &ip->i_flags)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000438 error = gfs2_inode_refresh(ip);
439 if (error)
440 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 }
442
Steven Whitehouse582d2f72013-12-19 11:04:14 +0000443 if (gh->gh_state != LM_ST_DEFERRED)
444 inode_dio_wait(&ip->i_inode);
445
Steven Whitehouse383f01f2008-11-04 10:05:22 +0000446 if ((ip->i_diskflags & GFS2_DIF_TRUNC_IN_PROG) &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000447 (gl->gl_state == LM_ST_EXCLUSIVE) &&
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000448 (gh->gh_state == LM_ST_EXCLUSIVE)) {
449 spin_lock(&sdp->sd_trunc_lock);
450 if (list_empty(&ip->i_trunc_list))
Wang Xiboe7cb5502017-07-21 07:40:59 -0500451 list_add(&ip->i_trunc_list, &sdp->sd_trunc_list);
Steven Whitehouse813e0c42008-11-18 13:38:48 +0000452 spin_unlock(&sdp->sd_trunc_lock);
453 wake_up(&sdp->sd_quota_wait);
454 return 1;
455 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000456
457 return error;
458}
459
460/**
Steven Whitehouse6802e342008-05-21 17:03:22 +0100461 * inode_go_dump - print information about an inode
462 * @seq: The iterator
463 * @ip: the inode
Bob Peterson3792ce92019-05-09 09:21:48 -0500464 * @fs_id_buf: file system id (may be empty)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100465 *
Steven Whitehouse6802e342008-05-21 17:03:22 +0100466 */
467
Bob Peterson3792ce92019-05-09 09:21:48 -0500468static void inode_go_dump(struct seq_file *seq, struct gfs2_glock *gl,
469 const char *fs_id_buf)
Steven Whitehouse6802e342008-05-21 17:03:22 +0100470{
Bob Peterson27a2660f2018-04-18 12:05:01 -0700471 struct gfs2_inode *ip = gl->gl_object;
472 struct inode *inode = &ip->i_inode;
473 unsigned long nrpages;
474
Steven Whitehouse6802e342008-05-21 17:03:22 +0100475 if (ip == NULL)
Steven Whitehouseac3beb62014-01-16 10:31:13 +0000476 return;
Bob Peterson27a2660f2018-04-18 12:05:01 -0700477
478 xa_lock_irq(&inode->i_data.i_pages);
479 nrpages = inode->i_data.nrpages;
480 xa_unlock_irq(&inode->i_data.i_pages);
481
Bob Peterson3792ce92019-05-09 09:21:48 -0500482 gfs2_print_dbg(seq, "%s I: n:%llu/%llu t:%u f:0x%02lx d:0x%08x s:%llu "
483 "p:%lu\n", fs_id_buf,
Steven Whitehouse6802e342008-05-21 17:03:22 +0100484 (unsigned long long)ip->i_no_formal_ino,
485 (unsigned long long)ip->i_no_addr,
Steven Whitehousefa75ced2008-11-10 10:10:12 +0000486 IF2DT(ip->i_inode.i_mode), ip->i_flags,
487 (unsigned int)ip->i_diskflags,
Bob Peterson27a2660f2018-04-18 12:05:01 -0700488 (unsigned long long)i_size_read(inode), nrpages);
Steven Whitehouse6802e342008-05-21 17:03:22 +0100489}
490
491/**
Benjamin Marzinski24972552014-05-01 22:26:55 -0500492 * freeze_go_sync - promote/demote the freeze glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000493 * @gl: the glock
494 * @state: the requested state
495 * @flags:
496 *
497 */
498
Benjamin Marzinski24972552014-05-01 22:26:55 -0500499static void freeze_go_sync(struct gfs2_glock *gl)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000500{
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600501 int error = 0;
Bob Peterson15562c42015-03-16 11:52:05 -0500502 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000503
Benjamin Marzinski24972552014-05-01 22:26:55 -0500504 if (gl->gl_state == LM_ST_SHARED &&
David Teiglandb3b94fa2006-01-16 16:50:04 +0000505 test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600506 atomic_set(&sdp->sd_freeze_state, SFS_STARTING_FREEZE);
507 error = freeze_super(sdp->sd_vfs);
508 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500509 fs_info(sdp, "GFS2: couldn't freeze filesystem: %d\n",
510 error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600511 gfs2_assert_withdraw(sdp, 0);
512 }
513 queue_work(gfs2_freeze_wq, &sdp->sd_freeze_work);
Bob Peterson805c09072018-01-08 10:34:17 -0500514 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_FREEZE |
515 GFS2_LFC_FREEZE_GO_SYNC);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000516 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000517}
518
519/**
Benjamin Marzinski24972552014-05-01 22:26:55 -0500520 * freeze_go_xmote_bh - After promoting/demoting the freeze glock
David Teiglandb3b94fa2006-01-16 16:50:04 +0000521 * @gl: the glock
522 *
523 */
524
Benjamin Marzinski24972552014-05-01 22:26:55 -0500525static int freeze_go_xmote_bh(struct gfs2_glock *gl, struct gfs2_holder *gh)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000526{
Bob Peterson15562c42015-03-16 11:52:05 -0500527 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400528 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500529 struct gfs2_glock *j_gl = ip->i_gl;
Al Viro55167622006-10-13 21:47:13 -0400530 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000531 int error;
532
Steven Whitehouse6802e342008-05-21 17:03:22 +0100533 if (test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) {
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500534 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000535
Abhi Dasf4686c22019-05-02 14:17:40 -0500536 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537 if (error)
538 gfs2_consist(sdp);
539 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT))
540 gfs2_consist(sdp);
541
542 /* Initialize some head of the log stuff */
Bob Petersoneb43e662019-11-14 09:52:15 -0500543 if (!gfs2_withdrawn(sdp)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000544 sdp->sd_log_sequence = head.lh_sequence + 1;
545 gfs2_log_pointers_init(sdp, head.lh_blkno);
546 }
547 }
Steven Whitehouse6802e342008-05-21 17:03:22 +0100548 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000549}
550
551/**
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000552 * trans_go_demote_ok
553 * @gl: the glock
554 *
555 * Always returns 0
556 */
557
Benjamin Marzinski24972552014-05-01 22:26:55 -0500558static int freeze_go_demote_ok(const struct gfs2_glock *gl)
Steven Whitehouse97cc10252008-11-20 13:39:47 +0000559{
560 return 0;
561}
562
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500563/**
564 * iopen_go_callback - schedule the dcache entry for the inode to be deleted
565 * @gl: the glock
566 *
Andreas Gruenbacherf3dd1642015-10-29 10:58:09 -0500567 * gl_lockref.lock lock is held while calling this
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500568 */
Steven Whitehouse81ffbf62013-04-10 10:26:55 +0100569static void iopen_go_callback(struct gfs2_glock *gl, bool remote)
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500570{
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500571 struct gfs2_inode *ip = gl->gl_object;
Bob Peterson15562c42015-03-16 11:52:05 -0500572 struct gfs2_sbd *sdp = gl->gl_name.ln_sbd;
Steven Whitehouse001e8e82011-03-30 14:17:51 +0100573
David Howellsbc98a422017-07-17 08:45:34 +0100574 if (!remote || sb_rdonly(sdp->sd_vfs))
Steven Whitehouse001e8e82011-03-30 14:17:51 +0100575 return;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500576
577 if (gl->gl_demote_state == LM_ST_UNLOCKED &&
Steven Whitehouse009d8512009-12-08 12:12:13 +0000578 gl->gl_state == LM_ST_SHARED && ip) {
Steven Whitehousee66cf162013-10-15 15:18:08 +0100579 gl->gl_lockref.count++;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500580 if (queue_work(gfs2_delete_workqueue, &gl->gl_delete) == 0)
Steven Whitehousee66cf162013-10-15 15:18:08 +0100581 gl->gl_lockref.count--;
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500582 }
583}
584
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400585const struct gfs2_glock_operations gfs2_meta_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400586 .go_type = LM_TYPE_META,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000587};
588
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400589const struct gfs2_glock_operations gfs2_inode_glops = {
Bob Peterson06dfc302012-10-24 14:41:05 -0400590 .go_sync = inode_go_sync,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000591 .go_inval = inode_go_inval,
592 .go_demote_ok = inode_go_demote_ok,
593 .go_lock = inode_go_lock,
Steven Whitehouse6802e342008-05-21 17:03:22 +0100594 .go_dump = inode_go_dump,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400595 .go_type = LM_TYPE_INODE,
Bob Petersone7ccaf52015-06-12 13:15:54 -0500596 .go_flags = GLOF_ASPACE | GLOF_LRU,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000597};
598
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400599const struct gfs2_glock_operations gfs2_rgrp_glops = {
Bob Peterson06dfc302012-10-24 14:41:05 -0400600 .go_sync = rgrp_go_sync,
Steven Whitehouse6bac2432009-03-09 09:03:51 +0000601 .go_inval = rgrp_go_inval,
Bob Peterson7c9ca622011-08-31 09:53:19 +0100602 .go_lock = gfs2_rgrp_go_lock,
603 .go_unlock = gfs2_rgrp_go_unlock,
Steven Whitehouse09010972009-05-20 10:48:47 +0100604 .go_dump = gfs2_rgrp_dump,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400605 .go_type = LM_TYPE_RGRP,
Steven Whitehouse70d4ee92013-12-06 16:19:54 +0000606 .go_flags = GLOF_LVB,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000607};
608
Benjamin Marzinski24972552014-05-01 22:26:55 -0500609const struct gfs2_glock_operations gfs2_freeze_glops = {
610 .go_sync = freeze_go_sync,
611 .go_xmote_bh = freeze_go_xmote_bh,
612 .go_demote_ok = freeze_go_demote_ok,
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400613 .go_type = LM_TYPE_NONDISK,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000614};
615
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400616const struct gfs2_glock_operations gfs2_iopen_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400617 .go_type = LM_TYPE_IOPEN,
Benjamin Marzinskib94a1702009-07-23 18:52:34 -0500618 .go_callback = iopen_go_callback,
Bob Petersone7ccaf52015-06-12 13:15:54 -0500619 .go_flags = GLOF_LRU,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000620};
621
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400622const struct gfs2_glock_operations gfs2_flock_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400623 .go_type = LM_TYPE_FLOCK,
Bob Petersone7ccaf52015-06-12 13:15:54 -0500624 .go_flags = GLOF_LRU,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000625};
626
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400627const struct gfs2_glock_operations gfs2_nondisk_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400628 .go_type = LM_TYPE_NONDISK,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000629};
630
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400631const struct gfs2_glock_operations gfs2_quota_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400632 .go_type = LM_TYPE_QUOTA,
Bob Petersone7ccaf52015-06-12 13:15:54 -0500633 .go_flags = GLOF_LVB | GLOF_LRU,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000634};
635
Steven Whitehouse8fb4b532006-08-30 09:30:00 -0400636const struct gfs2_glock_operations gfs2_journal_glops = {
Steven Whitehouseea67eed2006-09-05 10:53:09 -0400637 .go_type = LM_TYPE_JOURNAL,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638};
639
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000640const struct gfs2_glock_operations *gfs2_glops_list[] = {
641 [LM_TYPE_META] = &gfs2_meta_glops,
642 [LM_TYPE_INODE] = &gfs2_inode_glops,
643 [LM_TYPE_RGRP] = &gfs2_rgrp_glops,
Steven Whitehouse64d576b2009-02-12 13:31:58 +0000644 [LM_TYPE_IOPEN] = &gfs2_iopen_glops,
645 [LM_TYPE_FLOCK] = &gfs2_flock_glops,
646 [LM_TYPE_NONDISK] = &gfs2_nondisk_glops,
647 [LM_TYPE_QUOTA] = &gfs2_quota_glops,
648 [LM_TYPE_JOURNAL] = &gfs2_journal_glops,
649};
650