blob: d2b7ecbd1b1503a89b0116e66cd08986e0da258d [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;
84 int found = 0;
85
86 list_for_each_entry(jd, head, jd_list) {
87 if (jd->jd_jid == jid) {
88 found = 1;
89 break;
90 }
91 }
92
93 if (!found)
94 jd = NULL;
95
96 return jd;
97}
98
99struct gfs2_jdesc *gfs2_jdesc_find(struct gfs2_sbd *sdp, unsigned int jid)
100{
101 struct gfs2_jdesc *jd;
102
103 spin_lock(&sdp->sd_jindex_spin);
104 jd = jdesc_find_i(&sdp->sd_jindex_list, jid);
105 spin_unlock(&sdp->sd_jindex_spin);
106
107 return jd;
108}
109
David Teiglandb3b94fa2006-01-16 16:50:04 +0000110int gfs2_jdesc_check(struct gfs2_jdesc *jd)
111{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400112 struct gfs2_inode *ip = GFS2_I(jd->jd_inode);
113 struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode);
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100114 u64 size = i_size_read(jd->jd_inode);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000115
Fabian Frederick47a9a522016-08-02 12:05:27 -0500116 if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30)))
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000118
Steven Whitehousea2e0f792010-08-11 09:53:11 +0100119 jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift;
120
121 if (gfs2_write_alloc_required(ip, 0, size)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000122 gfs2_consist_inode(ip);
Bob Peterson461cb412010-06-24 19:21:20 -0400123 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000124 }
125
Bob Peterson461cb412010-06-24 19:21:20 -0400126 return 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000127}
128
David Teiglandb3b94fa2006-01-16 16:50:04 +0000129/**
130 * gfs2_make_fs_rw - Turn a Read-Only FS into a Read-Write one
131 * @sdp: the filesystem
132 *
133 * Returns: errno
134 */
135
136int gfs2_make_fs_rw(struct gfs2_sbd *sdp)
137{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400138 struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode);
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500139 struct gfs2_glock *j_gl = ip->i_gl;
Al Viro55167622006-10-13 21:47:13 -0400140 struct gfs2_log_header_host head;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000141 int error;
142
Steven Whitehouse1a14d3a2006-11-20 10:37:45 -0500143 j_gl->gl_ops->go_inval(j_gl, DIO_METADATA);
Bob Petersonb4bbb772021-05-14 07:42:33 -0500144 if (gfs2_withdrawn(sdp))
145 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000146
Abhi Dasf4686c22019-05-02 14:17:40 -0500147 error = gfs2_find_jhead(sdp->sd_jdesc, &head, false);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100148 if (error || gfs2_withdrawn(sdp))
Bob Petersonb4bbb772021-05-14 07:42:33 -0500149 return error;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000150
151 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
152 gfs2_consist(sdp);
Bob Petersonb4bbb772021-05-14 07:42:33 -0500153 return -EIO;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000154 }
155
156 /* Initialize some head of the log stuff */
157 sdp->sd_log_sequence = head.lh_sequence + 1;
158 gfs2_log_pointers_init(sdp, head.lh_blkno);
159
David Teiglandb3b94fa2006-01-16 16:50:04 +0000160 error = gfs2_quota_init(sdp);
Bob Petersonb4bbb772021-05-14 07:42:33 -0500161 if (!error && !gfs2_withdrawn(sdp))
162 set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163 return error;
164}
165
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500166void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100167{
168 const struct gfs2_statfs_change *str = buf;
169
170 sc->sc_total = be64_to_cpu(str->sc_total);
171 sc->sc_free = be64_to_cpu(str->sc_free);
172 sc->sc_dinodes = be64_to_cpu(str->sc_dinodes);
173}
174
Abhi Das73092692020-10-20 15:58:03 -0500175void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf)
Steven Whitehousebb8d8a62007-06-01 14:11:58 +0100176{
177 struct gfs2_statfs_change *str = buf;
178
179 str->sc_total = cpu_to_be64(sc->sc_total);
180 str->sc_free = cpu_to_be64(sc->sc_free);
181 str->sc_dinodes = cpu_to_be64(sc->sc_dinodes);
182}
183
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184int gfs2_statfs_init(struct gfs2_sbd *sdp)
185{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400186 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400187 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400188 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400189 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000190 struct buffer_head *m_bh, *l_bh;
191 struct gfs2_holder gh;
192 int error;
193
194 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
195 &gh);
196 if (error)
197 return error;
198
199 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
200 if (error)
201 goto out;
202
203 if (sdp->sd_args.ar_spectator) {
204 spin_lock(&sdp->sd_statfs_spin);
205 gfs2_statfs_change_in(m_sc, m_bh->b_data +
206 sizeof(struct gfs2_dinode));
207 spin_unlock(&sdp->sd_statfs_spin);
208 } else {
209 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
210 if (error)
211 goto out_m_bh;
212
213 spin_lock(&sdp->sd_statfs_spin);
214 gfs2_statfs_change_in(m_sc, m_bh->b_data +
215 sizeof(struct gfs2_dinode));
216 gfs2_statfs_change_in(l_sc, l_bh->b_data +
217 sizeof(struct gfs2_dinode));
218 spin_unlock(&sdp->sd_statfs_spin);
219
220 brelse(l_bh);
221 }
222
Steven Whitehousea91ea692006-09-04 12:04:26 -0400223out_m_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000224 brelse(m_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400225out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000226 gfs2_glock_dq_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000227 return 0;
228}
229
Steven Whitehousecd915492006-09-04 12:49:07 -0400230void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free,
231 s64 dinodes)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000232{
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400233 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400234 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500235 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000236 struct buffer_head *l_bh;
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500237 s64 x, y;
238 int need_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239 int error;
240
241 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
242 if (error)
243 return;
244
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000245 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000246
247 spin_lock(&sdp->sd_statfs_spin);
248 l_sc->sc_total += total;
249 l_sc->sc_free += free;
250 l_sc->sc_dinodes += dinodes;
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400251 gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode));
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500252 if (sdp->sd_args.ar_statfs_percent) {
253 x = 100 * l_sc->sc_free;
254 y = m_sc->sc_free * sdp->sd_args.ar_statfs_percent;
255 if (x >= y || x <= -y)
256 need_sync = 1;
257 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000258 spin_unlock(&sdp->sd_statfs_spin);
259
260 brelse(l_bh);
Benjamin Marzinskic14f5732009-10-26 13:29:47 -0500261 if (need_sync)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500262 gfs2_wake_up_statfs(sdp);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000263}
264
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500265void update_statfs(struct gfs2_sbd *sdp, struct buffer_head *m_bh,
266 struct buffer_head *l_bh)
267{
268 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
269 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
270 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
271 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
272
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000273 gfs2_trans_add_meta(l_ip->i_gl, l_bh);
Bob Peterson901c6c62015-03-11 09:52:31 -0500274 gfs2_trans_add_meta(m_ip->i_gl, m_bh);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500275
276 spin_lock(&sdp->sd_statfs_spin);
277 m_sc->sc_total += l_sc->sc_total;
278 m_sc->sc_free += l_sc->sc_free;
279 m_sc->sc_dinodes += l_sc->sc_dinodes;
280 memset(l_sc, 0, sizeof(struct gfs2_statfs_change));
281 memset(l_bh->b_data + sizeof(struct gfs2_dinode),
282 0, sizeof(struct gfs2_statfs_change));
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500283 gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode));
Bob Peterson901c6c62015-03-11 09:52:31 -0500284 spin_unlock(&sdp->sd_statfs_spin);
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500285}
286
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100287int gfs2_statfs_sync(struct super_block *sb, int type)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288{
Steven Whitehouse8c42d632009-09-11 14:36:44 +0100289 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400290 struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode);
291 struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode);
Al Virobd209cc2006-10-13 23:43:19 -0400292 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
293 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000294 struct gfs2_holder gh;
295 struct buffer_head *m_bh, *l_bh;
296 int error;
297
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600298 sb_start_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000299 error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE,
300 &gh);
301 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600302 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000303
304 error = gfs2_meta_inode_buffer(m_ip, &m_bh);
305 if (error)
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600306 goto out_unlock;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307
308 spin_lock(&sdp->sd_statfs_spin);
309 gfs2_statfs_change_in(m_sc, m_bh->b_data +
Steven Whitehouse907b9bc2006-09-25 09:26:04 -0400310 sizeof(struct gfs2_dinode));
David Teiglandb3b94fa2006-01-16 16:50:04 +0000311 if (!l_sc->sc_total && !l_sc->sc_free && !l_sc->sc_dinodes) {
312 spin_unlock(&sdp->sd_statfs_spin);
313 goto out_bh;
314 }
315 spin_unlock(&sdp->sd_statfs_spin);
316
317 error = gfs2_meta_inode_buffer(l_ip, &l_bh);
318 if (error)
319 goto out_bh;
320
321 error = gfs2_trans_begin(sdp, 2 * RES_DINODE, 0);
322 if (error)
323 goto out_bh2;
324
Benjamin Marzinski1946f702009-06-25 15:09:51 -0500325 update_statfs(sdp, m_bh, l_bh);
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -0500326 sdp->sd_statfs_force_sync = 0;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000327
328 gfs2_trans_end(sdp);
329
Steven Whitehousea91ea692006-09-04 12:04:26 -0400330out_bh2:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000331 brelse(l_bh);
Steven Whitehousea91ea692006-09-04 12:04:26 -0400332out_bh:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000333 brelse(m_bh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600334out_unlock:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000335 gfs2_glock_dq_uninit(&gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600336out:
337 sb_end_write(sb);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000338 return error;
339}
340
David Teiglandb3b94fa2006-01-16 16:50:04 +0000341struct lfcc {
342 struct list_head list;
343 struct gfs2_holder gh;
344};
345
346/**
347 * gfs2_lock_fs_check_clean - Stop all writes to the FS and check that all
348 * journals are clean
349 * @sdp: the file system
350 * @state: the state to put the transaction lock into
351 * @t_gh: the hold on the transaction lock
352 *
353 * Returns: errno
354 */
355
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500356static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000357{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500358 struct gfs2_inode *ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000359 struct gfs2_jdesc *jd;
360 struct lfcc *lfcc;
361 LIST_HEAD(list);
Al Viro55167622006-10-13 21:47:13 -0400362 struct gfs2_log_header_host lh;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000363 int error;
364
David Teiglandb3b94fa2006-01-16 16:50:04 +0000365 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
366 lfcc = kmalloc(sizeof(struct lfcc), GFP_KERNEL);
367 if (!lfcc) {
368 error = -ENOMEM;
369 goto out;
370 }
Steven Whitehousefeaa7bb2006-06-14 15:32:57 -0400371 ip = GFS2_I(jd->jd_inode);
372 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000373 if (error) {
374 kfree(lfcc);
375 goto out;
376 }
377 list_add(&lfcc->list, &list);
378 }
379
Benjamin Marzinski24972552014-05-01 22:26:55 -0500380 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE,
Bob Petersonc860f8f2020-06-25 14:42:17 -0500381 LM_FLAG_NOEXP, &sdp->sd_freeze_gh);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500382 if (error)
383 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000384
385 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
386 error = gfs2_jdesc_check(jd);
387 if (error)
388 break;
Abhi Dasf4686c22019-05-02 14:17:40 -0500389 error = gfs2_find_jhead(jd, &lh, false);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000390 if (error)
391 break;
392 if (!(lh.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) {
393 error = -EBUSY;
394 break;
395 }
396 }
397
398 if (error)
Bob Peterson49787b12020-12-22 14:43:27 -0600399 gfs2_freeze_unlock(&sdp->sd_freeze_gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000400
Steven Whitehousea91ea692006-09-04 12:04:26 -0400401out:
David Teiglandb3b94fa2006-01-16 16:50:04 +0000402 while (!list_empty(&list)) {
Andreas Gruenbacher969183b2020-02-03 19:22:45 +0100403 lfcc = list_first_entry(&list, struct lfcc, list);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000404 list_del(&lfcc->list);
405 gfs2_glock_dq_uninit(&lfcc->gh);
406 kfree(lfcc);
407 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000408 return error;
409}
410
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100411void gfs2_dinode_out(const struct gfs2_inode *ip, void *buf)
412{
413 struct gfs2_dinode *str = buf;
414
415 str->di_header.mh_magic = cpu_to_be32(GFS2_MAGIC);
416 str->di_header.mh_type = cpu_to_be32(GFS2_METATYPE_DI);
417 str->di_header.mh_format = cpu_to_be32(GFS2_FORMAT_DI);
418 str->di_num.no_addr = cpu_to_be64(ip->i_no_addr);
419 str->di_num.no_formal_ino = cpu_to_be64(ip->i_no_formal_ino);
420 str->di_mode = cpu_to_be32(ip->i_inode.i_mode);
Eric W. Biedermand0546422013-01-31 22:08:10 -0800421 str->di_uid = cpu_to_be32(i_uid_read(&ip->i_inode));
422 str->di_gid = cpu_to_be32(i_gid_read(&ip->i_inode));
Steven Whitehouse9eed04c2011-05-09 14:11:40 +0100423 str->di_nlink = cpu_to_be32(ip->i_inode.i_nlink);
424 str->di_size = cpu_to_be64(i_size_read(&ip->i_inode));
425 str->di_blocks = cpu_to_be64(gfs2_get_inode_blocks(&ip->i_inode));
426 str->di_atime = cpu_to_be64(ip->i_inode.i_atime.tv_sec);
427 str->di_mtime = cpu_to_be64(ip->i_inode.i_mtime.tv_sec);
428 str->di_ctime = cpu_to_be64(ip->i_inode.i_ctime.tv_sec);
429
430 str->di_goal_meta = cpu_to_be64(ip->i_goal);
431 str->di_goal_data = cpu_to_be64(ip->i_goal);
432 str->di_generation = cpu_to_be64(ip->i_generation);
433
434 str->di_flags = cpu_to_be32(ip->i_diskflags);
435 str->di_height = cpu_to_be16(ip->i_height);
436 str->di_payload_format = cpu_to_be32(S_ISDIR(ip->i_inode.i_mode) &&
437 !(ip->i_diskflags & GFS2_DIF_EXHASH) ?
438 GFS2_FORMAT_DE : 0);
439 str->di_depth = cpu_to_be16(ip->i_depth);
440 str->di_entries = cpu_to_be32(ip->i_entries);
441
442 str->di_eattr = cpu_to_be64(ip->i_eattr);
443 str->di_atime_nsec = cpu_to_be32(ip->i_inode.i_atime.tv_nsec);
444 str->di_mtime_nsec = cpu_to_be32(ip->i_inode.i_mtime.tv_nsec);
445 str->di_ctime_nsec = cpu_to_be32(ip->i_inode.i_ctime.tv_nsec);
446}
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100447
448/**
449 * gfs2_write_inode - Make sure the inode is stable on the disk
450 * @inode: The inode
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100451 * @wbc: The writeback control structure
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100452 *
453 * Returns: errno
454 */
455
Christoph Hellwiga9185b42010-03-05 09:21:37 +0100456static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100457{
458 struct gfs2_inode *ip = GFS2_I(inode);
459 struct gfs2_sbd *sdp = GFS2_SB(inode);
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100460 struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl);
Christoph Hellwigde1414a2015-01-14 10:42:36 +0100461 struct backing_dev_info *bdi = inode_to_bdi(metamapping->host);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100462 int ret = 0;
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200463 bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100464
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200465 if (flush_all)
Bob Petersonc1696fb2018-01-17 00:01:33 +0100466 gfs2_log_flush(GFS2_SB(inode), ip->i_gl,
Bob Peterson805c09072018-01-08 10:34:17 -0500467 GFS2_LOG_HEAD_FLUSH_NORMAL |
468 GFS2_LFC_WRITE_INODE);
Tejun Heoa88a3412015-05-22 17:13:28 -0400469 if (bdi->wb.dirty_exceeded)
Steven Whitehouse4667a0e2011-04-18 14:18:09 +0100470 gfs2_ail1_flush(sdp, wbc);
Steven Whitehouse1d4ec642011-08-02 13:13:20 +0100471 else
472 filemap_fdatawrite(metamapping);
Bob Petersonadbc3dd2017-10-11 16:22:07 +0200473 if (flush_all)
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100474 ret = filemap_fdatawait(metamapping);
475 if (ret)
476 mark_inode_dirty_sync(inode);
Abhi Das957a7ac2018-01-30 10:00:09 -0700477 else {
478 spin_lock(&inode->i_lock);
479 if (!(inode->i_flags & I_DIRTY))
480 gfs2_ordered_del_inode(ip);
481 spin_unlock(&inode->i_lock);
482 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100483 return ret;
484}
485
486/**
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100487 * gfs2_dirty_inode - check for atime updates
488 * @inode: The inode in question
489 * @flags: The type of dirty
490 *
491 * Unfortunately it can be called under any combination of inode
492 * glock and transaction lock, so we have to check carefully.
493 *
494 * At the moment this deals only with atime - it should be possible
495 * to expand that role in future, once a review of the locking has
496 * been carried out.
497 */
498
499static void gfs2_dirty_inode(struct inode *inode, int flags)
500{
501 struct gfs2_inode *ip = GFS2_I(inode);
502 struct gfs2_sbd *sdp = GFS2_SB(inode);
503 struct buffer_head *bh;
504 struct gfs2_holder gh;
505 int need_unlock = 0;
506 int need_endtrans = 0;
507 int ret;
508
Christoph Hellwig0e11f642018-02-21 07:54:49 -0800509 if (!(flags & I_DIRTY_INODE))
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100510 return;
Bob Petersoneb43e662019-11-14 09:52:15 -0500511 if (unlikely(gfs2_withdrawn(sdp)))
Bob Peterson0d1c7ae2017-03-03 12:37:14 -0500512 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100513 if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
514 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
515 if (ret) {
516 fs_err(sdp, "dirty_inode: glock %d\n", ret);
Bob Petersone28c02b2020-07-30 12:31:38 -0500517 gfs2_dump_glock(NULL, ip->i_gl, true);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100518 return;
519 }
520 need_unlock = 1;
Benjamin Marzinski3d162682012-11-06 00:49:28 -0600521 } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE))
522 return;
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100523
524 if (current->journal_info == NULL) {
525 ret = gfs2_trans_begin(sdp, RES_DINODE, 0);
526 if (ret) {
527 fs_err(sdp, "dirty_inode: gfs2_trans_begin %d\n", ret);
528 goto out;
529 }
530 need_endtrans = 1;
531 }
532
533 ret = gfs2_meta_inode_buffer(ip, &bh);
534 if (ret == 0) {
Steven Whitehouse350a9b02012-12-14 12:36:02 +0000535 gfs2_trans_add_meta(ip->i_gl, bh);
Steven Whitehouseab9bbda2011-08-15 14:20:36 +0100536 gfs2_dinode_out(ip, bh->b_data);
537 brelse(bh);
538 }
539
540 if (need_endtrans)
541 gfs2_trans_end(sdp);
542out:
543 if (need_unlock)
544 gfs2_glock_dq_uninit(&gh);
545}
546
547/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100548 * gfs2_make_fs_ro - Turn a Read-Write FS into a Read-Only one
549 * @sdp: the filesystem
550 *
551 * Returns: errno
552 */
553
Andrew Price1f52aa02019-03-27 14:46:00 +0000554int gfs2_make_fs_ro(struct gfs2_sbd *sdp)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100555{
Bob Peterson601ef0d2020-01-28 20:23:45 +0100556 int error = 0;
557 int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100558
Andreas Gruenbachera0e3cc62020-01-16 20:12:26 +0100559 gfs2_flush_delete_work(sdp);
Bob Peterson601ef0d2020-01-28 20:23:45 +0100560 if (!log_write_allowed && current == sdp->sd_quotad_process)
561 fs_warn(sdp, "The quotad daemon is withdrawing.\n");
562 else if (sdp->sd_quotad_process)
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600563 kthread_stop(sdp->sd_quotad_process);
564 sdp->sd_quotad_process = NULL;
Bob Peterson601ef0d2020-01-28 20:23:45 +0100565
566 if (!log_write_allowed && current == sdp->sd_logd_process)
567 fs_warn(sdp, "The logd daemon is withdrawing.\n");
568 else if (sdp->sd_logd_process)
Bob Peterson5b3a9f32019-04-26 08:11:27 -0600569 kthread_stop(sdp->sd_logd_process);
570 sdp->sd_logd_process = NULL;
Steven Whitehouse8ad151c2013-12-12 11:34:09 +0000571
Bob Peterson601ef0d2020-01-28 20:23:45 +0100572 if (log_write_allowed) {
573 gfs2_quota_sync(sdp->sd_vfs, 0);
574 gfs2_statfs_sync(sdp->sd_vfs, 0);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100575
Bob Peterson601ef0d2020-01-28 20:23:45 +0100576 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_SHUTDOWN |
577 GFS2_LFC_MAKE_FS_RO);
578 wait_event(sdp->sd_reserving_log_wait,
579 atomic_read(&sdp->sd_reserving_log) == 0);
580 gfs2_assert_warn(sdp, atomic_read(&sdp->sd_log_blks_free) ==
581 sdp->sd_jdesc->jd_blocks);
582 } else {
583 wait_event_timeout(sdp->sd_reserving_log_wait,
584 atomic_read(&sdp->sd_reserving_log) == 0,
585 HZ * 5);
586 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100587 gfs2_quota_cleanup(sdp);
588
Bob Peterson601ef0d2020-01-28 20:23:45 +0100589 if (!log_write_allowed)
590 sdp->sd_vfs->s_flags |= SB_RDONLY;
591
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100592 return error;
593}
594
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100595/**
596 * gfs2_put_super - Unmount the filesystem
597 * @sb: The VFS superblock
598 *
599 */
600
601static void gfs2_put_super(struct super_block *sb)
602{
603 struct gfs2_sbd *sdp = sb->s_fs_info;
604 int error;
605 struct gfs2_jdesc *jd;
606
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100607 /* No more recovery requests */
608 set_bit(SDF_NORECOVERY, &sdp->sd_flags);
609 smp_mb();
610
611 /* Wait on outstanding recovery */
612restart:
613 spin_lock(&sdp->sd_jindex_spin);
614 list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) {
615 if (!test_bit(JDF_RECOVERY, &jd->jd_flags))
616 continue;
617 spin_unlock(&sdp->sd_jindex_spin);
618 wait_on_bit(&jd->jd_flags, JDF_RECOVERY,
NeilBrown74316202014-07-07 15:16:04 +1000619 TASK_UNINTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100620 goto restart;
621 }
622 spin_unlock(&sdp->sd_jindex_spin);
623
David Howellsbc98a422017-07-17 08:45:34 +0100624 if (!sb_rdonly(sb)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100625 error = gfs2_make_fs_ro(sdp);
626 if (error)
627 gfs2_io_error(sdp);
628 }
Andreas Gruenbacher5a61ae12020-08-28 23:44:36 +0200629 WARN_ON(gfs2_withdrawing(sdp));
630
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100631 /* At this point, we're through modifying the disk */
632
633 /* Release stuff */
634
635 iput(sdp->sd_jindex);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100636 iput(sdp->sd_statfs_inode);
637 iput(sdp->sd_rindex);
638 iput(sdp->sd_quota_inode);
639
640 gfs2_glock_put(sdp->sd_rename_gl);
Benjamin Marzinski24972552014-05-01 22:26:55 -0500641 gfs2_glock_put(sdp->sd_freeze_gl);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100642
643 if (!sdp->sd_args.ar_spectator) {
Bob Peterson601ef0d2020-01-28 20:23:45 +0100644 if (gfs2_holder_initialized(&sdp->sd_journal_gh))
645 gfs2_glock_dq_uninit(&sdp->sd_journal_gh);
646 if (gfs2_holder_initialized(&sdp->sd_jinode_gh))
647 gfs2_glock_dq_uninit(&sdp->sd_jinode_gh);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100648 gfs2_glock_dq_uninit(&sdp->sd_sc_gh);
649 gfs2_glock_dq_uninit(&sdp->sd_qc_gh);
Abhi Das97fd7342020-10-20 15:58:04 -0500650 free_local_statfs_inodes(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100651 iput(sdp->sd_qc_inode);
652 }
653
654 gfs2_glock_dq_uninit(&sdp->sd_live_gh);
655 gfs2_clear_rgrpd(sdp);
656 gfs2_jindex_free(sdp);
657 /* Take apart glock structures and buffer lists */
658 gfs2_gl_hash_clear(sdp);
Bob Petersona9dd9452020-10-27 10:10:02 -0500659 truncate_inode_pages_final(&sdp->sd_aspace);
Bob Petersonb2fb7da2017-07-28 07:22:55 -0500660 gfs2_delete_debugfs_file(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100661 /* Unmount the locking protocol */
662 gfs2_lm_unmount(sdp);
663
664 /* At this point, we're through participating in the lockspace */
665 gfs2_sys_fs_del(sdp);
Jamie Ilesc2a04b02020-10-12 14:13:09 +0100666 free_sbd(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100667}
668
669/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100670 * gfs2_sync_fs - sync the filesystem
671 * @sb: the superblock
672 *
673 * Flushes the log to disk.
674 */
675
676static int gfs2_sync_fs(struct super_block *sb, int wait)
677{
Steven Whitehouse1027efa2011-03-30 16:13:25 +0100678 struct gfs2_sbd *sdp = sb->s_fs_info;
Jan Karaa1177822012-07-03 16:45:29 +0200679
680 gfs2_quota_sync(sb, -1);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500681 if (wait)
Bob Peterson805c09072018-01-08 10:34:17 -0500682 gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL |
683 GFS2_LFC_SYNC_FS);
Bob Peterson942b0cd2017-08-16 11:30:06 -0500684 return sdp->sd_log_error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100685}
686
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600687void gfs2_freeze_func(struct work_struct *work)
688{
689 int error;
690 struct gfs2_holder freeze_gh;
691 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_freeze_work);
692 struct super_block *sb = sdp->sd_vfs;
693
694 atomic_inc(&sb->s_active);
Bob Peterson49787b12020-12-22 14:43:27 -0600695 error = gfs2_freeze_lock(sdp, &freeze_gh, 0);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600696 if (error) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600697 gfs2_assert_withdraw(sdp, 0);
Abhi Das8f9182192019-04-30 16:53:47 -0500698 } else {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600699 atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN);
700 error = thaw_super(sb);
701 if (error) {
Bob Petersonf29e62e2019-05-13 09:42:18 -0500702 fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n",
703 error);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600704 gfs2_assert_withdraw(sdp, 0);
705 }
Bob Peterson49787b12020-12-22 14:43:27 -0600706 gfs2_freeze_unlock(&freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600707 }
708 deactivate_super(sb);
Abhi Das8f9182192019-04-30 16:53:47 -0500709 clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags);
710 wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600711 return;
712}
713
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100714/**
715 * gfs2_freeze - prevent further writes to the filesystem
716 * @sb: the VFS structure for the filesystem
717 *
718 */
719
720static int gfs2_freeze(struct super_block *sb)
721{
722 struct gfs2_sbd *sdp = sb->s_fs_info;
Bob Peterson6c6d5832021-03-25 08:51:13 -0400723 int error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100724
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600725 mutex_lock(&sdp->sd_freeze_mutex);
Bob Peterson6c6d5832021-03-25 08:51:13 -0400726 if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN) {
727 error = -EBUSY;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600728 goto out;
Bob Peterson6c6d5832021-03-25 08:51:13 -0400729 }
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600730
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100731 for (;;) {
Bob Peterson60528af2019-11-14 09:53:36 -0500732 if (gfs2_withdrawn(sdp)) {
733 error = -EINVAL;
734 goto out;
735 }
736
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500737 error = gfs2_lock_fs_check_clean(sdp);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100738 if (!error)
739 break;
740
Bob Peterson55317f52019-04-29 09:36:23 -0600741 if (error == -EBUSY)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100742 fs_err(sdp, "waiting for recovery before freeze\n");
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500743 else if (error == -EIO) {
744 fs_err(sdp, "Fatal IO error: cannot freeze gfs2 due "
745 "to recovery error.\n");
746 goto out;
747 } else {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100748 fs_err(sdp, "error freezing FS: %d\n", error);
Bob Peterson52b1cdc2019-11-15 09:42:46 -0500749 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100750 fs_err(sdp, "retrying...\n");
751 msleep(1000);
752 }
Abhi Das8f9182192019-04-30 16:53:47 -0500753 set_bit(SDF_FS_FROZEN, &sdp->sd_flags);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600754out:
755 mutex_unlock(&sdp->sd_freeze_mutex);
756 return error;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100757}
758
759/**
760 * gfs2_unfreeze - reallow writes to the filesystem
761 * @sb: the VFS structure for the filesystem
762 *
763 */
764
765static int gfs2_unfreeze(struct super_block *sb)
766{
Steven Whitehoused564053f2013-01-11 10:49:34 +0000767 struct gfs2_sbd *sdp = sb->s_fs_info;
768
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600769 mutex_lock(&sdp->sd_freeze_mutex);
Bob Peterson6c6d5832021-03-25 08:51:13 -0400770 if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN ||
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500771 !gfs2_holder_initialized(&sdp->sd_freeze_gh)) {
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600772 mutex_unlock(&sdp->sd_freeze_mutex);
Bob Peterson6c6d5832021-03-25 08:51:13 -0400773 return -EINVAL;
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600774 }
775
Bob Peterson49787b12020-12-22 14:43:27 -0600776 gfs2_freeze_unlock(&sdp->sd_freeze_gh);
Benjamin Marzinski2e60d762014-11-13 20:42:04 -0600777 mutex_unlock(&sdp->sd_freeze_mutex);
Abhi Das8f9182192019-04-30 16:53:47 -0500778 return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100779}
780
781/**
782 * statfs_fill - fill in the sg for a given RG
783 * @rgd: the RG
784 * @sc: the sc structure
785 *
786 * Returns: 0 on success, -ESTALE if the LVB is invalid
787 */
788
789static int statfs_slow_fill(struct gfs2_rgrpd *rgd,
790 struct gfs2_statfs_change_host *sc)
791{
792 gfs2_rgrp_verify(rgd);
793 sc->sc_total += rgd->rd_data;
794 sc->sc_free += rgd->rd_free;
795 sc->sc_dinodes += rgd->rd_dinodes;
796 return 0;
797}
798
799/**
800 * gfs2_statfs_slow - Stat a filesystem using asynchronous locking
801 * @sdp: the filesystem
802 * @sc: the sc info that will be returned
803 *
804 * Any error (other than a signal) will cause this routine to fall back
805 * to the synchronous version.
806 *
807 * FIXME: This really shouldn't busy wait like this.
808 *
809 * Returns: errno
810 */
811
812static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
813{
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100814 struct gfs2_rgrpd *rgd_next;
815 struct gfs2_holder *gha, *gh;
816 unsigned int slots = 64;
817 unsigned int x;
818 int done;
819 int error = 0, err;
820
821 memset(sc, 0, sizeof(struct gfs2_statfs_change_host));
Kees Cook6da2ec52018-06-12 13:55:00 -0700822 gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100823 if (!gha)
824 return -ENOMEM;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500825 for (x = 0; x < slots; x++)
826 gfs2_holder_mark_uninitialized(gha + x);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100827
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100828 rgd_next = gfs2_rgrpd_get_first(sdp);
829
830 for (;;) {
831 done = 1;
832
833 for (x = 0; x < slots; x++) {
834 gh = gha + x;
835
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500836 if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100837 err = gfs2_glock_wait(gh);
838 if (err) {
839 gfs2_holder_uninit(gh);
840 error = err;
841 } else {
Andreas Gruenbacher6f6597ba2017-06-30 07:55:08 -0500842 if (!error) {
843 struct gfs2_rgrpd *rgd =
844 gfs2_glock2rgrp(gh->gh_gl);
845
846 error = statfs_slow_fill(rgd, sc);
847 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100848 gfs2_glock_dq_uninit(gh);
849 }
850 }
851
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500852 if (gfs2_holder_initialized(gh))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100853 done = 0;
854 else if (rgd_next && !error) {
855 error = gfs2_glock_nq_init(rgd_next->rd_gl,
856 LM_ST_SHARED,
857 GL_ASYNC,
858 gh);
859 rgd_next = gfs2_rgrpd_get_next(rgd_next);
860 done = 0;
861 }
862
863 if (signal_pending(current))
864 error = -ERESTARTSYS;
865 }
866
867 if (done)
868 break;
869
870 yield();
871 }
872
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100873 kfree(gha);
874 return error;
875}
876
877/**
878 * gfs2_statfs_i - Do a statfs
879 * @sdp: the filesystem
880 * @sg: the sg structure
881 *
882 * Returns: errno
883 */
884
885static int gfs2_statfs_i(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc)
886{
887 struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master;
888 struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local;
889
890 spin_lock(&sdp->sd_statfs_spin);
891
892 *sc = *m_sc;
893 sc->sc_total += l_sc->sc_total;
894 sc->sc_free += l_sc->sc_free;
895 sc->sc_dinodes += l_sc->sc_dinodes;
896
897 spin_unlock(&sdp->sd_statfs_spin);
898
899 if (sc->sc_free < 0)
900 sc->sc_free = 0;
901 if (sc->sc_free > sc->sc_total)
902 sc->sc_free = sc->sc_total;
903 if (sc->sc_dinodes < 0)
904 sc->sc_dinodes = 0;
905
906 return 0;
907}
908
909/**
910 * gfs2_statfs - Gather and return stats about the filesystem
911 * @sb: The superblock
912 * @statfsbuf: The buffer
913 *
914 * Returns: 0 on success or error code
915 */
916
917static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf)
918{
Al Virofc640052016-04-10 01:33:30 -0400919 struct super_block *sb = dentry->d_sb;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100920 struct gfs2_sbd *sdp = sb->s_fs_info;
921 struct gfs2_statfs_change_host sc;
922 int error;
923
Steven Whitehouse8339ee52011-08-31 16:38:29 +0100924 error = gfs2_rindex_update(sdp);
925 if (error)
926 return error;
927
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100928 if (gfs2_tune_get(sdp, gt_statfs_slow))
929 error = gfs2_statfs_slow(sdp, &sc);
930 else
931 error = gfs2_statfs_i(sdp, &sc);
932
933 if (error)
934 return error;
935
936 buf->f_type = GFS2_MAGIC;
937 buf->f_bsize = sdp->sd_sb.sb_bsize;
938 buf->f_blocks = sc.sc_total;
939 buf->f_bfree = sc.sc_free;
940 buf->f_bavail = sc.sc_free;
941 buf->f_files = sc.sc_dinodes + sc.sc_free;
942 buf->f_ffree = sc.sc_free;
943 buf->f_namelen = GFS2_FNAMESIZE;
944
945 return 0;
946}
947
948/**
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100949 * gfs2_drop_inode - Drop an inode (test for remote unlink)
950 * @inode: The inode to drop
951 *
Andreas Gruenbacher61b91cf2017-08-01 09:54:33 -0500952 * If we've received a callback on an iopen lock then it's because a
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100953 * remote node tried to deallocate the inode but failed due to this node
954 * still having the inode open. Here we mark the link count zero
955 * since we know that it must have reached zero if the GLF_DEMOTE flag
956 * is set on the iopen glock. If we didn't do a disk read since the
957 * remote node removed the final link then we might otherwise miss
958 * this event. This check ensures that this node will deallocate the
959 * inode's blocks, or alternatively pass the baton on to another
960 * node for later deallocation.
961 */
962
Al Viro45321ac2010-06-07 13:43:19 -0400963static int gfs2_drop_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100964{
965 struct gfs2_inode *ip = GFS2_I(inode);
966
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500967 if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) &&
968 inode->i_nlink &&
969 gfs2_holder_initialized(&ip->i_iopen_gh)) {
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100970 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -0500971 if (test_bit(GLF_DEMOTE, &gl->gl_flags))
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100972 clear_nlink(inode);
973 }
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -0500974
975 /*
976 * When under memory pressure when an inode's link count has dropped to
977 * zero, defer deleting the inode to the delete workqueue. This avoids
978 * calling into DLM under memory pressure, which can deadlock.
979 */
980 if (!inode->i_nlink &&
981 unlikely(current->flags & PF_MEMALLOC) &&
982 gfs2_holder_initialized(&ip->i_iopen_gh)) {
983 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
984
985 gfs2_glock_hold(gl);
Andreas Gruenbachera0e3cc62020-01-16 20:12:26 +0100986 if (!gfs2_queue_delete_work(gl, 0))
Andreas Gruenbacher6a1c8f62017-08-01 11:49:42 -0500987 gfs2_glock_queue_put(gl);
988 return false;
989 }
990
Al Viro45321ac2010-06-07 13:43:19 -0400991 return generic_drop_inode(inode);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +0100992}
993
994static int is_ancestor(const struct dentry *d1, const struct dentry *d2)
995{
996 do {
997 if (d1 == d2)
998 return 1;
999 d1 = d1->d_parent;
1000 } while (!IS_ROOT(d1));
1001 return 0;
1002}
1003
1004/**
1005 * gfs2_show_options - Show mount options for /proc/mounts
1006 * @s: seq_file structure
Al Viro34c80b12011-12-08 21:32:45 -05001007 * @root: root of this (sub)tree
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001008 *
1009 * Returns: 0 on success or error code
1010 */
1011
Al Viro34c80b12011-12-08 21:32:45 -05001012static int gfs2_show_options(struct seq_file *s, struct dentry *root)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001013{
Al Viro34c80b12011-12-08 21:32:45 -05001014 struct gfs2_sbd *sdp = root->d_sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001015 struct gfs2_args *args = &sdp->sd_args;
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001016 int val;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001017
Al Viro34c80b12011-12-08 21:32:45 -05001018 if (is_ancestor(root, sdp->sd_master_dir))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001019 seq_puts(s, ",meta");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001020 if (args->ar_lockproto[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001021 seq_show_option(s, "lockproto", args->ar_lockproto);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001022 if (args->ar_locktable[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001023 seq_show_option(s, "locktable", args->ar_locktable);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001024 if (args->ar_hostdata[0])
Kees Cooka068acf2015-09-04 15:44:57 -07001025 seq_show_option(s, "hostdata", args->ar_hostdata);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001026 if (args->ar_spectator)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001027 seq_puts(s, ",spectator");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001028 if (args->ar_localflocks)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001029 seq_puts(s, ",localflocks");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001030 if (args->ar_debug)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001031 seq_puts(s, ",debug");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001032 if (args->ar_posix_acl)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001033 seq_puts(s, ",acl");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001034 if (args->ar_quota != GFS2_QUOTA_DEFAULT) {
1035 char *state;
1036 switch (args->ar_quota) {
1037 case GFS2_QUOTA_OFF:
1038 state = "off";
1039 break;
1040 case GFS2_QUOTA_ACCOUNT:
1041 state = "account";
1042 break;
1043 case GFS2_QUOTA_ON:
1044 state = "on";
1045 break;
1046 default:
1047 state = "unknown";
1048 break;
1049 }
1050 seq_printf(s, ",quota=%s", state);
1051 }
1052 if (args->ar_suiddir)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001053 seq_puts(s, ",suiddir");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001054 if (args->ar_data != GFS2_DATA_DEFAULT) {
1055 char *state;
1056 switch (args->ar_data) {
1057 case GFS2_DATA_WRITEBACK:
1058 state = "writeback";
1059 break;
1060 case GFS2_DATA_ORDERED:
1061 state = "ordered";
1062 break;
1063 default:
1064 state = "unknown";
1065 break;
1066 }
1067 seq_printf(s, ",data=%s", state);
1068 }
1069 if (args->ar_discard)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001070 seq_puts(s, ",discard");
Benjamin Marzinski5e687ea2010-05-04 14:29:16 -05001071 val = sdp->sd_tune.gt_logd_secs;
1072 if (val != 30)
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001073 seq_printf(s, ",commit=%d", val);
1074 val = sdp->sd_tune.gt_statfs_quantum;
1075 if (val != 30)
1076 seq_printf(s, ",statfs_quantum=%d", val);
Steven Whitehouse2b9731e2012-08-20 17:07:49 +01001077 else if (sdp->sd_tune.gt_statfs_slow)
1078 seq_puts(s, ",statfs_quantum=0");
Benjamin Marzinski3d3c10f2009-10-20 02:39:44 -05001079 val = sdp->sd_tune.gt_quota_quantum;
1080 if (val != 60)
1081 seq_printf(s, ",quota_quantum=%d", val);
1082 if (args->ar_statfs_percent)
1083 seq_printf(s, ",statfs_percent=%d", args->ar_statfs_percent);
Bob Petersond34843d2009-08-24 10:44:18 +01001084 if (args->ar_errors != GFS2_ERRORS_DEFAULT) {
1085 const char *state;
1086
1087 switch (args->ar_errors) {
1088 case GFS2_ERRORS_WITHDRAW:
1089 state = "withdraw";
1090 break;
1091 case GFS2_ERRORS_PANIC:
1092 state = "panic";
1093 break;
1094 default:
1095 state = "unknown";
1096 break;
1097 }
1098 seq_printf(s, ",errors=%s", state);
1099 }
Steven Whitehousecdcfde62009-10-30 10:48:53 +00001100 if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001101 seq_puts(s, ",nobarrier");
Steven Whitehouse913a71d2010-05-06 11:03:29 +01001102 if (test_bit(SDF_DEMOTE, &sdp->sd_flags))
Fabian Frederickeaebded2014-07-02 22:08:46 +02001103 seq_puts(s, ",demote_interface_used");
Benjamin Marzinski90306c42012-05-29 23:01:09 -05001104 if (args->ar_rgrplvb)
Fabian Frederickeaebded2014-07-02 22:08:46 +02001105 seq_puts(s, ",rgrplvb");
Benjamin Marzinski471f3db2015-12-01 08:46:55 -06001106 if (args->ar_loccookie)
1107 seq_puts(s, ",loccookie");
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001108 return 0;
1109}
1110
Steven Whitehousef42ab082011-04-14 16:50:31 +01001111static void gfs2_final_release_pages(struct gfs2_inode *ip)
1112{
1113 struct inode *inode = &ip->i_inode;
1114 struct gfs2_glock *gl = ip->i_gl;
1115
1116 truncate_inode_pages(gfs2_glock2aspace(ip->i_gl), 0);
1117 truncate_inode_pages(&inode->i_data, 0);
1118
Bob Peterson638803d2019-06-06 07:33:38 -05001119 if (atomic_read(&gl->gl_revokes) == 0) {
Steven Whitehousef42ab082011-04-14 16:50:31 +01001120 clear_bit(GLF_LFLUSH, &gl->gl_flags);
1121 clear_bit(GLF_DIRTY, &gl->gl_flags);
1122 }
1123}
1124
1125static int gfs2_dinode_dealloc(struct gfs2_inode *ip)
1126{
1127 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001128 struct gfs2_rgrpd *rgd;
Bob Peterson564e12b2011-11-21 13:36:17 -05001129 struct gfs2_holder gh;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001130 int error;
1131
1132 if (gfs2_get_inode_blocks(&ip->i_inode) != 1) {
Steven Whitehouse94fb7632011-05-09 13:36:10 +01001133 gfs2_consist_inode(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001134 return -EIO;
1135 }
1136
Bob Peterson8e2e0042012-07-19 08:12:40 -04001137 error = gfs2_rindex_update(sdp);
1138 if (error)
1139 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001140
Eric W. Biedermanf4108a62013-01-31 17:49:26 -08001141 error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001142 if (error)
Bob Peterson5407e242012-05-18 09:28:23 -04001143 return error;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001144
Steven Whitehouse66fc0612012-02-08 12:58:32 +00001145 rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001146 if (!rgd) {
1147 gfs2_consist_inode(ip);
1148 error = -EIO;
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001149 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001150 }
1151
Bob Peterson564e12b2011-11-21 13:36:17 -05001152 error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001153 if (error)
Steven Whitehouse8339ee52011-08-31 16:38:29 +01001154 goto out_qs;
Steven Whitehousef42ab082011-04-14 16:50:31 +01001155
Steven Whitehouse4667a0e2011-04-18 14:18:09 +01001156 error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA,
1157 sdp->sd_jdesc->jd_blocks);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001158 if (error)
1159 goto out_rg_gunlock;
1160
1161 gfs2_free_di(rgd, ip);
1162
1163 gfs2_final_release_pages(ip);
1164
1165 gfs2_trans_end(sdp);
1166
1167out_rg_gunlock:
Bob Peterson564e12b2011-11-21 13:36:17 -05001168 gfs2_glock_dq_uninit(&gh);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001169out_qs:
1170 gfs2_quota_unhold(ip);
Steven Whitehousef42ab082011-04-14 16:50:31 +01001171 return error;
1172}
1173
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001174/**
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001175 * gfs2_glock_put_eventually
1176 * @gl: The glock to put
1177 *
1178 * When under memory pressure, trigger a deferred glock put to make sure we
1179 * won't call into DLM and deadlock. Otherwise, put the glock directly.
1180 */
1181
1182static void gfs2_glock_put_eventually(struct gfs2_glock *gl)
1183{
1184 if (current->flags & PF_MEMALLOC)
1185 gfs2_glock_queue_put(gl);
1186 else
1187 gfs2_glock_put(gl);
1188}
1189
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001190static bool gfs2_upgrade_iopen_glock(struct inode *inode)
1191{
1192 struct gfs2_inode *ip = GFS2_I(inode);
1193 struct gfs2_sbd *sdp = GFS2_SB(inode);
1194 struct gfs2_holder *gh = &ip->i_iopen_gh;
1195 long timeout = 5 * HZ;
1196 int error;
1197
1198 gh->gh_flags |= GL_NOCACHE;
1199 gfs2_glock_dq_wait(gh);
1200
1201 /*
1202 * If there are no other lock holders, we'll get the lock immediately.
1203 * Otherwise, the other nodes holding the lock will be notified about
1204 * our locking request. If they don't have the inode open, they'll
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001205 * evict the cached inode and release the lock. Otherwise, if they
1206 * poke the inode glock, we'll take this as an indication that they
1207 * still need the iopen glock and that they'll take care of deleting
1208 * the inode when they're done. As a last resort, if another node
1209 * keeps holding the iopen glock without showing any activity on the
1210 * inode glock, we'll eventually time out.
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001211 *
1212 * Note that we're passing the LM_FLAG_TRY_1CB flag to the first
1213 * locking request as an optimization to notify lock holders as soon as
1214 * possible. Without that flag, they'd be notified implicitly by the
1215 * second locking request.
1216 */
1217
1218 gfs2_holder_reinit(LM_ST_EXCLUSIVE, LM_FLAG_TRY_1CB | GL_NOCACHE, gh);
1219 error = gfs2_glock_nq(gh);
1220 if (error != GLR_TRYFAILED)
1221 return !error;
1222
1223 gfs2_holder_reinit(LM_ST_EXCLUSIVE, GL_ASYNC | GL_NOCACHE, gh);
1224 error = gfs2_glock_nq(gh);
1225 if (error)
1226 return false;
1227
1228 timeout = wait_event_interruptible_timeout(sdp->sd_async_glock_wait,
Andreas Gruenbacher9e8990d2020-01-17 10:53:23 +01001229 !test_bit(HIF_WAIT, &gh->gh_iflags) ||
1230 test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags),
Andreas Gruenbacher9e733302020-01-14 14:59:08 +01001231 timeout);
1232 if (!test_bit(HIF_HOLDER, &gh->gh_iflags)) {
1233 gfs2_glock_dq(gh);
1234 return false;
1235 }
1236 return true;
1237}
1238
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001239/**
Bob Peterson53dbc27e2020-09-11 10:30:26 -05001240 * evict_should_delete - determine whether the inode is eligible for deletion
1241 * @inode: The inode to evict
1242 *
1243 * This function determines whether the evicted inode is eligible to be deleted
1244 * and locks the inode glock.
1245 *
1246 * Returns: the fate of the dinode
1247 */
1248static enum dinode_demise evict_should_delete(struct inode *inode,
1249 struct gfs2_holder *gh)
1250{
1251 struct gfs2_inode *ip = GFS2_I(inode);
1252 struct super_block *sb = inode->i_sb;
1253 struct gfs2_sbd *sdp = sb->s_fs_info;
1254 int ret;
1255
1256 if (test_bit(GIF_ALLOC_FAILED, &ip->i_flags)) {
1257 BUG_ON(!gfs2_glock_is_locked_by_me(ip->i_gl));
1258 goto should_delete;
1259 }
1260
1261 if (test_bit(GIF_DEFERRED_DELETE, &ip->i_flags))
1262 return SHOULD_DEFER_EVICTION;
1263
1264 /* Deletes should never happen under memory pressure anymore. */
1265 if (WARN_ON_ONCE(current->flags & PF_MEMALLOC))
1266 return SHOULD_DEFER_EVICTION;
1267
1268 /* Must not read inode block until block type has been verified */
1269 ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_SKIP, gh);
1270 if (unlikely(ret)) {
1271 glock_clear_object(ip->i_iopen_gh.gh_gl, ip);
1272 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1273 gfs2_glock_dq_uninit(&ip->i_iopen_gh);
1274 return SHOULD_DEFER_EVICTION;
1275 }
1276
1277 if (gfs2_inode_already_deleted(ip->i_gl, ip->i_no_formal_ino))
1278 return SHOULD_NOT_DELETE_DINODE;
1279 ret = gfs2_check_blk_type(sdp, ip->i_no_addr, GFS2_BLKST_UNLINKED);
1280 if (ret)
1281 return SHOULD_NOT_DELETE_DINODE;
1282
1283 if (test_bit(GIF_INVALID, &ip->i_flags)) {
1284 ret = gfs2_inode_refresh(ip);
1285 if (ret)
1286 return SHOULD_NOT_DELETE_DINODE;
1287 }
1288
1289 /*
1290 * The inode may have been recreated in the meantime.
1291 */
1292 if (inode->i_nlink)
1293 return SHOULD_NOT_DELETE_DINODE;
1294
1295should_delete:
1296 if (gfs2_holder_initialized(&ip->i_iopen_gh) &&
1297 test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1298 if (!gfs2_upgrade_iopen_glock(inode)) {
1299 gfs2_holder_uninit(&ip->i_iopen_gh);
1300 return SHOULD_NOT_DELETE_DINODE;
1301 }
1302 }
1303 return SHOULD_DELETE_DINODE;
1304}
1305
1306/**
Bob Peterson6e7e9a52020-09-11 09:29:25 -05001307 * evict_unlinked_inode - delete the pieces of an unlinked evicted inode
1308 * @inode: The inode to evict
1309 */
1310static int evict_unlinked_inode(struct inode *inode)
1311{
1312 struct gfs2_inode *ip = GFS2_I(inode);
1313 int ret;
1314
1315 if (S_ISDIR(inode->i_mode) &&
1316 (ip->i_diskflags & GFS2_DIF_EXHASH)) {
1317 ret = gfs2_dir_exhash_dealloc(ip);
1318 if (ret)
1319 goto out;
1320 }
1321
1322 if (ip->i_eattr) {
1323 ret = gfs2_ea_dealloc(ip);
1324 if (ret)
1325 goto out;
1326 }
1327
1328 if (!gfs2_is_stuffed(ip)) {
1329 ret = gfs2_file_dealloc(ip);
1330 if (ret)
1331 goto out;
1332 }
1333
1334 /* We're about to clear the bitmap for the dinode, but as soon as we
1335 do, gfs2_create_inode can create another inode at the same block
1336 location and try to set gl_object again. We clear gl_object here so
1337 that subsequent inode creates don't see an old gl_object. */
1338 glock_clear_object(ip->i_gl, ip);
1339 ret = gfs2_dinode_dealloc(ip);
1340 gfs2_inode_remember_delete(ip->i_gl, ip->i_no_formal_ino);
1341out:
1342 return ret;
1343}
1344
Bob Petersond90be6a2020-09-11 14:53:52 -05001345/*
1346 * evict_linked_inode - evict an inode whose dinode has not been unlinked
1347 * @inode: The inode to evict
1348 */
1349static int evict_linked_inode(struct inode *inode)
1350{
1351 struct super_block *sb = inode->i_sb;
1352 struct gfs2_sbd *sdp = sb->s_fs_info;
1353 struct gfs2_inode *ip = GFS2_I(inode);
1354 struct address_space *metamapping;
1355 int ret;
1356
1357 gfs2_log_flush(sdp, ip->i_gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
1358 GFS2_LFC_EVICT_INODE);
1359 metamapping = gfs2_glock2aspace(ip->i_gl);
1360 if (test_bit(GLF_DIRTY, &ip->i_gl->gl_flags)) {
1361 filemap_fdatawrite(metamapping);
1362 filemap_fdatawait(metamapping);
1363 }
1364 write_inode_now(inode, 1);
1365 gfs2_ail_flush(ip->i_gl, 0);
1366
1367 ret = gfs2_trans_begin(sdp, 0, sdp->sd_jdesc->jd_blocks);
1368 if (ret)
1369 return ret;
1370
1371 /* Needs to be done before glock release & also in a transaction */
1372 truncate_inode_pages(&inode->i_data, 0);
1373 truncate_inode_pages(metamapping, 0);
1374 gfs2_trans_end(sdp);
1375 return 0;
1376}
1377
Bob Peterson6e7e9a52020-09-11 09:29:25 -05001378/**
Steven Whitehouse380f7c62011-07-14 08:59:44 +01001379 * gfs2_evict_inode - Remove an inode from cache
1380 * @inode: The inode to evict
1381 *
1382 * There are three cases to consider:
1383 * 1. i_nlink == 0, we are final opener (and must deallocate)
1384 * 2. i_nlink == 0, we are not the final opener (and cannot deallocate)
1385 * 3. i_nlink > 0
1386 *
1387 * If the fs is read only, then we have to treat all cases as per #3
1388 * since we are unable to do any deallocation. The inode will be
1389 * deallocated by the next read/write node to attempt an allocation
1390 * in the same resource group
1391 *
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001392 * We have to (at the moment) hold the inodes main lock to cover
1393 * the gap between unlocking the shared lock on the iopen lock and
1394 * taking the exclusive lock. I'd rather do a shared -> exclusive
1395 * conversion on the iopen lock, but we can change that later. This
1396 * is safe, just less efficient.
1397 */
1398
Al Virod5c15152010-06-07 11:05:19 -04001399static void gfs2_evict_inode(struct inode *inode)
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001400{
Steven Whitehouse001e8e82011-03-30 14:17:51 +01001401 struct super_block *sb = inode->i_sb;
1402 struct gfs2_sbd *sdp = sb->s_fs_info;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001403 struct gfs2_inode *ip = GFS2_I(inode);
1404 struct gfs2_holder gh;
Bob Peterson23d828f2020-09-11 10:56:31 -05001405 int ret;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001406
Abhi Das05978802014-03-31 10:33:17 -05001407 if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) {
1408 clear_inode(inode);
1409 return;
1410 }
1411
David Howellsbc98a422017-07-17 08:45:34 +01001412 if (inode->i_nlink || sb_rdonly(sb))
Al Virod5c15152010-06-07 11:05:19 -04001413 goto out;
1414
Bob Peterson53dbc27e2020-09-11 10:30:26 -05001415 gfs2_holder_mark_uninitialized(&gh);
1416 ret = evict_should_delete(inode, &gh);
1417 if (ret == SHOULD_DEFER_EVICTION)
Andreas Gruenbacher8c7b9262020-01-13 22:16:17 +01001418 goto out;
Bob Peterson0a0d9f52020-09-16 08:50:44 -05001419 if (ret == SHOULD_DELETE_DINODE)
1420 ret = evict_unlinked_inode(inode);
1421 else
1422 ret = evict_linked_inode(inode);
Steven Whitehouseacf7e242009-09-08 18:00:30 +01001423
Bob Petersona097dc7e2015-07-16 08:28:04 -05001424 if (gfs2_rs_active(&ip->i_res))
1425 gfs2_rs_deltree(&ip->i_res);
Bob Peterson8e2e0042012-07-19 08:12:40 -04001426
Bob Peterson240c6232017-07-18 12:36:01 -05001427 if (gfs2_holder_initialized(&gh)) {
1428 glock_clear_object(ip->i_gl, ip);
Andreas Gruenbachere0b62e22017-06-30 08:16:46 -05001429 gfs2_glock_dq_uninit(&gh);
Bob Peterson240c6232017-07-18 12:36:01 -05001430 }
Bob Peterson23d828f2020-09-11 10:56:31 -05001431 if (ret && ret != GLR_TRYFAILED && ret != -EROFS)
1432 fs_warn(sdp, "gfs2_evict_inode: %d\n", ret);
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001433out:
Johannes Weiner91b0abe2014-04-03 14:47:49 -07001434 truncate_inode_pages_final(&inode->i_data);
Bob Peterson2fba46a2020-02-27 12:47:53 -06001435 if (ip->i_qadata)
1436 gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0);
Andreas Gruenbacher15955482020-03-06 10:32:35 -06001437 gfs2_rs_delete(ip, NULL);
Steven Whitehouse45138992013-01-28 09:30:07 +00001438 gfs2_ordered_del_inode(ip);
Jan Karadbd57682012-05-03 14:48:02 +02001439 clear_inode(inode);
Steven Whitehouse17d539f2011-06-15 10:29:37 +01001440 gfs2_dir_hash_inval(ip);
Andreas Gruenbacher6df9f9a2016-06-17 07:31:27 -05001441 if (gfs2_holder_initialized(&ip->i_iopen_gh)) {
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001442 struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl;
1443
1444 glock_clear_object(gl, ip);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001445 if (test_bit(HIF_HOLDER, &ip->i_iopen_gh.gh_iflags)) {
1446 ip->i_iopen_gh.gh_flags |= GL_NOCACHE;
1447 gfs2_glock_dq(&ip->i_iopen_gh);
1448 }
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001449 gfs2_glock_hold(gl);
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001450 gfs2_holder_uninit(&ip->i_iopen_gh);
Andreas Gruenbacher71c1b2132017-08-01 11:45:23 -05001451 gfs2_glock_put_eventually(gl);
Al Virod5c15152010-06-07 11:05:19 -04001452 }
Bob Peterson664ccea2021-10-28 11:53:10 -05001453 if (ip->i_gl) {
1454 glock_clear_object(ip->i_gl, ip);
1455 wait_on_bit_io(&ip->i_flags, GIF_GLOP_PENDING, TASK_UNINTERRUPTIBLE);
1456 gfs2_glock_add_to_lru(ip->i_gl);
1457 gfs2_glock_put_eventually(ip->i_gl);
1458 ip->i_gl = NULL;
1459 }
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001460}
1461
1462static struct inode *gfs2_alloc_inode(struct super_block *sb)
1463{
1464 struct gfs2_inode *ip;
1465
1466 ip = kmem_cache_alloc(gfs2_inode_cachep, GFP_KERNEL);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001467 if (!ip)
1468 return NULL;
1469 ip->i_flags = 0;
1470 ip->i_gl = NULL;
Andreas Gruenbacher40e7e862020-01-24 14:14:46 +01001471 gfs2_holder_mark_uninitialized(&ip->i_iopen_gh);
Andreas Gruenbacherd4031252019-07-24 13:05:38 +02001472 memset(&ip->i_res, 0, sizeof(ip->i_res));
1473 RB_CLEAR_NODE(&ip->i_res.rs_node);
1474 ip->i_rahead = 0;
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001475 return &ip->i_inode;
1476}
1477
Al Viro784494e2019-04-15 19:45:26 -04001478static void gfs2_free_inode(struct inode *inode)
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +11001479{
Al Viro784494e2019-04-15 19:45:26 -04001480 kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode));
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001481}
1482
Abhi Das97fd7342020-10-20 15:58:04 -05001483extern void free_local_statfs_inodes(struct gfs2_sbd *sdp)
1484{
1485 struct local_statfs_inode *lsi, *safe;
1486
1487 /* Run through the statfs inodes list to iput and free memory */
1488 list_for_each_entry_safe(lsi, safe, &sdp->sd_sc_inodes_list, si_list) {
1489 if (lsi->si_jid == sdp->sd_jdesc->jd_jid)
1490 sdp->sd_sc_inode = NULL; /* belongs to this node */
1491 if (lsi->si_sc_inode)
1492 iput(lsi->si_sc_inode);
1493 list_del(&lsi->si_list);
1494 kfree(lsi);
1495 }
1496}
1497
1498extern struct inode *find_local_statfs_inode(struct gfs2_sbd *sdp,
1499 unsigned int index)
1500{
1501 struct local_statfs_inode *lsi;
1502
1503 /* Return the local (per node) statfs inode in the
1504 * sdp->sd_sc_inodes_list corresponding to the 'index'. */
1505 list_for_each_entry(lsi, &sdp->sd_sc_inodes_list, si_list) {
1506 if (lsi->si_jid == index)
1507 return lsi->si_sc_inode;
1508 }
1509 return NULL;
1510}
1511
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001512const struct super_operations gfs2_super_ops = {
1513 .alloc_inode = gfs2_alloc_inode,
Al Viro784494e2019-04-15 19:45:26 -04001514 .free_inode = gfs2_free_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001515 .write_inode = gfs2_write_inode,
Steven Whitehouseab9bbda2011-08-15 14:20:36 +01001516 .dirty_inode = gfs2_dirty_inode,
Al Virod5c15152010-06-07 11:05:19 -04001517 .evict_inode = gfs2_evict_inode,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001518 .put_super = gfs2_put_super,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001519 .sync_fs = gfs2_sync_fs,
Benjamin Marzinski2e60d762014-11-13 20:42:04 -06001520 .freeze_super = gfs2_freeze,
1521 .thaw_super = gfs2_unfreeze,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001522 .statfs = gfs2_statfs,
Steven Whitehouse9e6e0a12009-05-22 10:36:01 +01001523 .drop_inode = gfs2_drop_inode,
1524 .show_options = gfs2_show_options,
1525};
1526