blob: ed7a829e9ffe3c6a888620acc1934dced552e661 [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
Bob Peterson53dbc27e2020-09-11 10:30:26 -050047enum dinode_demise {
48 SHOULD_DELETE_DINODE,
49 SHOULD_NOT_DELETE_DINODE,
50 SHOULD_DEFER_EVICTION,
51};
52
Steven Whitehousefefc03b2008-12-19 15:32:06 +000053/**
54 * gfs2_jindex_free - Clear all the journal index information
55 * @sdp: The GFS2 superblock
56 *
57 */
58
59void gfs2_jindex_free(struct gfs2_sbd *sdp)
60{
Steven Whitehouseb50f2272014-03-03 13:35:57 +000061 struct list_head list;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000062 struct gfs2_jdesc *jd;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000063
64 spin_lock(&sdp->sd_jindex_spin);
65 list_add(&list, &sdp->sd_jindex_list);
66 list_del_init(&sdp->sd_jindex_list);
67 sdp->sd_journals = 0;
68 spin_unlock(&sdp->sd_jindex_spin);
69
Bob Peterson601ef0d2020-01-28 20:23:45 +010070 sdp->sd_jdesc = NULL;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000071 while (!list_empty(&list)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +010072 jd = list_first_entry(&list, struct gfs2_jdesc, jd_list);
Steven Whitehouseb50f2272014-03-03 13:35:57 +000073 gfs2_free_journal_extents(jd);
Steven Whitehousefefc03b2008-12-19 15:32:06 +000074 list_del(&jd->jd_list);
75 iput(jd->jd_inode);
Bob Peterson601ef0d2020-01-28 20:23:45 +010076 jd->jd_inode = NULL;
Steven Whitehousefefc03b2008-12-19 15:32:06 +000077 kfree(jd);
78 }
79}
80
David Teiglandb3b94fa2006-01-16 16:50:04 +000081static struct gfs2_jdesc *jdesc_find_i(struct list_head *head, unsigned int jid)
82{
83 struct gfs2_jdesc *jd;
David Teiglandb3b94fa2006-01-16 16:50:04 +000084
85 list_for_each_entry(jd, head, jd_list) {
Andreas Gruenbacher736b2f72020-12-07 00:06:32 +010086 if (jd->jd_jid == jid)
87 return jd;
David Teiglandb3b94fa2006-01-16 16:50:04 +000088 }
Andreas Gruenbacher736b2f72020-12-07 00:06:32 +010089 return NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +000090}
91
92struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
93{
94 struct gfs2_jdesc *jd;
95
96 spin_lock(&sdp->sd_jindex_spin);
97 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
98 spin_unlock(&sdp->sd_jindex_spin);
99
100 return jd;
101}
102
David Teiglandb3b94fa2006-01-16 16:50:04 +0000103int gfs2_jdesc_check(struct gfs2_jdesc *jd)
104{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400105 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
106 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100107 u64 size = i_size_read(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000108
Fabian Frederick47a9a522016-08-02 12:05:27 -0500109 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000111
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100112 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
113
114 if (gfs2_write_alloc_required(ip, 0, size)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115 gfs2_consist_inode(ip);
Bob Peterson461cb412010-06-24 19:21:20 -0400116 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 }
118
Bob Peterson461cb412010-06-24 19:21:20 -0400119 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000120}
121
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000122static int init_threads(struct gfs2_sbd *sdp)
123{
124 struct task_struct *p;
125 int error = 0;
126
127 p = kthread_run(gfs2_logd, sdp, "gfs2_logd");
128 if (IS_ERR(p)) {
129 error = PTR_ERR(p);
130 fs_err(sdp, "can't start logd thread: %d\n", error);
131 return error;
132 }
133 sdp->sd_logd_process = p;
134
135 p = kthread_run(gfs2_quotad, sdp, "gfs2_quotad");
136 if (IS_ERR(p)) {
137 error = PTR_ERR(p);
138 fs_err(sdp, "can't start quotad thread: %d\n", error);
139 goto fail;
140 }
141 sdp->sd_quotad_process = p;
142 return 0;
143
144fail:
145 kthread_stop(sdp->sd_logd_process);
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600146 sdp->sd_logd_process = NULL;
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000147 return error;
148}
149
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150/**
151 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
152 * @sdp: the filesystem
153 *
154 * Returns: errno
155 */
156
157int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
158{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400159 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500160 struct gfs2_glock *j_gl = ip->i_gl;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600161 struct gfs2_holder freeze_gh;
Al Viro55167622006-10-13 21:47:13 -0400162 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 int error;
164
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000165 error = init_threads(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000166 if (error)
167 return error;
168
Bob Petersonc860f8f2020-06-25 14:42:17 -0500169 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
170 LM_FLAG_NOEXP | 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
Abhi Das73092692020-10-20 15:58:03 -0500226void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100227{
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
349 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
350 &gh);
351 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600352 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000353
354 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
355 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600356 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357
358 spin_lock(&sdp->sd_statfs_spin);
359 gfs2_statfs_change_in(m_sc, m_bh->b_data +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400360 sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000361 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
362 spin_unlock(&sdp->sd_statfs_spin);
363 goto out_bh;
364 }
365 spin_unlock(&sdp->sd_statfs_spin);
366
367 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
368 if (error)
369 goto out_bh;
370
371 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
372 if (error)
373 goto out_bh2;
374
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500375 update_statfs(sdp, m_bh, l_bh);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500376 sdp->sd_statfs_force_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000377
378 gfs2_trans_end(sdp);
379
Steven Whitehousea91ea692006-09-04 12:04:26 -0400380out_bh2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000381 brelse(l_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400382out_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000383 brelse(m_bh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600384out_unlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000385 gfs2_glock_dq_uninit(&gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600386out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000387 return error;
388}
389
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390struct lfcc {
391 struct list_head list;
392 struct gfs2_holder gh;
393};
394
395/**
396 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
397 * journals are clean
398 * @sdp: the file system
399 * @state: the state to put the transaction lock into
400 * @t_gh: the hold on the transaction lock
401 *
402 * Returns: errno
403 */
404
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500405static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000406{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500407 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 struct gfs2_jdesc *jd;
409 struct lfcc *lfcc;
410 LIST_HEAD(list);
Al Viro55167622006-10-13 21:47:13 -0400411 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000412 int error;
413
David Teiglandb3b94fa2006-01-16 16:50:04 +0000414 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
415 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
416 if (!lfcc) {
417 error = -ENOMEM;
418 goto out;
419 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400420 ip = GFS2_I(jd->jd_inode);
421 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000422 if (error) {
423 kfree(lfcc);
424 goto out;
425 }
426 list_add(&lfcc->list, &list);
427 }
428
Benjamin Marzinski24972552014-05-01 22:26:55 -0500429 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
Bob Petersonc860f8f2020-06-25 14:42:17 -0500430 LM_FLAG_NOEXP, &sdp->sd_freeze_gh);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500431 if (error)
432 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000433
434 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
435 error = gfs2_jdesc_check(jd);
436 if (error)
437 break;
Abhi Dasf4686c22019-05-02 14:17:40 -0500438 error = gfs2_find_jhead(jd, &lh, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000439 if (error)
440 break;
441 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
442 error = -EBUSY;
443 break;
444 }
445 }
446
447 if (error)
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500448 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000449
Steven Whitehousea91ea692006-09-04 12:04:26 -0400450out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 while (!list_empty(&list)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +0100452 lfcc = list_first_entry(&list, struct lfcc, list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000453 list_del(&lfcc->list);
454 gfs2_glock_dq_uninit(&lfcc->gh);
455 kfree(lfcc);
456 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000457 return error;
458}
459
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100460void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
461{
462 struct gfs2_dinode *str = buf;
463
464 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
465 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
466 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
467 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
468 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
469 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
Eric W. Biedermand0546422013-01-31 22:08:10 -0800470 str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
471 str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100472 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
473 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
474 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
475 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
476 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
477 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
478
479 str->di_goal_meta = cpu_to_be64(ip->i_goal);
480 str->di_goal_data = cpu_to_be64(ip->i_goal);
481 str->di_generation = cpu_to_be64(ip->i_generation);
482
483 str->di_flags = cpu_to_be32(ip->i_diskflags);
484 str->di_height = cpu_to_be16(ip->i_height);
485 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
486 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
487 GFS2_FORMAT_DE : 0);
488 str->di_depth = cpu_to_be16(ip->i_depth);
489 str->di_entries = cpu_to_be32(ip->i_entries);
490
491 str->di_eattr = cpu_to_be64(ip->i_eattr);
492 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
493 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
494 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
495}
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100496
497/**
498 * gfs2_write_inode - Make sure the inode is stable on the disk
499 * @inode: The inode
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100500 * @wbc: The writeback control structure
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100501 *
502 * Returns: errno
503 */
504
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100505static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100506{
507 struct gfs2_inode *ip = GFS2_I(inode);
508 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100509 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100510 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100511 int ret = 0;
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200512 bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100513
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200514 if (flush_all)
Bob Petersonc1696fb2018-01-17 00:01:33 +0100515 gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
Bob Peterson805c09072018-01-08 10:34:17 -0500516 GFS2_LOG_HEAD_FLUSH_NORMAL |
517 GFS2_LFC_WRITE_INODE);
Tejun Heoa88a3412015-05-22 17:13:28 -0400518 if (bdi->wb.dirty_exceeded)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100519 gfs2_ail1_flush(sdp, wbc);
Steven Whitehouse1d4ec642011-08-02 13:13:20 +0100520 else
521 filemap_fdatawrite(metamapping);
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200522 if (flush_all)
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100523 ret = filemap_fdatawait(metamapping);
524 if (ret)
525 mark_inode_dirty_sync(inode);
Abhi Das957a7ac2018-01-30 10:00:09 -0700526 else {
527 spin_lock(&inode->i_lock);
528 if (!(inode->i_flags & I_DIRTY))
529 gfs2_ordered_del_inode(ip);
530 spin_unlock(&inode->i_lock);
531 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100532 return ret;
533}
534
535/**
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100536 * gfs2_dirty_inode - check for atime updates
537 * @inode: The inode in question
538 * @flags: The type of dirty
539 *
540 * Unfortunately it can be called under any combination of inode
541 * glock and transaction lock, so we have to check carefully.
542 *
543 * At the moment this deals only with atime - it should be possible
544 * to expand that role in future, once a review of the locking has
545 * been carried out.
546 */
547
548static void gfs2_dirty_inode(struct inode *inode, int flags)
549{
550 struct gfs2_inode *ip = GFS2_I(inode);
551 struct gfs2_sbd *sdp = GFS2_SB(inode);
552 struct buffer_head *bh;
553 struct gfs2_holder gh;
554 int need_unlock = 0;
555 int need_endtrans = 0;
556 int ret;
557
Christoph Hellwig0e11f642018-02-21 07:54:49 -0800558 if (!(flags & I_DIRTY_INODE))
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100559 return;
Bob Petersoneb43e662019-11-14 09:52:15 -0500560 if (unlikely(gfs2_withdrawn(sdp)))
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500561 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100562 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
563 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
564 if (ret) {
565 fs_err(sdp, "dirty_inode: glock %d\n", ret);
Bob Petersone28c02b2020-07-30 12:31:38 -0500566 gfs2_dump_glock(NULL, ip->i_gl, true);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100567 return;
568 }
569 need_unlock = 1;
Benjamin Marzinski3d162682012-11-06 00:49:28 -0600570 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
571 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100572
573 if (current->journal_info == NULL) {
574 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
575 if (ret) {
576 fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
577 goto out;
578 }
579 need_endtrans = 1;
580 }
581
582 ret = gfs2_meta_inode_buffer(ip, &bh);
583 if (ret == 0) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000584 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100585 gfs2_dinode_out(ip, bh->b_data);
586 brelse(bh);
587 }
588
589 if (need_endtrans)
590 gfs2_trans_end(sdp);
591out:
592 if (need_unlock)
593 gfs2_glock_dq_uninit(&gh);
594}
595
596/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100597 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
598 * @sdp: the filesystem
599 *
600 * Returns: errno
601 */
602
Andrew Price1f52aa02019-03-27 14:46:00 +0000603int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100604{
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600605 struct gfs2_holder freeze_gh;
Bob Peterson601ef0d2020-01-28 20:23:45 +0100606 int error = 0;
607 int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100608
Bob Peterson601ef0d2020-01-28 20:23:45 +0100609 gfs2_holder_mark_uninitialized(&freeze_gh);
610 if (sdp->sd_freeze_gl &&
611 !gfs2_glock_is_locked_by_me(sdp->sd_freeze_gl)) {
612 if (!log_write_allowed) {
613 error = gfs2_glock_nq_init(sdp->sd_freeze_gl,
Bob Petersonc860f8f2020-06-25 14:42:17 -0500614 LM_ST_SHARED, LM_FLAG_TRY |
615 LM_FLAG_NOEXP | GL_EXACT,
Bob Peterson623ba662020-06-25 13:30:52 -0500616 &freeze_gh);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100617 if (error == GLR_TRYFAILED)
618 error = 0;
619 } else {
620 error = gfs2_glock_nq_init(sdp->sd_freeze_gl,
Bob Petersonc860f8f2020-06-25 14:42:17 -0500621 LM_ST_SHARED,
622 LM_FLAG_NOEXP | 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 }
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +0200702 WARN_ON(gfs2_withdrawing(sdp));
703
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100704 /* At this point, we're through modifying the disk */
705
706 /* Release stuff */
707
708 iput(sdp->sd_jindex);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100709 iput(sdp->sd_statfs_inode);
710 iput(sdp->sd_rindex);
711 iput(sdp->sd_quota_inode);
712
713 gfs2_glock_put(sdp->sd_rename_gl);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500714 gfs2_glock_put(sdp->sd_freeze_gl);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100715
716 if (!sdp->sd_args.ar_spectator) {
Bob Peterson601ef0d2020-01-28 20:23:45 +0100717 if (gfs2_holder_initialized(&sdp->sd_journal_gh))
718 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
719 if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
720 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100721 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
722 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Abhi Das97fd7342020-10-20 15:58:04 -0500723 free_local_statfs_inodes(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100724 iput(sdp->sd_qc_inode);
725 }
726
727 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
728 gfs2_clear_rgrpd(sdp);
729 gfs2_jindex_free(sdp);
730 /* Take apart glock structures and buffer lists */
731 gfs2_gl_hash_clear(sdp);
Bob Petersona9dd9452020-10-27 10:10:02 -0500732 truncate_inode_pages_final(&sdp->sd_aspace);
Bob Petersonb2fb7da2017-07-28 07:22:55 -0500733 gfs2_delete_debugfs_file(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100734 /* Unmount the locking protocol */
735 gfs2_lm_unmount(sdp);
736
737 /* At this point, we're through participating in the lockspace */
738 gfs2_sys_fs_del(sdp);
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100739 free_sbd(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100740}
741
742/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100743 * gfs2_sync_fs - sync the filesystem
744 * @sb: the superblock
745 *
746 * Flushes the log to disk.
747 */
748
749static int gfs2_sync_fs(struct super_block *sb, int wait)
750{
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100751 struct gfs2_sbd *sdp = sb->s_fs_info;
Jan Karaa1177822012-07-03 16:45:29 +0200752
753 gfs2_quota_sync(sb, -1);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500754 if (wait)
Bob Peterson805c09072018-01-08 10:34:17 -0500755 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
756 GFS2_LFC_SYNC_FS);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500757 return sdp->sd_log_error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100758}
759
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600760void gfs2_freeze_func(struct work_struct *work)
761{
762 int error;
763 struct gfs2_holder freeze_gh;
764 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
765 struct super_block *sb = sdp->sd_vfs;
766
767 atomic_inc(&sb->s_active);
Bob Petersonc860f8f2020-06-25 14:42:17 -0500768 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED,
769 LM_FLAG_NOEXP | GL_EXACT, &freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600770 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500771 fs_info(sdp, "GFS2: couldn't get freeze lock : %d\n", error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600772 gfs2_assert_withdraw(sdp, 0);
Abhi Das8f9182192019-04-30 16:53:47 -0500773 } else {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600774 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
775 error = thaw_super(sb);
776 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500777 fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n",
778 error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600779 gfs2_assert_withdraw(sdp, 0);
780 }
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600781 gfs2_glock_dq_uninit(&freeze_gh);
782 }
783 deactivate_super(sb);
Abhi Das8f9182192019-04-30 16:53:47 -0500784 clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags);
785 wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600786 return;
787}
788
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100789/**
790 * gfs2_freeze - prevent further writes to the filesystem
791 * @sb: the VFS structure for the filesystem
792 *
793 */
794
795static int gfs2_freeze(struct super_block *sb)
796{
797 struct gfs2_sbd *sdp = sb->s_fs_info;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600798 int error = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100799
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600800 mutex_lock(&sdp->sd_freeze_mutex);
801 if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN)
802 goto out;
803
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100804 for (;;) {
Bob Peterson60528af2019-11-14 09:53:36 -0500805 if (gfs2_withdrawn(sdp)) {
806 error = -EINVAL;
807 goto out;
808 }
809
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500810 error = gfs2_lock_fs_check_clean(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100811 if (!error)
812 break;
813
Bob Peterson55317f52019-04-29 09:36:23 -0600814 if (error == -EBUSY)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100815 fs_err(sdp, "waiting for recovery before freeze\n");
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500816 else if (error == -EIO) {
817 fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
818 "to recovery error.\n");
819 goto out;
820 } else {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100821 fs_err(sdp, "error freezing FS: %d\n", error);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500822 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100823 fs_err(sdp, "retrying...\n");
824 msleep(1000);
825 }
Abhi Das8f9182192019-04-30 16:53:47 -0500826 set_bit(SDF_FS_FROZEN, &sdp->sd_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600827out:
828 mutex_unlock(&sdp->sd_freeze_mutex);
829 return error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100830}
831
832/**
833 * gfs2_unfreeze - reallow writes to the filesystem
834 * @sb: the VFS structure for the filesystem
835 *
836 */
837
838static int gfs2_unfreeze(struct super_block *sb)
839{
Steven Whitehoused564053f2013-01-11 10:49:34 +0000840 struct gfs2_sbd *sdp = sb->s_fs_info;
841
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600842 mutex_lock(&sdp->sd_freeze_mutex);
843 if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500844 !gfs2_holder_initialized(&sdp->sd_freeze_gh)) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600845 mutex_unlock(&sdp->sd_freeze_mutex);
846 return 0;
847 }
848
Steven Whitehoused564053f2013-01-11 10:49:34 +0000849 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600850 mutex_unlock(&sdp->sd_freeze_mutex);
Abhi Das8f9182192019-04-30 16:53:47 -0500851 return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100852}
853
854/**
855 * statfs_fill - fill in the sg for a given RG
856 * @rgd: the RG
857 * @sc: the sc structure
858 *
859 * Returns: 0 on success, -ESTALE if the LVB is invalid
860 */
861
862static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
863 struct gfs2_statfs_change_host *sc)
864{
865 gfs2_rgrp_verify(rgd);
866 sc->sc_total += rgd->rd_data;
867 sc->sc_free += rgd->rd_free;
868 sc->sc_dinodes += rgd->rd_dinodes;
869 return 0;
870}
871
872/**
873 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
874 * @sdp: the filesystem
875 * @sc: the sc info that will be returned
876 *
877 * Any error (other than a signal) will cause this routine to fall back
878 * to the synchronous version.
879 *
880 * FIXME: This really shouldn't busy wait like this.
881 *
882 * Returns: errno
883 */
884
885static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
886{
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100887 struct gfs2_rgrpd *rgd_next;
888 struct gfs2_holder *gha, *gh;
889 unsigned int slots = 64;
890 unsigned int x;
891 int done;
892 int error = 0, err;
893
894 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
Kees Cook6da2ec52018-06-12 13:55:00 -0700895 gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100896 if (!gha)
897 return -ENOMEM;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500898 for (x = 0; x < slots; x++)
899 gfs2_holder_mark_uninitialized(gha + x);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100900
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100901 rgd_next = gfs2_rgrpd_get_first(sdp);
902
903 for (;;) {
904 done = 1;
905
906 for (x = 0; x < slots; x++) {
907 gh = gha + x;
908
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500909 if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100910 err = gfs2_glock_wait(gh);
911 if (err) {
912 gfs2_holder_uninit(gh);
913 error = err;
914 } else {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500915 if (!error) {
916 struct gfs2_rgrpd *rgd =
917 gfs2_glock2rgrp(gh->gh_gl);
918
919 error = statfs_slow_fill(rgd, sc);
920 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100921 gfs2_glock_dq_uninit(gh);
922 }
923 }
924
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500925 if (gfs2_holder_initialized(gh))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100926 done = 0;
927 else if (rgd_next && !error) {
928 error = gfs2_glock_nq_init(rgd_next->rd_gl,
929 LM_ST_SHARED,
930 GL_ASYNC,
931 gh);
932 rgd_next = gfs2_rgrpd_get_next(rgd_next);
933 done = 0;
934 }
935
936 if (signal_pending(current))
937 error = -ERESTARTSYS;
938 }
939
940 if (done)
941 break;
942
943 yield();
944 }
945
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100946 kfree(gha);
947 return error;
948}
949
950/**
951 * gfs2_statfs_i - Do a statfs
952 * @sdp: the filesystem
953 * @sg: the sg structure
954 *
955 * Returns: errno
956 */
957
958static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
959{
960 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
961 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
962
963 spin_lock(&sdp->sd_statfs_spin);
964
965 *sc = *m_sc;
966 sc->sc_total += l_sc->sc_total;
967 sc->sc_free += l_sc->sc_free;
968 sc->sc_dinodes += l_sc->sc_dinodes;
969
970 spin_unlock(&sdp->sd_statfs_spin);
971
972 if (sc->sc_free < 0)
973 sc->sc_free = 0;
974 if (sc->sc_free > sc->sc_total)
975 sc->sc_free = sc->sc_total;
976 if (sc->sc_dinodes < 0)
977 sc->sc_dinodes = 0;
978
979 return 0;
980}
981
982/**
983 * gfs2_statfs - Gather and return stats about the filesystem
984 * @sb: The superblock
985 * @statfsbuf: The buffer
986 *
987 * Returns: 0 on success or error code
988 */
989
990static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
991{
Al Virofc640052016-04-10 01:33:30 -0400992 struct super_block *sb = dentry->d_sb;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100993 struct gfs2_sbd *sdp = sb->s_fs_info;
994 struct gfs2_statfs_change_host sc;
995 int error;
996
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100997 error = gfs2_rindex_update(sdp);
998 if (error)
999 return error;
1000
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001001 if (gfs2_tune_get(sdp, gt_statfs_slow))
1002 error = gfs2_statfs_slow(sdp, &sc);
1003 else
1004 error = gfs2_statfs_i(sdp, &sc);
1005
1006 if (error)
1007 return error;
1008
1009 buf->f_type = GFS2_MAGIC;
1010 buf->f_bsize = sdp->sd_sb.sb_bsize;
1011 buf->f_blocks = sc.sc_total;
1012 buf->f_bfree = sc.sc_free;
1013 buf->f_bavail = sc.sc_free;
1014 buf->f_files = sc.sc_dinodes + sc.sc_free;
1015 buf->f_ffree = sc.sc_free;
1016 buf->f_namelen = GFS2_FNAMESIZE;
1017
1018 return 0;
1019}
1020
1021/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001022 * gfs2_drop_inode - Drop an inode (test for remote unlink)
1023 * @inode: The inode to drop
1024 *
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -05001025 * If we've received a callback on an iopen lock then it's because a
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001026 * remote node tried to deallocate the inode but failed due to this node
1027 * still having the inode open. Here we mark the link count zero
1028 * since we know that it must have reached zero if the GLF_DEMOTE flag
1029 * is set on the iopen glock. If we didn't do a disk read since the
1030 * remote node removed the final link then we might otherwise miss
1031 * this event. This check ensures that this node will deallocate the
1032 * inode's blocks, or alternatively pass the baton on to another
1033 * node for later deallocation.
1034 */
1035
Al Viro45321ac2010-06-07 13:43:19 -04001036static int gfs2_drop_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001037{
1038 struct gfs2_inode *ip = GFS2_I(inode);
1039
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001040 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) &&
1041 inode->i_nlink &&
1042 gfs2_holder_initialized(&ip->i_iopen_gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001043 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001044 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001045 clear_nlink(inode);
1046 }
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -05001047
1048 /*
1049 * When under memory pressure when an inode's link count has dropped to
1050 * zero, defer deleting the inode to the delete workqueue. This avoids
1051 * calling into DLM under memory pressure, which can deadlock.
1052 */
1053 if (!inode->i_nlink &&
1054 unlikely(current->flags & PF_MEMALLOC) &&
1055 gfs2_holder_initialized(&ip->i_iopen_gh)) {
1056 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1057
1058 gfs2_glock_hold(gl);
Andreas Gruenbachera0e3cc62020-01-16 20:12:26 +01001059 if (!gfs2_queue_delete_work(gl, 0))
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -05001060 gfs2_glock_queue_put(gl);
1061 return false;
1062 }
1063
Al Viro45321ac2010-06-07 13:43:19 -04001064 return generic_drop_inode(inode);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001065}
1066
1067static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
1068{
1069 do {
1070 if (d1 == d2)
1071 return 1;
1072 d1 = d1->d_parent;
1073 } while (!IS_ROOT(d1));
1074 return 0;
1075}
1076
1077/**
1078 * gfs2_show_options - Show mount options for /proc/mounts
1079 * @s: seq_file structure
Al Viro34c80b12011-12-08 21:32:45 -05001080 * @root: root of this (sub)tree
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001081 *
1082 * Returns: 0 on success or error code
1083 */
1084
Al Viro34c80b12011-12-08 21:32:45 -05001085static int gfs2_show_options(struct seq_file *s, struct dentry *root)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001086{
Al Viro34c80b12011-12-08 21:32:45 -05001087 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001088 struct gfs2_args *args = &sdp->sd_args;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001089 int val;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001090
Al Viro34c80b12011-12-08 21:32:45 -05001091 if (is_ancestor(root, sdp->sd_master_dir))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001092 seq_puts(s, ",meta");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001093 if (args->ar_lockproto[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001094 seq_show_option(s, "lockproto", args->ar_lockproto);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001095 if (args->ar_locktable[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001096 seq_show_option(s, "locktable", args->ar_locktable);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001097 if (args->ar_hostdata[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001098 seq_show_option(s, "hostdata", args->ar_hostdata);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001099 if (args->ar_spectator)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001100 seq_puts(s, ",spectator");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001101 if (args->ar_localflocks)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001102 seq_puts(s, ",localflocks");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001103 if (args->ar_debug)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001104 seq_puts(s, ",debug");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001105 if (args->ar_posix_acl)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001106 seq_puts(s, ",acl");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001107 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1108 char *state;
1109 switch (args->ar_quota) {
1110 case GFS2_QUOTA_OFF:
1111 state = "off";
1112 break;
1113 case GFS2_QUOTA_ACCOUNT:
1114 state = "account";
1115 break;
1116 case GFS2_QUOTA_ON:
1117 state = "on";
1118 break;
1119 default:
1120 state = "unknown";
1121 break;
1122 }
1123 seq_printf(s, ",quota=%s", state);
1124 }
1125 if (args->ar_suiddir)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001126 seq_puts(s, ",suiddir");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001127 if (args->ar_data != GFS2_DATA_DEFAULT) {
1128 char *state;
1129 switch (args->ar_data) {
1130 case GFS2_DATA_WRITEBACK:
1131 state = "writeback";
1132 break;
1133 case GFS2_DATA_ORDERED:
1134 state = "ordered";
1135 break;
1136 default:
1137 state = "unknown";
1138 break;
1139 }
1140 seq_printf(s, ",data=%s", state);
1141 }
1142 if (args->ar_discard)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001143 seq_puts(s, ",discard");
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001144 val = sdp->sd_tune.gt_logd_secs;
1145 if (val != 30)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001146 seq_printf(s, ",commit=%d", val);
1147 val = sdp->sd_tune.gt_statfs_quantum;
1148 if (val != 30)
1149 seq_printf(s, ",statfs_quantum=%d", val);
Steven Whitehouse2b9731e2012-08-20 17:07:49 +01001150 else if (sdp->sd_tune.gt_statfs_slow)
1151 seq_puts(s, ",statfs_quantum=0");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001152 val = sdp->sd_tune.gt_quota_quantum;
1153 if (val != 60)
1154 seq_printf(s, ",quota_quantum=%d", val);
1155 if (args->ar_statfs_percent)
1156 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
Bob Petersond34843d2009-08-24 10:44:18 +01001157 if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1158 const char *state;
1159
1160 switch (args->ar_errors) {
1161 case GFS2_ERRORS_WITHDRAW:
1162 state = "withdraw";
1163 break;
1164 case GFS2_ERRORS_PANIC:
1165 state = "panic";
1166 break;
1167 default:
1168 state = "unknown";
1169 break;
1170 }
1171 seq_printf(s, ",errors=%s", state);
1172 }
Steven Whitehousecdcfde62009-10-30 10:48:53 +00001173 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001174 seq_puts(s, ",nobarrier");
Steven Whitehouse913a71d2010-05-06 11:03:29 +01001175 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001176 seq_puts(s, ",demote_interface_used");
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001177 if (args->ar_rgrplvb)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001178 seq_puts(s, ",rgrplvb");
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001179 if (args->ar_loccookie)
1180 seq_puts(s, ",loccookie");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001181 return 0;
1182}
1183
Steven Whitehousef42ab082011-04-14 16:50:31 +01001184static void gfs2_final_release_pages(struct gfs2_inode *ip)
1185{
1186 struct inode *inode = &ip->i_inode;
1187 struct gfs2_glock *gl = ip->i_gl;
1188
1189 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1190 truncate_inode_pages(&inode->i_data, 0);
1191
Bob Peterson638803d2019-06-06 07:33:38 -05001192 if (atomic_read(&gl->gl_revokes) == 0) {
Steven Whitehousef42ab082011-04-14 16:50:31 +01001193 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1194 clear_bit(GLF_DIRTY, &gl->gl_flags);
1195 }
1196}
1197
1198static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1199{
1200 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001201 struct gfs2_rgrpd *rgd;
Bob Peterson564e12b2011-11-21 13:36:17 -05001202 struct gfs2_holder gh;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001203 int error;
1204
1205 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001206 gfs2_consist_inode(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001207 return -EIO;
1208 }
1209
Bob Peterson8e2e0042012-07-19 08:12:40 -04001210 error = gfs2_rindex_update(sdp);
1211 if (error)
1212 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001213
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001214 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001215 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001216 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001217
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001218 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001219 if (!rgd) {
1220 gfs2_consist_inode(ip);
1221 error = -EIO;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001222 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001223 }
1224
Bob Peterson564e12b2011-11-21 13:36:17 -05001225 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001226 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001227 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001228
Steven Whitehouse4667a0e2011-04-18 14:18:09 +01001229 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1230 sdp->sd_jdesc->jd_blocks);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001231 if (error)
1232 goto out_rg_gunlock;
1233
1234 gfs2_free_di(rgd, ip);
1235
1236 gfs2_final_release_pages(ip);
1237
1238 gfs2_trans_end(sdp);
1239
1240out_rg_gunlock:
Bob Peterson564e12b2011-11-21 13:36:17 -05001241 gfs2_glock_dq_uninit(&gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001242out_qs:
1243 gfs2_quota_unhold(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001244 return error;
1245}
1246
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001247/**
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001248 * gfs2_glock_put_eventually
1249 * @gl: The glock to put
1250 *
1251 * When under memory pressure, trigger a deferred glock put to make sure we
1252 * won't call into DLM and deadlock. Otherwise, put the glock directly.
1253 */
1254
1255static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1256{
1257 if (current->flags & PF_MEMALLOC)
1258 gfs2_glock_queue_put(gl);
1259 else
1260 gfs2_glock_put(gl);
1261}
1262
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001263static bool gfs2_upgrade_iopen_glock(struct inode *inode)
1264{
1265 struct gfs2_inode *ip = GFS2_I(inode);
1266 struct gfs2_sbd *sdp = GFS2_SB(inode);
1267 struct gfs2_holder *gh = &ip->i_iopen_gh;
1268 long timeout = 5 * HZ;
1269 int error;
1270
1271 gh->gh_flags |= GL_NOCACHE;
1272 gfs2_glock_dq_wait(gh);
1273
1274 /*
1275 * If there are no other lock holders, we'll get the lock immediately.
1276 * Otherwise, the other nodes holding the lock will be notified about
1277 * our locking request. If they don't have the inode open, they'll
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001278 * evict the cached inode and release the lock. Otherwise, if they
1279 * poke the inode glock, we'll take this as an indication that they
1280 * still need the iopen glock and that they'll take care of deleting
1281 * the inode when they're done. As a last resort, if another node
1282 * keeps holding the iopen glock without showing any activity on the
1283 * inode glock, we'll eventually time out.
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001284 *
1285 * Note that we're passing the LM_FLAG_TRY_1CB flag to the first
1286 * locking request as an optimization to notify lock holders as soon as
1287 * possible. Without that flag, they'd be notified implicitly by the
1288 * second locking request.
1289 */
1290
1291 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, gh);
1292 error = gfs2_glock_nq(gh);
1293 if (error != GLR_TRYFAILED)
1294 return !error;
1295
1296 gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1297 error = gfs2_glock_nq(gh);
1298 if (error)
1299 return false;
1300
1301 timeout = wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001302 !test_bit(HIF_WAIT, &gh->gh_iflags) ||
1303 test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags),
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001304 timeout);
1305 if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1306 gfs2_glock_dq(gh);
1307 return false;
1308 }
1309 return true;
1310}
1311
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001312/**
Bob Peterson53dbc27e2020-09-11 10:30:26 -05001313 * evict_should_delete - determine whether the inode is eligible for deletion
1314 * @inode: The inode to evict
1315 *
1316 * This function determines whether the evicted inode is eligible to be deleted
1317 * and locks the inode glock.
1318 *
1319 * Returns: the fate of the dinode
1320 */
1321static enum dinode_demise evict_should_delete(struct inode *inode,
1322 struct gfs2_holder *gh)
1323{
1324 struct gfs2_inode *ip = GFS2_I(inode);
1325 struct super_block *sb = inode->i_sb;
1326 struct gfs2_sbd *sdp = sb->s_fs_info;
1327 int ret;
1328
1329 if (test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1330 BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));
1331 goto should_delete;
1332 }
1333
1334 if (test_bit(GIF_DEFERRED_DELETE, &ip->i_flags))
1335 return SHOULD_DEFER_EVICTION;
1336
1337 /* Deletes should never happen under memory pressure anymore. */
1338 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1339 return SHOULD_DEFER_EVICTION;
1340
1341 /* Must not read inode block until block type has been verified */
1342 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
1343 if (unlikely(ret)) {
1344 glock_clear_object(ip->i_iopen_gh.gh_gl, ip);
1345 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1346 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1347 return SHOULD_DEFER_EVICTION;
1348 }
1349
1350 if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
1351 return SHOULD_NOT_DELETE_DINODE;
1352 ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1353 if (ret)
1354 return SHOULD_NOT_DELETE_DINODE;
1355
1356 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1357 ret = gfs2_inode_refresh(ip);
1358 if (ret)
1359 return SHOULD_NOT_DELETE_DINODE;
1360 }
1361
1362 /*
1363 * The inode may have been recreated in the meantime.
1364 */
1365 if (inode->i_nlink)
1366 return SHOULD_NOT_DELETE_DINODE;
1367
1368should_delete:
1369 if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1370 test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1371 if (!gfs2_upgrade_iopen_glock(inode)) {
1372 gfs2_holder_uninit(&ip->i_iopen_gh);
1373 return SHOULD_NOT_DELETE_DINODE;
1374 }
1375 }
1376 return SHOULD_DELETE_DINODE;
1377}
1378
1379/**
Bob Peterson6e7e9a52020-09-11 09:29:25 -05001380 * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
1381 * @inode: The inode to evict
1382 */
1383static int evict_unlinked_inode(struct inode *inode)
1384{
1385 struct gfs2_inode *ip = GFS2_I(inode);
1386 int ret;
1387
1388 if (S_ISDIR(inode->i_mode) &&
1389 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1390 ret = gfs2_dir_exhash_dealloc(ip);
1391 if (ret)
1392 goto out;
1393 }
1394
1395 if (ip->i_eattr) {
1396 ret = gfs2_ea_dealloc(ip);
1397 if (ret)
1398 goto out;
1399 }
1400
1401 if (!gfs2_is_stuffed(ip)) {
1402 ret = gfs2_file_dealloc(ip);
1403 if (ret)
1404 goto out;
1405 }
1406
1407 /* We're about to clear the bitmap for the dinode, but as soon as we
1408 do, gfs2_create_inode can create another inode at the same block
1409 location and try to set gl_object again. We clear gl_object here so
1410 that subsequent inode creates don't see an old gl_object. */
1411 glock_clear_object(ip->i_gl, ip);
1412 ret = gfs2_dinode_dealloc(ip);
1413 gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
1414out:
1415 return ret;
1416}
1417
Bob Petersond90be6a2020-09-11 14:53:52 -05001418/*
1419 * evict_linked_inode - evict an inode whose dinode has not been unlinked
1420 * @inode: The inode to evict
1421 */
1422static int evict_linked_inode(struct inode *inode)
1423{
1424 struct super_block *sb = inode->i_sb;
1425 struct gfs2_sbd *sdp = sb->s_fs_info;
1426 struct gfs2_inode *ip = GFS2_I(inode);
1427 struct address_space *metamapping;
1428 int ret;
1429
1430 gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1431 GFS2_LFC_EVICT_INODE);
1432 metamapping = gfs2_glock2aspace(ip->i_gl);
1433 if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1434 filemap_fdatawrite(metamapping);
1435 filemap_fdatawait(metamapping);
1436 }
1437 write_inode_now(inode, 1);
1438 gfs2_ail_flush(ip->i_gl, 0);
1439
1440 ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1441 if (ret)
1442 return ret;
1443
1444 /* Needs to be done before glock release & also in a transaction */
1445 truncate_inode_pages(&inode->i_data, 0);
1446 truncate_inode_pages(metamapping, 0);
1447 gfs2_trans_end(sdp);
1448 return 0;
1449}
1450
Bob Peterson6e7e9a52020-09-11 09:29:25 -05001451/**
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001452 * gfs2_evict_inode - Remove an inode from cache
1453 * @inode: The inode to evict
1454 *
1455 * There are three cases to consider:
1456 * 1. i_nlink == 0, we are final opener (and must deallocate)
1457 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1458 * 3. i_nlink > 0
1459 *
1460 * If the fs is read only, then we have to treat all cases as per #3
1461 * since we are unable to do any deallocation. The inode will be
1462 * deallocated by the next read/write node to attempt an allocation
1463 * in the same resource group
1464 *
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001465 * We have to (at the moment) hold the inodes main lock to cover
1466 * the gap between unlocking the shared lock on the iopen lock and
1467 * taking the exclusive lock. I'd rather do a shared -> exclusive
1468 * conversion on the iopen lock, but we can change that later. This
1469 * is safe, just less efficient.
1470 */
1471
Al Virod5c15152010-06-07 11:05:19 -04001472static void gfs2_evict_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001473{
Steven Whitehouse001e8e82011-03-30 14:17:51 +01001474 struct super_block *sb = inode->i_sb;
1475 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001476 struct gfs2_inode *ip = GFS2_I(inode);
1477 struct gfs2_holder gh;
Bob Peterson23d828f2020-09-11 10:56:31 -05001478 int ret;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001479
Abhi Das05978802014-03-31 10:33:17 -05001480 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1481 clear_inode(inode);
1482 return;
1483 }
1484
David Howellsbc98a422017-07-17 08:45:34 +01001485 if (inode->i_nlink || sb_rdonly(sb))
Al Virod5c15152010-06-07 11:05:19 -04001486 goto out;
1487
Bob Peterson53dbc27e2020-09-11 10:30:26 -05001488 gfs2_holder_mark_uninitialized(&gh);
1489 ret = evict_should_delete(inode, &gh);
1490 if (ret == SHOULD_DEFER_EVICTION)
Andreas Gruenbacher8c7b9262020-01-13 22:16:17 +01001491 goto out;
Bob Peterson0a0d9f52020-09-16 08:50:44 -05001492 if (ret == SHOULD_DELETE_DINODE)
1493 ret = evict_unlinked_inode(inode);
1494 else
1495 ret = evict_linked_inode(inode);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001496
Bob Petersona097dc7e2015-07-16 08:28:04 -05001497 if (gfs2_rs_active(&ip->i_res))
1498 gfs2_rs_deltree(&ip->i_res);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001499
Bob Peterson240c6232017-07-18 12:36:01 -05001500 if (gfs2_holder_initialized(&gh)) {
1501 glock_clear_object(ip->i_gl, ip);
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001502 gfs2_glock_dq_uninit(&gh);
Bob Peterson240c6232017-07-18 12:36:01 -05001503 }
Bob Peterson23d828f2020-09-11 10:56:31 -05001504 if (ret && ret != GLR_TRYFAILED && ret != -EROFS)
1505 fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001506out:
Johannes Weiner91b0abe2014-04-03 14:47:49 -07001507 truncate_inode_pages_final(&inode->i_data);
Bob Peterson2fba46a2020-02-27 12:47:53 -06001508 if (ip->i_qadata)
1509 gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
Andreas Gruenbacher15955482020-03-06 10:32:35 -06001510 gfs2_rs_delete(ip, NULL);
Steven Whitehouse45138992013-01-28 09:30:07 +00001511 gfs2_ordered_del_inode(ip);
Jan Karadbd57682012-05-03 14:48:02 +02001512 clear_inode(inode);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001513 gfs2_dir_hash_inval(ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001514 if (ip->i_gl) {
1515 glock_clear_object(ip->i_gl, ip);
1516 wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1517 gfs2_glock_add_to_lru(ip->i_gl);
1518 gfs2_glock_put_eventually(ip->i_gl);
1519 ip->i_gl = NULL;
1520 }
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001521 if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001522 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1523
1524 glock_clear_object(gl, ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001525 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1526 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1527 gfs2_glock_dq(&ip->i_iopen_gh);
1528 }
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001529 gfs2_glock_hold(gl);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001530 gfs2_holder_uninit(&ip->i_iopen_gh);
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001531 gfs2_glock_put_eventually(gl);
Al Virod5c15152010-06-07 11:05:19 -04001532 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001533}
1534
1535static struct inode *gfs2_alloc_inode(struct super_block *sb)
1536{
1537 struct gfs2_inode *ip;
1538
1539 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001540 if (!ip)
1541 return NULL;
1542 ip->i_flags = 0;
1543 ip->i_gl = NULL;
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001544 gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001545 memset(&ip->i_res, 0, sizeof(ip->i_res));
1546 RB_CLEAR_NODE(&ip->i_res.rs_node);
1547 ip->i_rahead = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001548 return &ip->i_inode;
1549}
1550
Al Viro784494e2019-04-15 19:45:26 -04001551static void gfs2_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001552{
Al Viro784494e2019-04-15 19:45:26 -04001553 kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001554}
1555
Abhi Das97fd7342020-10-20 15:58:04 -05001556extern void free_local_statfs_inodes(struct gfs2_sbd *sdp)
1557{
1558 struct local_statfs_inode *lsi, *safe;
1559
1560 /* Run through the statfs inodes list to iput and free memory */
1561 list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
1562 if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
1563 sdp->sd_sc_inode = NULL; /* belongs to this node */
1564 if (lsi->si_sc_inode)
1565 iput(lsi->si_sc_inode);
1566 list_del(&lsi->si_list);
1567 kfree(lsi);
1568 }
1569}
1570
1571extern struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
1572 unsigned int index)
1573{
1574 struct local_statfs_inode *lsi;
1575
1576 /* Return the local (per node) statfs inode in the
1577 * sdp->sd_sc_inodes_list corresponding to the 'index'. */
1578 list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
1579 if (lsi->si_jid == index)
1580 return lsi->si_sc_inode;
1581 }
1582 return NULL;
1583}
1584
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001585const struct super_operations gfs2_super_ops = {
1586 .alloc_inode = gfs2_alloc_inode,
Al Viro784494e2019-04-15 19:45:26 -04001587 .free_inode = gfs2_free_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001588 .write_inode = gfs2_write_inode,
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001589 .dirty_inode = gfs2_dirty_inode,
Al Virod5c15152010-06-07 11:05:19 -04001590 .evict_inode = gfs2_evict_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001591 .put_super = gfs2_put_super,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001592 .sync_fs = gfs2_sync_fs,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001593 .freeze_super = gfs2_freeze,
1594 .thaw_super = gfs2_unfreeze,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001595 .statfs = gfs2_statfs,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001596 .drop_inode = gfs2_drop_inode,
1597 .show_options = gfs2_show_options,
1598};
1599