blob: d79a0357384036dee209d7a41b664db8609e0071 [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 Petersonda6dd402007-12-11 18:49:21 -06004 * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved.
David Teiglandb3b94fa2006-01-16 16:50:04 +00005 */
6
Joe Perchesd77d1b52014-03-06 12:10:45 -08007#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01009#include <linux/bio.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010010#include <linux/sched/signal.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000011#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010015#include <linux/statfs.h>
16#include <linux/seq_file.h>
17#include <linux/mount.h>
18#include <linux/kthread.h>
19#include <linux/delay.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050020#include <linux/gfs2_ondisk.h>
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010021#include <linux/crc32.h>
22#include <linux/time.h>
Steven Whitehousee4027462010-01-25 11:20:19 +000023#include <linux/wait.h>
Christoph Hellwiga9185b42010-03-05 09:21:37 +010024#include <linux/writeback.h>
Steven Whitehouse4667a0e2011-04-18 14:18:09 +010025#include <linux/backing-dev.h>
Benjamin Marzinski2e60d762014-11-13 20:42:04 -060026#include <linux/kernel.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000027
28#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050029#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000030#include "bmap.h"
31#include "dir.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000032#include "glock.h"
33#include "glops.h"
34#include "inode.h"
35#include "log.h"
36#include "meta_io.h"
37#include "quota.h"
38#include "recovery.h"
39#include "rgrp.h"
40#include "super.h"
41#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050042#include "util.h"
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010043#include "sys.h"
Steven Whitehouse307cf6e2009-08-26 18:51:04 +010044#include "xattr.h"
Abhi Dasf4686c22019-05-02 14:17:40 -050045#include "lops.h"
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +010046
Steven Whitehousefefc03b2008-12-19 15:32:06 +000047/**
48 * gfs2_jindex_free - Clear all the journal index information
49 * @sdp: The GFS2 superblock
50 *
51 */
52
53void gfs2_jindex_free(struct gfs2_sbd *sdp)
54{
Steven Whitehouseb50f2272014-03-03 13:35:57 +000055 struct list_head list;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000056 struct gfs2_jdesc *jd;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000057
58 spin_lock(&sdp->sd_jindex_spin);
59 list_add(&list, &sdp->sd_jindex_list);
60 list_del_init(&sdp->sd_jindex_list);
61 sdp->sd_journals = 0;
62 spin_unlock(&sdp->sd_jindex_spin);
63
Bob Peterson601ef0d2020-01-28 20:23:45 +010064 sdp->sd_jdesc = NULL;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000065 while (!list_empty(&list)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +010066 jd = list_first_entry(&list, struct gfs2_jdesc, jd_list);
Steven Whitehouseb50f2272014-03-03 13:35:57 +000067 gfs2_free_journal_extents(jd);
Steven Whitehousefefc03b2008-12-19 15:32:06 +000068 list_del(&jd->jd_list);
69 iput(jd->jd_inode);
Bob Peterson601ef0d2020-01-28 20:23:45 +010070 jd->jd_inode = NULL;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000071 kfree(jd);
72 }
73}
74
David Teiglandb3b94fa2006-01-16 16:50:04 +000075static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
76{
77 struct gfs2_jdesc *jd;
78 int found = 0;
79
80 list_for_each_entry(jd, head, jd_list) {
81 if (jd->jd_jid == jid) {
82 found = 1;
83 break;
84 }
85 }
86
87 if (!found)
88 jd = NULL;
89
90 return jd;
91}
92
93struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
94{
95 struct gfs2_jdesc *jd;
96
97 spin_lock(&sdp->sd_jindex_spin);
98 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
99 spin_unlock(&sdp->sd_jindex_spin);
100
101 return jd;
102}
103
David Teiglandb3b94fa2006-01-16 16:50:04 +0000104int gfs2_jdesc_check(struct gfs2_jdesc *jd)
105{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400106 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
107 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100108 u64 size = i_size_read(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000109
Fabian Frederick47a9a522016-08-02 12:05:27 -0500110 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000112
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100113 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
114
115 if (gfs2_write_alloc_required(ip, 0, size)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000116 gfs2_consist_inode(ip);
Bob Peterson461cb412010-06-24 19:21:20 -0400117 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118 }
119
Bob Peterson461cb412010-06-24 19:21:20 -0400120 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000121}
122
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000123static int init_threads(struct gfs2_sbd *sdp)
124{
125 struct task_struct *p;
126 int error = 0;
127
128 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
129 if (IS_ERR(p)) {
130 error = PTR_ERR(p);
131 fs_err(sdp, "can't start logd thread: %d\n", error);
132 return error;
133 }
134 sdp->sd_logd_process = p;
135
136 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
137 if (IS_ERR(p)) {
138 error = PTR_ERR(p);
139 fs_err(sdp, "can't start quotad thread: %d\n", error);
140 goto fail;
141 }
142 sdp->sd_quotad_process = p;
143 return 0;
144
145fail:
146 kthread_stop(sdp->sd_logd_process);
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600147 sdp->sd_logd_process = NULL;
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000148 return error;
149}
150
David Teiglandb3b94fa2006-01-16 16:50:04 +0000151/**
152 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
153 * @sdp: the filesystem
154 *
155 * Returns: errno
156 */
157
158int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
159{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400160 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500161 struct gfs2_glock *j_gl = ip->i_gl;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600162 struct gfs2_holder freeze_gh;
Al Viro55167622006-10-13 21:47:13 -0400163 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000164 int error;
165
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000166 error = init_threads(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000167 if (error)
168 return error;
169
Bob Peterson623ba662020-06-25 13:30:52 -0500170 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_EXACT,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600171 &freeze_gh);
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000172 if (error)
173 goto fail_threads;
174
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500175 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100176 if (gfs2_withdrawn(sdp)) {
177 error = -EIO;
178 goto fail;
179 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180
Abhi Dasf4686c22019-05-02 14:17:40 -0500181 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100182 if (error || gfs2_withdrawn(sdp))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000183 goto fail;
184
185 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
186 gfs2_consist(sdp);
187 error = -EIO;
188 goto fail;
189 }
190
191 /* Initialize some head of the log stuff */
192 sdp->sd_log_sequence = head.lh_sequence + 1;
193 gfs2_log_pointers_init(sdp, head.lh_blkno);
194
David Teiglandb3b94fa2006-01-16 16:50:04 +0000195 error = gfs2_quota_init(sdp);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100196 if (error || gfs2_withdrawn(sdp))
Steven Whitehousea91ea692006-09-04 12:04:26 -0400197 goto fail;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000198
199 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
200
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600201 gfs2_glock_dq_uninit(&freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000202
203 return 0;
204
Steven Whitehousea91ea692006-09-04 12:04:26 -0400205fail:
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600206 gfs2_glock_dq_uninit(&freeze_gh);
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000207fail_threads:
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600208 if (sdp->sd_quotad_process)
209 kthread_stop(sdp->sd_quotad_process);
210 sdp->sd_quotad_process = NULL;
211 if (sdp->sd_logd_process)
212 kthread_stop(sdp->sd_logd_process);
213 sdp->sd_logd_process = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000214 return error;
215}
216
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500217void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100218{
219 const struct gfs2_statfs_change *str = buf;
220
221 sc->sc_total = be64_to_cpu(str->sc_total);
222 sc->sc_free = be64_to_cpu(str->sc_free);
223 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
224}
225
226static void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
227{
228 struct gfs2_statfs_change *str = buf;
229
230 str->sc_total = cpu_to_be64(sc->sc_total);
231 str->sc_free = cpu_to_be64(sc->sc_free);
232 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
233}
234
David Teiglandb3b94fa2006-01-16 16:50:04 +0000235int gfs2_statfs_init(struct gfs2_sbd *sdp)
236{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400237 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400238 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400239 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400240 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000241 struct buffer_head *m_bh, *l_bh;
242 struct gfs2_holder gh;
243 int error;
244
245 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
246 &gh);
247 if (error)
248 return error;
249
250 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
251 if (error)
252 goto out;
253
254 if (sdp->sd_args.ar_spectator) {
255 spin_lock(&sdp->sd_statfs_spin);
256 gfs2_statfs_change_in(m_sc, m_bh->b_data +
257 sizeof(struct gfs2_dinode));
258 spin_unlock(&sdp->sd_statfs_spin);
259 } else {
260 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
261 if (error)
262 goto out_m_bh;
263
264 spin_lock(&sdp->sd_statfs_spin);
265 gfs2_statfs_change_in(m_sc, m_bh->b_data +
266 sizeof(struct gfs2_dinode));
267 gfs2_statfs_change_in(l_sc, l_bh->b_data +
268 sizeof(struct gfs2_dinode));
269 spin_unlock(&sdp->sd_statfs_spin);
270
271 brelse(l_bh);
272 }
273
Steven Whitehousea91ea692006-09-04 12:04:26 -0400274out_m_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000275 brelse(m_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400276out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000278 return 0;
279}
280
Steven Whitehousecd915492006-09-04 12:49:07 -0400281void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
282 s64 dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000283{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400284 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400285 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500286 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000287 struct buffer_head *l_bh;
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500288 s64 x, y;
289 int need_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000290 int error;
291
292 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
293 if (error)
294 return;
295
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000296 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000297
298 spin_lock(&sdp->sd_statfs_spin);
299 l_sc->sc_total += total;
300 l_sc->sc_free += free;
301 l_sc->sc_dinodes += dinodes;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400302 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500303 if (sdp->sd_args.ar_statfs_percent) {
304 x = 100 * l_sc->sc_free;
305 y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
306 if (x >= y || x <= -y)
307 need_sync = 1;
308 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000309 spin_unlock(&sdp->sd_statfs_spin);
310
311 brelse(l_bh);
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500312 if (need_sync)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500313 gfs2_wake_up_statfs(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000314}
315
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500316void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh,
317 struct buffer_head *l_bh)
318{
319 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
320 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
321 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
322 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
323
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000324 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
Bob Peterson901c6c62015-03-11 09:52:31 -0500325 gfs2_trans_add_meta(m_ip->i_gl, m_bh);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500326
327 spin_lock(&sdp->sd_statfs_spin);
328 m_sc->sc_total += l_sc->sc_total;
329 m_sc->sc_free += l_sc->sc_free;
330 m_sc->sc_dinodes += l_sc->sc_dinodes;
331 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
332 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
333 0, sizeof(struct gfs2_statfs_change));
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500334 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
Bob Peterson901c6c62015-03-11 09:52:31 -0500335 spin_unlock(&sdp->sd_statfs_spin);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500336}
337
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100338int gfs2_statfs_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000339{
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100340 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400341 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
342 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400343 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
344 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000345 struct gfs2_holder gh;
346 struct buffer_head *m_bh, *l_bh;
347 int error;
348
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600349 sb_start_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000350 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
351 &gh);
352 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600353 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000354
355 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
356 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600357 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000358
359 spin_lock(&sdp->sd_statfs_spin);
360 gfs2_statfs_change_in(m_sc, m_bh->b_data +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400361 sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
363 spin_unlock(&sdp->sd_statfs_spin);
364 goto out_bh;
365 }
366 spin_unlock(&sdp->sd_statfs_spin);
367
368 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
369 if (error)
370 goto out_bh;
371
372 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
373 if (error)
374 goto out_bh2;
375
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500376 update_statfs(sdp, m_bh, l_bh);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500377 sdp->sd_statfs_force_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000378
379 gfs2_trans_end(sdp);
380
Steven Whitehousea91ea692006-09-04 12:04:26 -0400381out_bh2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000382 brelse(l_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400383out_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384 brelse(m_bh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600385out_unlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000386 gfs2_glock_dq_uninit(&gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600387out:
388 sb_end_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000389 return error;
390}
391
David Teiglandb3b94fa2006-01-16 16:50:04 +0000392struct lfcc {
393 struct list_head list;
394 struct gfs2_holder gh;
395};
396
397/**
398 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
399 * journals are clean
400 * @sdp: the file system
401 * @state: the state to put the transaction lock into
402 * @t_gh: the hold on the transaction lock
403 *
404 * Returns: errno
405 */
406
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500407static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500409 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000410 struct gfs2_jdesc *jd;
411 struct lfcc *lfcc;
412 LIST_HEAD(list);
Al Viro55167622006-10-13 21:47:13 -0400413 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 int error;
415
David Teiglandb3b94fa2006-01-16 16:50:04 +0000416 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
417 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
418 if (!lfcc) {
419 error = -ENOMEM;
420 goto out;
421 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400422 ip = GFS2_I(jd->jd_inode);
423 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000424 if (error) {
425 kfree(lfcc);
426 goto out;
427 }
428 list_add(&lfcc->list, &list);
429 }
430
Benjamin Marzinski24972552014-05-01 22:26:55 -0500431 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
Bob Peterson623ba662020-06-25 13:30:52 -0500432 0, &sdp->sd_freeze_gh);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500433 if (error)
434 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000435
436 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
437 error = gfs2_jdesc_check(jd);
438 if (error)
439 break;
Abhi Dasf4686c22019-05-02 14:17:40 -0500440 error = gfs2_find_jhead(jd, &lh, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000441 if (error)
442 break;
443 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
444 error = -EBUSY;
445 break;
446 }
447 }
448
449 if (error)
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500450 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451
Steven Whitehousea91ea692006-09-04 12:04:26 -0400452out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 while (!list_empty(&list)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +0100454 lfcc = list_first_entry(&list, struct lfcc, list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000455 list_del(&lfcc->list);
456 gfs2_glock_dq_uninit(&lfcc->gh);
457 kfree(lfcc);
458 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000459 return error;
460}
461
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100462void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
463{
464 struct gfs2_dinode *str = buf;
465
466 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
467 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
468 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
469 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
470 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
471 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
Eric W. Biedermand0546422013-01-31 22:08:10 -0800472 str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
473 str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100474 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
475 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
476 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
477 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
478 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
479 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
480
481 str->di_goal_meta = cpu_to_be64(ip->i_goal);
482 str->di_goal_data = cpu_to_be64(ip->i_goal);
483 str->di_generation = cpu_to_be64(ip->i_generation);
484
485 str->di_flags = cpu_to_be32(ip->i_diskflags);
486 str->di_height = cpu_to_be16(ip->i_height);
487 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
488 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
489 GFS2_FORMAT_DE : 0);
490 str->di_depth = cpu_to_be16(ip->i_depth);
491 str->di_entries = cpu_to_be32(ip->i_entries);
492
493 str->di_eattr = cpu_to_be64(ip->i_eattr);
494 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
495 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
496 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
497}
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100498
499/**
500 * gfs2_write_inode - Make sure the inode is stable on the disk
501 * @inode: The inode
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100502 * @wbc: The writeback control structure
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100503 *
504 * Returns: errno
505 */
506
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100507static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100508{
509 struct gfs2_inode *ip = GFS2_I(inode);
510 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100511 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100512 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100513 int ret = 0;
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200514 bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100515
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200516 if (flush_all)
Bob Petersonc1696fb2018-01-17 00:01:33 +0100517 gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
Bob Peterson805c09072018-01-08 10:34:17 -0500518 GFS2_LOG_HEAD_FLUSH_NORMAL |
519 GFS2_LFC_WRITE_INODE);
Tejun Heoa88a3412015-05-22 17:13:28 -0400520 if (bdi->wb.dirty_exceeded)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100521 gfs2_ail1_flush(sdp, wbc);
Steven Whitehouse1d4ec642011-08-02 13:13:20 +0100522 else
523 filemap_fdatawrite(metamapping);
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200524 if (flush_all)
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100525 ret = filemap_fdatawait(metamapping);
526 if (ret)
527 mark_inode_dirty_sync(inode);
Abhi Das957a7ac2018-01-30 10:00:09 -0700528 else {
529 spin_lock(&inode->i_lock);
530 if (!(inode->i_flags & I_DIRTY))
531 gfs2_ordered_del_inode(ip);
532 spin_unlock(&inode->i_lock);
533 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100534 return ret;
535}
536
537/**
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100538 * gfs2_dirty_inode - check for atime updates
539 * @inode: The inode in question
540 * @flags: The type of dirty
541 *
542 * Unfortunately it can be called under any combination of inode
543 * glock and transaction lock, so we have to check carefully.
544 *
545 * At the moment this deals only with atime - it should be possible
546 * to expand that role in future, once a review of the locking has
547 * been carried out.
548 */
549
550static void gfs2_dirty_inode(struct inode *inode, int flags)
551{
552 struct gfs2_inode *ip = GFS2_I(inode);
553 struct gfs2_sbd *sdp = GFS2_SB(inode);
554 struct buffer_head *bh;
555 struct gfs2_holder gh;
556 int need_unlock = 0;
557 int need_endtrans = 0;
558 int ret;
559
Christoph Hellwig0e11f642018-02-21 07:54:49 -0800560 if (!(flags & I_DIRTY_INODE))
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100561 return;
Bob Petersoneb43e662019-11-14 09:52:15 -0500562 if (unlikely(gfs2_withdrawn(sdp)))
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500563 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100564 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
565 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
566 if (ret) {
567 fs_err(sdp, "dirty_inode: glock %d\n", ret);
568 return;
569 }
570 need_unlock = 1;
Benjamin Marzinski3d162682012-11-06 00:49:28 -0600571 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
572 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100573
574 if (current->journal_info == NULL) {
575 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
576 if (ret) {
577 fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
578 goto out;
579 }
580 need_endtrans = 1;
581 }
582
583 ret = gfs2_meta_inode_buffer(ip, &bh);
584 if (ret == 0) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000585 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100586 gfs2_dinode_out(ip, bh->b_data);
587 brelse(bh);
588 }
589
590 if (need_endtrans)
591 gfs2_trans_end(sdp);
592out:
593 if (need_unlock)
594 gfs2_glock_dq_uninit(&gh);
595}
596
597/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100598 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
599 * @sdp: the filesystem
600 *
601 * Returns: errno
602 */
603
Andrew Price1f52aa02019-03-27 14:46:00 +0000604int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100605{
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600606 struct gfs2_holder freeze_gh;
Bob Peterson601ef0d2020-01-28 20:23:45 +0100607 int error = 0;
608 int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100609
Bob Peterson601ef0d2020-01-28 20:23:45 +0100610 gfs2_holder_mark_uninitialized(&freeze_gh);
611 if (sdp->sd_freeze_gl &&
612 !gfs2_glock_is_locked_by_me(sdp->sd_freeze_gl)) {
613 if (!log_write_allowed) {
614 error = gfs2_glock_nq_init(sdp->sd_freeze_gl,
Bob Peterson623ba662020-06-25 13:30:52 -0500615 LM_ST_SHARED,
616 LM_FLAG_TRY | GL_EXACT,
617 &freeze_gh);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100618 if (error == GLR_TRYFAILED)
619 error = 0;
620 } else {
621 error = gfs2_glock_nq_init(sdp->sd_freeze_gl,
Bob Peterson623ba662020-06-25 13:30:52 -0500622 LM_ST_SHARED, GL_EXACT,
Bob Peterson601ef0d2020-01-28 20:23:45 +0100623 &freeze_gh);
624 if (error && !gfs2_withdrawn(sdp))
625 return error;
626 }
627 }
Benjamin Marzinski24972552014-05-01 22:26:55 -0500628
Andreas Gruenbachera0e3cc62020-01-16 20:12:26 +0100629 gfs2_flush_delete_work(sdp);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100630 if (!log_write_allowed && current == sdp->sd_quotad_process)
631 fs_warn(sdp, "The quotad daemon is withdrawing.\n");
632 else if (sdp->sd_quotad_process)
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600633 kthread_stop(sdp->sd_quotad_process);
634 sdp->sd_quotad_process = NULL;
Bob Peterson601ef0d2020-01-28 20:23:45 +0100635
636 if (!log_write_allowed && current == sdp->sd_logd_process)
637 fs_warn(sdp, "The logd daemon is withdrawing.\n");
638 else if (sdp->sd_logd_process)
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600639 kthread_stop(sdp->sd_logd_process);
640 sdp->sd_logd_process = NULL;
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000641
Bob Peterson601ef0d2020-01-28 20:23:45 +0100642 if (log_write_allowed) {
643 gfs2_quota_sync(sdp->sd_vfs, 0);
644 gfs2_statfs_sync(sdp->sd_vfs, 0);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100645
Bob Peterson601ef0d2020-01-28 20:23:45 +0100646 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
647 GFS2_LFC_MAKE_FS_RO);
648 wait_event(sdp->sd_reserving_log_wait,
649 atomic_read(&sdp->sd_reserving_log) == 0);
650 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) ==
651 sdp->sd_jdesc->jd_blocks);
652 } else {
653 wait_event_timeout(sdp->sd_reserving_log_wait,
654 atomic_read(&sdp->sd_reserving_log) == 0,
655 HZ * 5);
656 }
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500657 if (gfs2_holder_initialized(&freeze_gh))
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600658 gfs2_glock_dq_uninit(&freeze_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100659
660 gfs2_quota_cleanup(sdp);
661
Bob Peterson601ef0d2020-01-28 20:23:45 +0100662 if (!log_write_allowed)
663 sdp->sd_vfs->s_flags |= SB_RDONLY;
664
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100665 return error;
666}
667
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100668/**
669 * gfs2_put_super - Unmount the filesystem
670 * @sb: The VFS superblock
671 *
672 */
673
674static void gfs2_put_super(struct super_block *sb)
675{
676 struct gfs2_sbd *sdp = sb->s_fs_info;
677 int error;
678 struct gfs2_jdesc *jd;
679
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100680 /* No more recovery requests */
681 set_bit(SDF_NORECOVERY, &sdp->sd_flags);
682 smp_mb();
683
684 /* Wait on outstanding recovery */
685restart:
686 spin_lock(&sdp->sd_jindex_spin);
687 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
688 if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
689 continue;
690 spin_unlock(&sdp->sd_jindex_spin);
691 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
NeilBrown74316202014-07-07 15:16:04 +1000692 TASK_UNINTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100693 goto restart;
694 }
695 spin_unlock(&sdp->sd_jindex_spin);
696
David Howellsbc98a422017-07-17 08:45:34 +0100697 if (!sb_rdonly(sb)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100698 error = gfs2_make_fs_ro(sdp);
699 if (error)
700 gfs2_io_error(sdp);
701 }
702 /* At this point, we're through modifying the disk */
703
704 /* Release stuff */
705
706 iput(sdp->sd_jindex);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100707 iput(sdp->sd_statfs_inode);
708 iput(sdp->sd_rindex);
709 iput(sdp->sd_quota_inode);
710
711 gfs2_glock_put(sdp->sd_rename_gl);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500712 gfs2_glock_put(sdp->sd_freeze_gl);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100713
714 if (!sdp->sd_args.ar_spectator) {
Bob Peterson601ef0d2020-01-28 20:23:45 +0100715 if (gfs2_holder_initialized(&sdp->sd_journal_gh))
716 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
717 if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
718 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100719 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
720 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100721 iput(sdp->sd_sc_inode);
722 iput(sdp->sd_qc_inode);
723 }
724
725 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
726 gfs2_clear_rgrpd(sdp);
727 gfs2_jindex_free(sdp);
728 /* Take apart glock structures and buffer lists */
729 gfs2_gl_hash_clear(sdp);
Bob Petersonb2fb7da2017-07-28 07:22:55 -0500730 gfs2_delete_debugfs_file(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100731 /* Unmount the locking protocol */
732 gfs2_lm_unmount(sdp);
733
734 /* At this point, we're through participating in the lockspace */
735 gfs2_sys_fs_del(sdp);
736}
737
738/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100739 * gfs2_sync_fs - sync the filesystem
740 * @sb: the superblock
741 *
742 * Flushes the log to disk.
743 */
744
745static int gfs2_sync_fs(struct super_block *sb, int wait)
746{
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100747 struct gfs2_sbd *sdp = sb->s_fs_info;
Jan Karaa1177822012-07-03 16:45:29 +0200748
749 gfs2_quota_sync(sb, -1);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500750 if (wait)
Bob Peterson805c09072018-01-08 10:34:17 -0500751 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
752 GFS2_LFC_SYNC_FS);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500753 return sdp->sd_log_error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100754}
755
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600756void gfs2_freeze_func(struct work_struct *work)
757{
758 int error;
759 struct gfs2_holder freeze_gh;
760 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
761 struct super_block *sb = sdp->sd_vfs;
762
763 atomic_inc(&sb->s_active);
Bob Peterson623ba662020-06-25 13:30:52 -0500764 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, GL_EXACT,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600765 &freeze_gh);
766 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500767 fs_info(sdp, "GFS2: couldn't get freeze lock : %d\n", error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600768 gfs2_assert_withdraw(sdp, 0);
Abhi Das8f9182192019-04-30 16:53:47 -0500769 } else {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600770 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
771 error = thaw_super(sb);
772 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500773 fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n",
774 error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600775 gfs2_assert_withdraw(sdp, 0);
776 }
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600777 gfs2_glock_dq_uninit(&freeze_gh);
778 }
779 deactivate_super(sb);
Abhi Das8f9182192019-04-30 16:53:47 -0500780 clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags);
781 wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600782 return;
783}
784
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100785/**
786 * gfs2_freeze - prevent further writes to the filesystem
787 * @sb: the VFS structure for the filesystem
788 *
789 */
790
791static int gfs2_freeze(struct super_block *sb)
792{
793 struct gfs2_sbd *sdp = sb->s_fs_info;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600794 int error = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100795
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600796 mutex_lock(&sdp->sd_freeze_mutex);
797 if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN)
798 goto out;
799
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100800 for (;;) {
Bob Peterson60528af2019-11-14 09:53:36 -0500801 if (gfs2_withdrawn(sdp)) {
802 error = -EINVAL;
803 goto out;
804 }
805
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500806 error = gfs2_lock_fs_check_clean(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100807 if (!error)
808 break;
809
Bob Peterson55317f52019-04-29 09:36:23 -0600810 if (error == -EBUSY)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100811 fs_err(sdp, "waiting for recovery before freeze\n");
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500812 else if (error == -EIO) {
813 fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
814 "to recovery error.\n");
815 goto out;
816 } else {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100817 fs_err(sdp, "error freezing FS: %d\n", error);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500818 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100819 fs_err(sdp, "retrying...\n");
820 msleep(1000);
821 }
Abhi Das8f9182192019-04-30 16:53:47 -0500822 set_bit(SDF_FS_FROZEN, &sdp->sd_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600823out:
824 mutex_unlock(&sdp->sd_freeze_mutex);
825 return error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100826}
827
828/**
829 * gfs2_unfreeze - reallow writes to the filesystem
830 * @sb: the VFS structure for the filesystem
831 *
832 */
833
834static int gfs2_unfreeze(struct super_block *sb)
835{
Steven Whitehoused564053f2013-01-11 10:49:34 +0000836 struct gfs2_sbd *sdp = sb->s_fs_info;
837
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600838 mutex_lock(&sdp->sd_freeze_mutex);
839 if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500840 !gfs2_holder_initialized(&sdp->sd_freeze_gh)) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600841 mutex_unlock(&sdp->sd_freeze_mutex);
842 return 0;
843 }
844
Steven Whitehoused564053f2013-01-11 10:49:34 +0000845 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600846 mutex_unlock(&sdp->sd_freeze_mutex);
Abhi Das8f9182192019-04-30 16:53:47 -0500847 return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100848}
849
850/**
851 * statfs_fill - fill in the sg for a given RG
852 * @rgd: the RG
853 * @sc: the sc structure
854 *
855 * Returns: 0 on success, -ESTALE if the LVB is invalid
856 */
857
858static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
859 struct gfs2_statfs_change_host *sc)
860{
861 gfs2_rgrp_verify(rgd);
862 sc->sc_total += rgd->rd_data;
863 sc->sc_free += rgd->rd_free;
864 sc->sc_dinodes += rgd->rd_dinodes;
865 return 0;
866}
867
868/**
869 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
870 * @sdp: the filesystem
871 * @sc: the sc info that will be returned
872 *
873 * Any error (other than a signal) will cause this routine to fall back
874 * to the synchronous version.
875 *
876 * FIXME: This really shouldn't busy wait like this.
877 *
878 * Returns: errno
879 */
880
881static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
882{
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100883 struct gfs2_rgrpd *rgd_next;
884 struct gfs2_holder *gha, *gh;
885 unsigned int slots = 64;
886 unsigned int x;
887 int done;
888 int error = 0, err;
889
890 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
Kees Cook6da2ec52018-06-12 13:55:00 -0700891 gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100892 if (!gha)
893 return -ENOMEM;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500894 for (x = 0; x < slots; x++)
895 gfs2_holder_mark_uninitialized(gha + x);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100896
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100897 rgd_next = gfs2_rgrpd_get_first(sdp);
898
899 for (;;) {
900 done = 1;
901
902 for (x = 0; x < slots; x++) {
903 gh = gha + x;
904
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500905 if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100906 err = gfs2_glock_wait(gh);
907 if (err) {
908 gfs2_holder_uninit(gh);
909 error = err;
910 } else {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500911 if (!error) {
912 struct gfs2_rgrpd *rgd =
913 gfs2_glock2rgrp(gh->gh_gl);
914
915 error = statfs_slow_fill(rgd, sc);
916 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100917 gfs2_glock_dq_uninit(gh);
918 }
919 }
920
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500921 if (gfs2_holder_initialized(gh))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100922 done = 0;
923 else if (rgd_next && !error) {
924 error = gfs2_glock_nq_init(rgd_next->rd_gl,
925 LM_ST_SHARED,
926 GL_ASYNC,
927 gh);
928 rgd_next = gfs2_rgrpd_get_next(rgd_next);
929 done = 0;
930 }
931
932 if (signal_pending(current))
933 error = -ERESTARTSYS;
934 }
935
936 if (done)
937 break;
938
939 yield();
940 }
941
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100942 kfree(gha);
943 return error;
944}
945
946/**
947 * gfs2_statfs_i - Do a statfs
948 * @sdp: the filesystem
949 * @sg: the sg structure
950 *
951 * Returns: errno
952 */
953
954static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
955{
956 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
957 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
958
959 spin_lock(&sdp->sd_statfs_spin);
960
961 *sc = *m_sc;
962 sc->sc_total += l_sc->sc_total;
963 sc->sc_free += l_sc->sc_free;
964 sc->sc_dinodes += l_sc->sc_dinodes;
965
966 spin_unlock(&sdp->sd_statfs_spin);
967
968 if (sc->sc_free < 0)
969 sc->sc_free = 0;
970 if (sc->sc_free > sc->sc_total)
971 sc->sc_free = sc->sc_total;
972 if (sc->sc_dinodes < 0)
973 sc->sc_dinodes = 0;
974
975 return 0;
976}
977
978/**
979 * gfs2_statfs - Gather and return stats about the filesystem
980 * @sb: The superblock
981 * @statfsbuf: The buffer
982 *
983 * Returns: 0 on success or error code
984 */
985
986static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
987{
Al Virofc640052016-04-10 01:33:30 -0400988 struct super_block *sb = dentry->d_sb;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100989 struct gfs2_sbd *sdp = sb->s_fs_info;
990 struct gfs2_statfs_change_host sc;
991 int error;
992
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100993 error = gfs2_rindex_update(sdp);
994 if (error)
995 return error;
996
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100997 if (gfs2_tune_get(sdp, gt_statfs_slow))
998 error = gfs2_statfs_slow(sdp, &sc);
999 else
1000 error = gfs2_statfs_i(sdp, &sc);
1001
1002 if (error)
1003 return error;
1004
1005 buf->f_type = GFS2_MAGIC;
1006 buf->f_bsize = sdp->sd_sb.sb_bsize;
1007 buf->f_blocks = sc.sc_total;
1008 buf->f_bfree = sc.sc_free;
1009 buf->f_bavail = sc.sc_free;
1010 buf->f_files = sc.sc_dinodes + sc.sc_free;
1011 buf->f_ffree = sc.sc_free;
1012 buf->f_namelen = GFS2_FNAMESIZE;
1013
1014 return 0;
1015}
1016
1017/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001018 * gfs2_drop_inode - Drop an inode (test for remote unlink)
1019 * @inode: The inode to drop
1020 *
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -05001021 * If we've received a callback on an iopen lock then it's because a
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001022 * remote node tried to deallocate the inode but failed due to this node
1023 * still having the inode open. Here we mark the link count zero
1024 * since we know that it must have reached zero if the GLF_DEMOTE flag
1025 * is set on the iopen glock. If we didn't do a disk read since the
1026 * remote node removed the final link then we might otherwise miss
1027 * this event. This check ensures that this node will deallocate the
1028 * inode's blocks, or alternatively pass the baton on to another
1029 * node for later deallocation.
1030 */
1031
Al Viro45321ac2010-06-07 13:43:19 -04001032static int gfs2_drop_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001033{
1034 struct gfs2_inode *ip = GFS2_I(inode);
1035
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001036 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) &&
1037 inode->i_nlink &&
1038 gfs2_holder_initialized(&ip->i_iopen_gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001039 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001040 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001041 clear_nlink(inode);
1042 }
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -05001043
1044 /*
1045 * When under memory pressure when an inode's link count has dropped to
1046 * zero, defer deleting the inode to the delete workqueue. This avoids
1047 * calling into DLM under memory pressure, which can deadlock.
1048 */
1049 if (!inode->i_nlink &&
1050 unlikely(current->flags & PF_MEMALLOC) &&
1051 gfs2_holder_initialized(&ip->i_iopen_gh)) {
1052 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1053
1054 gfs2_glock_hold(gl);
Andreas Gruenbachera0e3cc62020-01-16 20:12:26 +01001055 if (!gfs2_queue_delete_work(gl, 0))
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -05001056 gfs2_glock_queue_put(gl);
1057 return false;
1058 }
1059
Al Viro45321ac2010-06-07 13:43:19 -04001060 return generic_drop_inode(inode);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001061}
1062
1063static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
1064{
1065 do {
1066 if (d1 == d2)
1067 return 1;
1068 d1 = d1->d_parent;
1069 } while (!IS_ROOT(d1));
1070 return 0;
1071}
1072
1073/**
1074 * gfs2_show_options - Show mount options for /proc/mounts
1075 * @s: seq_file structure
Al Viro34c80b12011-12-08 21:32:45 -05001076 * @root: root of this (sub)tree
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001077 *
1078 * Returns: 0 on success or error code
1079 */
1080
Al Viro34c80b12011-12-08 21:32:45 -05001081static int gfs2_show_options(struct seq_file *s, struct dentry *root)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001082{
Al Viro34c80b12011-12-08 21:32:45 -05001083 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001084 struct gfs2_args *args = &sdp->sd_args;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001085 int val;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001086
Al Viro34c80b12011-12-08 21:32:45 -05001087 if (is_ancestor(root, sdp->sd_master_dir))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001088 seq_puts(s, ",meta");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001089 if (args->ar_lockproto[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001090 seq_show_option(s, "lockproto", args->ar_lockproto);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001091 if (args->ar_locktable[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001092 seq_show_option(s, "locktable", args->ar_locktable);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001093 if (args->ar_hostdata[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001094 seq_show_option(s, "hostdata", args->ar_hostdata);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001095 if (args->ar_spectator)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001096 seq_puts(s, ",spectator");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001097 if (args->ar_localflocks)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001098 seq_puts(s, ",localflocks");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001099 if (args->ar_debug)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001100 seq_puts(s, ",debug");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001101 if (args->ar_posix_acl)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001102 seq_puts(s, ",acl");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001103 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1104 char *state;
1105 switch (args->ar_quota) {
1106 case GFS2_QUOTA_OFF:
1107 state = "off";
1108 break;
1109 case GFS2_QUOTA_ACCOUNT:
1110 state = "account";
1111 break;
1112 case GFS2_QUOTA_ON:
1113 state = "on";
1114 break;
1115 default:
1116 state = "unknown";
1117 break;
1118 }
1119 seq_printf(s, ",quota=%s", state);
1120 }
1121 if (args->ar_suiddir)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001122 seq_puts(s, ",suiddir");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001123 if (args->ar_data != GFS2_DATA_DEFAULT) {
1124 char *state;
1125 switch (args->ar_data) {
1126 case GFS2_DATA_WRITEBACK:
1127 state = "writeback";
1128 break;
1129 case GFS2_DATA_ORDERED:
1130 state = "ordered";
1131 break;
1132 default:
1133 state = "unknown";
1134 break;
1135 }
1136 seq_printf(s, ",data=%s", state);
1137 }
1138 if (args->ar_discard)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001139 seq_puts(s, ",discard");
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001140 val = sdp->sd_tune.gt_logd_secs;
1141 if (val != 30)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001142 seq_printf(s, ",commit=%d", val);
1143 val = sdp->sd_tune.gt_statfs_quantum;
1144 if (val != 30)
1145 seq_printf(s, ",statfs_quantum=%d", val);
Steven Whitehouse2b9731e2012-08-20 17:07:49 +01001146 else if (sdp->sd_tune.gt_statfs_slow)
1147 seq_puts(s, ",statfs_quantum=0");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001148 val = sdp->sd_tune.gt_quota_quantum;
1149 if (val != 60)
1150 seq_printf(s, ",quota_quantum=%d", val);
1151 if (args->ar_statfs_percent)
1152 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
Bob Petersond34843d2009-08-24 10:44:18 +01001153 if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1154 const char *state;
1155
1156 switch (args->ar_errors) {
1157 case GFS2_ERRORS_WITHDRAW:
1158 state = "withdraw";
1159 break;
1160 case GFS2_ERRORS_PANIC:
1161 state = "panic";
1162 break;
1163 default:
1164 state = "unknown";
1165 break;
1166 }
1167 seq_printf(s, ",errors=%s", state);
1168 }
Steven Whitehousecdcfde62009-10-30 10:48:53 +00001169 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001170 seq_puts(s, ",nobarrier");
Steven Whitehouse913a71d2010-05-06 11:03:29 +01001171 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001172 seq_puts(s, ",demote_interface_used");
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001173 if (args->ar_rgrplvb)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001174 seq_puts(s, ",rgrplvb");
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001175 if (args->ar_loccookie)
1176 seq_puts(s, ",loccookie");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001177 return 0;
1178}
1179
Steven Whitehousef42ab082011-04-14 16:50:31 +01001180static void gfs2_final_release_pages(struct gfs2_inode *ip)
1181{
1182 struct inode *inode = &ip->i_inode;
1183 struct gfs2_glock *gl = ip->i_gl;
1184
1185 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1186 truncate_inode_pages(&inode->i_data, 0);
1187
Bob Peterson638803d2019-06-06 07:33:38 -05001188 if (atomic_read(&gl->gl_revokes) == 0) {
Steven Whitehousef42ab082011-04-14 16:50:31 +01001189 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1190 clear_bit(GLF_DIRTY, &gl->gl_flags);
1191 }
1192}
1193
1194static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1195{
1196 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001197 struct gfs2_rgrpd *rgd;
Bob Peterson564e12b2011-11-21 13:36:17 -05001198 struct gfs2_holder gh;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001199 int error;
1200
1201 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001202 gfs2_consist_inode(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001203 return -EIO;
1204 }
1205
Bob Peterson8e2e0042012-07-19 08:12:40 -04001206 error = gfs2_rindex_update(sdp);
1207 if (error)
1208 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001209
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001210 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001211 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001212 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001213
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001214 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001215 if (!rgd) {
1216 gfs2_consist_inode(ip);
1217 error = -EIO;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001218 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001219 }
1220
Bob Peterson564e12b2011-11-21 13:36:17 -05001221 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001222 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001223 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001224
Steven Whitehouse4667a0e2011-04-18 14:18:09 +01001225 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1226 sdp->sd_jdesc->jd_blocks);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001227 if (error)
1228 goto out_rg_gunlock;
1229
1230 gfs2_free_di(rgd, ip);
1231
1232 gfs2_final_release_pages(ip);
1233
1234 gfs2_trans_end(sdp);
1235
1236out_rg_gunlock:
Bob Peterson564e12b2011-11-21 13:36:17 -05001237 gfs2_glock_dq_uninit(&gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001238out_qs:
1239 gfs2_quota_unhold(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001240 return error;
1241}
1242
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001243/**
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001244 * gfs2_glock_put_eventually
1245 * @gl: The glock to put
1246 *
1247 * When under memory pressure, trigger a deferred glock put to make sure we
1248 * won't call into DLM and deadlock. Otherwise, put the glock directly.
1249 */
1250
1251static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1252{
1253 if (current->flags & PF_MEMALLOC)
1254 gfs2_glock_queue_put(gl);
1255 else
1256 gfs2_glock_put(gl);
1257}
1258
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001259static bool gfs2_upgrade_iopen_glock(struct inode *inode)
1260{
1261 struct gfs2_inode *ip = GFS2_I(inode);
1262 struct gfs2_sbd *sdp = GFS2_SB(inode);
1263 struct gfs2_holder *gh = &ip->i_iopen_gh;
1264 long timeout = 5 * HZ;
1265 int error;
1266
1267 gh->gh_flags |= GL_NOCACHE;
1268 gfs2_glock_dq_wait(gh);
1269
1270 /*
1271 * If there are no other lock holders, we'll get the lock immediately.
1272 * Otherwise, the other nodes holding the lock will be notified about
1273 * our locking request. If they don't have the inode open, they'll
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001274 * evict the cached inode and release the lock. Otherwise, if they
1275 * poke the inode glock, we'll take this as an indication that they
1276 * still need the iopen glock and that they'll take care of deleting
1277 * the inode when they're done. As a last resort, if another node
1278 * keeps holding the iopen glock without showing any activity on the
1279 * inode glock, we'll eventually time out.
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001280 *
1281 * Note that we're passing the LM_FLAG_TRY_1CB flag to the first
1282 * locking request as an optimization to notify lock holders as soon as
1283 * possible. Without that flag, they'd be notified implicitly by the
1284 * second locking request.
1285 */
1286
1287 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, gh);
1288 error = gfs2_glock_nq(gh);
1289 if (error != GLR_TRYFAILED)
1290 return !error;
1291
1292 gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1293 error = gfs2_glock_nq(gh);
1294 if (error)
1295 return false;
1296
1297 timeout = wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001298 !test_bit(HIF_WAIT, &gh->gh_iflags) ||
1299 test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags),
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001300 timeout);
1301 if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1302 gfs2_glock_dq(gh);
1303 return false;
1304 }
1305 return true;
1306}
1307
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001308/**
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001309 * gfs2_evict_inode - Remove an inode from cache
1310 * @inode: The inode to evict
1311 *
1312 * There are three cases to consider:
1313 * 1. i_nlink == 0, we are final opener (and must deallocate)
1314 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1315 * 3. i_nlink > 0
1316 *
1317 * If the fs is read only, then we have to treat all cases as per #3
1318 * since we are unable to do any deallocation. The inode will be
1319 * deallocated by the next read/write node to attempt an allocation
1320 * in the same resource group
1321 *
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001322 * We have to (at the moment) hold the inodes main lock to cover
1323 * the gap between unlocking the shared lock on the iopen lock and
1324 * taking the exclusive lock. I'd rather do a shared -> exclusive
1325 * conversion on the iopen lock, but we can change that later. This
1326 * is safe, just less efficient.
1327 */
1328
Al Virod5c15152010-06-07 11:05:19 -04001329static void gfs2_evict_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001330{
Steven Whitehouse001e8e82011-03-30 14:17:51 +01001331 struct super_block *sb = inode->i_sb;
1332 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001333 struct gfs2_inode *ip = GFS2_I(inode);
1334 struct gfs2_holder gh;
Bob Petersonee530be2015-12-07 15:13:28 -06001335 struct address_space *metamapping;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001336 int error;
1337
Abhi Das05978802014-03-31 10:33:17 -05001338 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1339 clear_inode(inode);
1340 return;
1341 }
1342
David Howellsbc98a422017-07-17 08:45:34 +01001343 if (inode->i_nlink || sb_rdonly(sb))
Al Virod5c15152010-06-07 11:05:19 -04001344 goto out;
1345
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001346 if (test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1347 BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));
1348 gfs2_holder_mark_uninitialized(&gh);
Andreas Gruenbacher8c7b9262020-01-13 22:16:17 +01001349 goto out_delete;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001350 }
1351
Andreas Gruenbacher8c7b9262020-01-13 22:16:17 +01001352 if (test_bit(GIF_DEFERRED_DELETE, &ip->i_flags))
1353 goto out;
1354
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -05001355 /* Deletes should never happen under memory pressure anymore. */
1356 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1357 goto out;
1358
Bob Peterson44ad37d2011-03-17 16:19:58 -04001359 /* Must not read inode block until block type has been verified */
1360 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, &gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001361 if (unlikely(error)) {
Bob Peterson240c6232017-07-18 12:36:01 -05001362 glock_clear_object(ip->i_iopen_gh.gh_gl, ip);
Bob Petersona6a4d982013-05-29 11:51:52 -04001363 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
Andreas Gruenbacherd4da3192017-02-22 20:05:09 +01001364 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001365 goto out;
1366 }
1367
Andreas Gruenbacherf286d622020-01-13 21:21:49 +01001368 if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
1369 goto out_truncate;
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001370 error = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1371 if (error)
1372 goto out_truncate;
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001373
Bob Peterson44ad37d2011-03-17 16:19:58 -04001374 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1375 error = gfs2_inode_refresh(ip);
1376 if (error)
1377 goto out_truncate;
1378 }
1379
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001380 /*
1381 * The inode may have been recreated in the meantime.
1382 */
1383 if (inode->i_nlink)
1384 goto out_truncate;
1385
Andreas Gruenbacher8c7b9262020-01-13 22:16:17 +01001386out_delete:
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001387 if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
Bob Peterson7508abc2015-12-18 11:54:55 -06001388 test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001389 if (!gfs2_upgrade_iopen_glock(inode)) {
1390 gfs2_holder_uninit(&ip->i_iopen_gh);
Bob Peterson7508abc2015-12-18 11:54:55 -06001391 goto out_truncate;
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001392 }
Bob Peterson7508abc2015-12-18 11:54:55 -06001393 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001394
1395 if (S_ISDIR(inode->i_mode) &&
1396 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1397 error = gfs2_dir_exhash_dealloc(ip);
1398 if (error)
1399 goto out_unlock;
1400 }
1401
1402 if (ip->i_eattr) {
1403 error = gfs2_ea_dealloc(ip);
1404 if (error)
1405 goto out_unlock;
1406 }
1407
1408 if (!gfs2_is_stuffed(ip)) {
1409 error = gfs2_file_dealloc(ip);
1410 if (error)
1411 goto out_unlock;
1412 }
1413
Bob Peterson240c6232017-07-18 12:36:01 -05001414 /* We're about to clear the bitmap for the dinode, but as soon as we
1415 do, gfs2_create_inode can create another inode at the same block
1416 location and try to set gl_object again. We clear gl_object here so
1417 that subsequent inode creates don't see an old gl_object. */
1418 glock_clear_object(ip->i_gl, ip);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001419 error = gfs2_dinode_dealloc(ip);
Andreas Gruenbacherf286d622020-01-13 21:21:49 +01001420 gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001421 goto out_unlock;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001422
1423out_truncate:
Bob Peterson805c09072018-01-08 10:34:17 -05001424 gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1425 GFS2_LFC_EVICT_INODE);
Bob Petersonee530be2015-12-07 15:13:28 -06001426 metamapping = gfs2_glock2aspace(ip->i_gl);
Benjamin Marzinski2216db72012-09-20 09:52:58 -05001427 if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
Benjamin Marzinski2216db72012-09-20 09:52:58 -05001428 filemap_fdatawrite(metamapping);
1429 filemap_fdatawait(metamapping);
1430 }
Steven Whitehouse40ac2182011-08-02 13:17:27 +01001431 write_inode_now(inode, 1);
Steven Whitehouseb5b24d72011-09-07 10:33:25 +01001432 gfs2_ail_flush(ip->i_gl, 0);
Steven Whitehouse40ac2182011-08-02 13:17:27 +01001433
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001434 error = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1435 if (error)
1436 goto out_unlock;
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001437 /* Needs to be done before glock release & also in a transaction */
1438 truncate_inode_pages(&inode->i_data, 0);
Bob Petersonee530be2015-12-07 15:13:28 -06001439 truncate_inode_pages(metamapping, 0);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001440 gfs2_trans_end(sdp);
1441
1442out_unlock:
Bob Petersona097dc7e2015-07-16 08:28:04 -05001443 if (gfs2_rs_active(&ip->i_res))
1444 gfs2_rs_deltree(&ip->i_res);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001445
Bob Peterson240c6232017-07-18 12:36:01 -05001446 if (gfs2_holder_initialized(&gh)) {
1447 glock_clear_object(ip->i_gl, ip);
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001448 gfs2_glock_dq_uninit(&gh);
Bob Peterson240c6232017-07-18 12:36:01 -05001449 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001450 if (error && error != GLR_TRYFAILED && error != -EROFS)
Al Virod5c15152010-06-07 11:05:19 -04001451 fs_warn(sdp, "gfs2_evict_inode: %d\n", error);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001452out:
Johannes Weiner91b0abe2014-04-03 14:47:49 -07001453 truncate_inode_pages_final(&inode->i_data);
Bob Peterson2fba46a2020-02-27 12:47:53 -06001454 if (ip->i_qadata)
1455 gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
Andreas Gruenbacher15955482020-03-06 10:32:35 -06001456 gfs2_rs_delete(ip, NULL);
Steven Whitehouse45138992013-01-28 09:30:07 +00001457 gfs2_ordered_del_inode(ip);
Jan Karadbd57682012-05-03 14:48:02 +02001458 clear_inode(inode);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001459 gfs2_dir_hash_inval(ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001460 if (ip->i_gl) {
1461 glock_clear_object(ip->i_gl, ip);
1462 wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1463 gfs2_glock_add_to_lru(ip->i_gl);
1464 gfs2_glock_put_eventually(ip->i_gl);
1465 ip->i_gl = NULL;
1466 }
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001467 if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001468 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1469
1470 glock_clear_object(gl, ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001471 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1472 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1473 gfs2_glock_dq(&ip->i_iopen_gh);
1474 }
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001475 gfs2_glock_hold(gl);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001476 gfs2_holder_uninit(&ip->i_iopen_gh);
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001477 gfs2_glock_put_eventually(gl);
Al Virod5c15152010-06-07 11:05:19 -04001478 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001479}
1480
1481static struct inode *gfs2_alloc_inode(struct super_block *sb)
1482{
1483 struct gfs2_inode *ip;
1484
1485 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001486 if (!ip)
1487 return NULL;
1488 ip->i_flags = 0;
1489 ip->i_gl = NULL;
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001490 gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001491 memset(&ip->i_res, 0, sizeof(ip->i_res));
1492 RB_CLEAR_NODE(&ip->i_res.rs_node);
1493 ip->i_rahead = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001494 return &ip->i_inode;
1495}
1496
Al Viro784494e2019-04-15 19:45:26 -04001497static void gfs2_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001498{
Al Viro784494e2019-04-15 19:45:26 -04001499 kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001500}
1501
1502const struct super_operations gfs2_super_ops = {
1503 .alloc_inode = gfs2_alloc_inode,
Al Viro784494e2019-04-15 19:45:26 -04001504 .free_inode = gfs2_free_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001505 .write_inode = gfs2_write_inode,
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001506 .dirty_inode = gfs2_dirty_inode,
Al Virod5c15152010-06-07 11:05:19 -04001507 .evict_inode = gfs2_evict_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001508 .put_super = gfs2_put_super,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001509 .sync_fs = gfs2_sync_fs,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001510 .freeze_super = gfs2_freeze,
1511 .thaw_super = gfs2_unfreeze,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001512 .statfs = gfs2_statfs,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001513 .drop_inode = gfs2_drop_inode,
1514 .show_options = gfs2_show_options,
1515};
1516