Thomas Gleixner | 7336d0e | 2019-05-31 01:09:56 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Bob Peterson | da6dd40 | 2007-12-11 18:49:21 -0600 | [diff] [blame] | 4 | * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Joe Perches | d77d1b5 | 2014-03-06 12:10:45 -0800 | [diff] [blame] | 7 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 8 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 9 | #include <linux/bio.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 10 | #include <linux/sched/signal.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 15 | #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 Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 20 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 21 | #include <linux/crc32.h> |
| 22 | #include <linux/time.h> |
Steven Whitehouse | e402746 | 2010-01-25 11:20:19 +0000 | [diff] [blame] | 23 | #include <linux/wait.h> |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 24 | #include <linux/writeback.h> |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 25 | #include <linux/backing-dev.h> |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 26 | #include <linux/kernel.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 27 | |
| 28 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 29 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | #include "bmap.h" |
| 31 | #include "dir.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 32 | #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 Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 42 | #include "util.h" |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 43 | #include "sys.h" |
Steven Whitehouse | 307cf6e | 2009-08-26 18:51:04 +0100 | [diff] [blame] | 44 | #include "xattr.h" |
Abhi Das | f4686c2 | 2019-05-02 14:17:40 -0500 | [diff] [blame] | 45 | #include "lops.h" |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 46 | |
Bob Peterson | 53dbc27e | 2020-09-11 10:30:26 -0500 | [diff] [blame] | 47 | enum dinode_demise { |
| 48 | SHOULD_DELETE_DINODE, |
| 49 | SHOULD_NOT_DELETE_DINODE, |
| 50 | SHOULD_DEFER_EVICTION, |
| 51 | }; |
| 52 | |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 53 | /** |
| 54 | * gfs2_jindex_free - Clear all the journal index information |
| 55 | * @sdp: The GFS2 superblock |
| 56 | * |
| 57 | */ |
| 58 | |
| 59 | void gfs2_jindex_free(struct gfs2_sbd *sdp) |
| 60 | { |
Steven Whitehouse | b50f227 | 2014-03-03 13:35:57 +0000 | [diff] [blame] | 61 | struct list_head list; |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 62 | struct gfs2_jdesc *jd; |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 63 | |
| 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 Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 70 | sdp->sd_jdesc = NULL; |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 71 | while (!list_empty(&list)) { |
Andreas Gruenbacher | 969183b | 2020-02-03 19:22:45 +0100 | [diff] [blame] | 72 | jd = list_first_entry(&list, struct gfs2_jdesc, jd_list); |
Steven Whitehouse | b50f227 | 2014-03-03 13:35:57 +0000 | [diff] [blame] | 73 | gfs2_free_journal_extents(jd); |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 74 | list_del(&jd->jd_list); |
| 75 | iput(jd->jd_inode); |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 76 | jd->jd_inode = NULL; |
Steven Whitehouse | fefc03b | 2008-12-19 15:32:06 +0000 | [diff] [blame] | 77 | kfree(jd); |
| 78 | } |
| 79 | } |
| 80 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 81 | static 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 | |
| 99 | struct 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 110 | int gfs2_jdesc_check(struct gfs2_jdesc *jd) |
| 111 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 112 | struct gfs2_inode *ip = GFS2_I(jd->jd_inode); |
| 113 | struct gfs2_sbd *sdp = GFS2_SB(jd->jd_inode); |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 114 | u64 size = i_size_read(jd->jd_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 115 | |
Fabian Frederick | 47a9a52 | 2016-08-02 12:05:27 -0500 | [diff] [blame] | 116 | if (gfs2_check_internal_file_size(jd->jd_inode, 8 << 20, BIT(30))) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 117 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 118 | |
Steven Whitehouse | a2e0f79 | 2010-08-11 09:53:11 +0100 | [diff] [blame] | 119 | jd->jd_blocks = size >> sdp->sd_sb.sb_bsize_shift; |
| 120 | |
| 121 | if (gfs2_write_alloc_required(ip, 0, size)) { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 122 | gfs2_consist_inode(ip); |
Bob Peterson | 461cb41 | 2010-06-24 19:21:20 -0400 | [diff] [blame] | 123 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Bob Peterson | 461cb41 | 2010-06-24 19:21:20 -0400 | [diff] [blame] | 126 | return 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 127 | } |
| 128 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 129 | /** |
| 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 | |
| 136 | int gfs2_make_fs_rw(struct gfs2_sbd *sdp) |
| 137 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 138 | struct gfs2_inode *ip = GFS2_I(sdp->sd_jdesc->jd_inode); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 139 | struct gfs2_glock *j_gl = ip->i_gl; |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 140 | struct gfs2_log_header_host head; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 141 | int error; |
| 142 | |
Steven Whitehouse | 1a14d3a | 2006-11-20 10:37:45 -0500 | [diff] [blame] | 143 | j_gl->gl_ops->go_inval(j_gl, DIO_METADATA); |
Bob Peterson | b4bbb77 | 2021-05-14 07:42:33 -0500 | [diff] [blame] | 144 | if (gfs2_withdrawn(sdp)) |
| 145 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 146 | |
Abhi Das | f4686c2 | 2019-05-02 14:17:40 -0500 | [diff] [blame] | 147 | error = gfs2_find_jhead(sdp->sd_jdesc, &head, false); |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 148 | if (error || gfs2_withdrawn(sdp)) |
Bob Peterson | b4bbb77 | 2021-05-14 07:42:33 -0500 | [diff] [blame] | 149 | return error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | |
| 151 | if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { |
| 152 | gfs2_consist(sdp); |
Bob Peterson | b4bbb77 | 2021-05-14 07:42:33 -0500 | [diff] [blame] | 153 | return -EIO; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 154 | } |
| 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 160 | error = gfs2_quota_init(sdp); |
Bob Peterson | b4bbb77 | 2021-05-14 07:42:33 -0500 | [diff] [blame] | 161 | if (!error && !gfs2_withdrawn(sdp)) |
| 162 | set_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 163 | return error; |
| 164 | } |
| 165 | |
Benjamin Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 166 | void gfs2_statfs_change_in(struct gfs2_statfs_change_host *sc, const void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 167 | { |
| 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 Das | 7309269 | 2020-10-20 15:58:03 -0500 | [diff] [blame] | 175 | void gfs2_statfs_change_out(const struct gfs2_statfs_change_host *sc, void *buf) |
Steven Whitehouse | bb8d8a6 | 2007-06-01 14:11:58 +0100 | [diff] [blame] | 176 | { |
| 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 184 | int gfs2_statfs_init(struct gfs2_sbd *sdp) |
| 185 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 186 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 187 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 188 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 189 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 190 | 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 Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 223 | out_m_bh: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 224 | brelse(m_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 225 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 226 | gfs2_glock_dq_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 227 | return 0; |
| 228 | } |
| 229 | |
Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 230 | void gfs2_statfs_change(struct gfs2_sbd *sdp, s64 total, s64 free, |
| 231 | s64 dinodes) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 232 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 233 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 234 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 235 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | struct buffer_head *l_bh; |
Benjamin Marzinski | c14f573 | 2009-10-26 13:29:47 -0500 | [diff] [blame] | 237 | s64 x, y; |
| 238 | int need_sync = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 239 | int error; |
| 240 | |
| 241 | error = gfs2_meta_inode_buffer(l_ip, &l_bh); |
| 242 | if (error) |
| 243 | return; |
| 244 | |
Steven Whitehouse | 350a9b0 | 2012-12-14 12:36:02 +0000 | [diff] [blame] | 245 | gfs2_trans_add_meta(l_ip->i_gl, l_bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 246 | |
| 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 Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 251 | gfs2_statfs_change_out(l_sc, l_bh->b_data + sizeof(struct gfs2_dinode)); |
Benjamin Marzinski | c14f573 | 2009-10-26 13:29:47 -0500 | [diff] [blame] | 252 | 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 Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 258 | spin_unlock(&sdp->sd_statfs_spin); |
| 259 | |
| 260 | brelse(l_bh); |
Benjamin Marzinski | c14f573 | 2009-10-26 13:29:47 -0500 | [diff] [blame] | 261 | if (need_sync) |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 262 | gfs2_wake_up_statfs(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Benjamin Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 265 | void 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 Whitehouse | 350a9b0 | 2012-12-14 12:36:02 +0000 | [diff] [blame] | 273 | gfs2_trans_add_meta(l_ip->i_gl, l_bh); |
Bob Peterson | 901c6c6 | 2015-03-11 09:52:31 -0500 | [diff] [blame] | 274 | gfs2_trans_add_meta(m_ip->i_gl, m_bh); |
Benjamin Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 275 | |
| 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 Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 283 | gfs2_statfs_change_out(m_sc, m_bh->b_data + sizeof(struct gfs2_dinode)); |
Bob Peterson | 901c6c6 | 2015-03-11 09:52:31 -0500 | [diff] [blame] | 284 | spin_unlock(&sdp->sd_statfs_spin); |
Benjamin Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 285 | } |
| 286 | |
Steven Whitehouse | 8c42d63 | 2009-09-11 14:36:44 +0100 | [diff] [blame] | 287 | int gfs2_statfs_sync(struct super_block *sb, int type) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 288 | { |
Steven Whitehouse | 8c42d63 | 2009-09-11 14:36:44 +0100 | [diff] [blame] | 289 | struct gfs2_sbd *sdp = sb->s_fs_info; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 290 | struct gfs2_inode *m_ip = GFS2_I(sdp->sd_statfs_inode); |
| 291 | struct gfs2_inode *l_ip = GFS2_I(sdp->sd_sc_inode); |
Al Viro | bd209cc | 2006-10-13 23:43:19 -0400 | [diff] [blame] | 292 | struct gfs2_statfs_change_host *m_sc = &sdp->sd_statfs_master; |
| 293 | struct gfs2_statfs_change_host *l_sc = &sdp->sd_statfs_local; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 294 | struct gfs2_holder gh; |
| 295 | struct buffer_head *m_bh, *l_bh; |
| 296 | int error; |
| 297 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 298 | sb_start_write(sb); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 299 | error = gfs2_glock_nq_init(m_ip->i_gl, LM_ST_EXCLUSIVE, GL_NOCACHE, |
| 300 | &gh); |
| 301 | if (error) |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 302 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 303 | |
| 304 | error = gfs2_meta_inode_buffer(m_ip, &m_bh); |
| 305 | if (error) |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 306 | goto out_unlock; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 307 | |
| 308 | spin_lock(&sdp->sd_statfs_spin); |
| 309 | gfs2_statfs_change_in(m_sc, m_bh->b_data + |
Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 310 | sizeof(struct gfs2_dinode)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 311 | 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 Marzinski | 1946f70 | 2009-06-25 15:09:51 -0500 | [diff] [blame] | 325 | update_statfs(sdp, m_bh, l_bh); |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 326 | sdp->sd_statfs_force_sync = 0; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 327 | |
| 328 | gfs2_trans_end(sdp); |
| 329 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 330 | out_bh2: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 331 | brelse(l_bh); |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 332 | out_bh: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 333 | brelse(m_bh); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 334 | out_unlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 335 | gfs2_glock_dq_uninit(&gh); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 336 | out: |
| 337 | sb_end_write(sb); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 338 | return error; |
| 339 | } |
| 340 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 341 | struct 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 Peterson | 52b1cdc | 2019-11-15 09:42:46 -0500 | [diff] [blame] | 356 | static int gfs2_lock_fs_check_clean(struct gfs2_sbd *sdp) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 357 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 358 | struct gfs2_inode *ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 359 | struct gfs2_jdesc *jd; |
| 360 | struct lfcc *lfcc; |
| 361 | LIST_HEAD(list); |
Al Viro | 5516762 | 2006-10-13 21:47:13 -0400 | [diff] [blame] | 362 | struct gfs2_log_header_host lh; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 363 | int error; |
| 364 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 365 | 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 Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 371 | ip = GFS2_I(jd->jd_inode); |
| 372 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, 0, &lfcc->gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 373 | if (error) { |
| 374 | kfree(lfcc); |
| 375 | goto out; |
| 376 | } |
| 377 | list_add(&lfcc->list, &list); |
| 378 | } |
| 379 | |
Benjamin Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 380 | error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_EXCLUSIVE, |
Bob Peterson | c860f8f | 2020-06-25 14:42:17 -0500 | [diff] [blame] | 381 | LM_FLAG_NOEXP, &sdp->sd_freeze_gh); |
Bob Peterson | 52b1cdc | 2019-11-15 09:42:46 -0500 | [diff] [blame] | 382 | if (error) |
| 383 | goto out; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 384 | |
| 385 | list_for_each_entry(jd, &sdp->sd_jindex_list, jd_list) { |
| 386 | error = gfs2_jdesc_check(jd); |
| 387 | if (error) |
| 388 | break; |
Abhi Das | f4686c2 | 2019-05-02 14:17:40 -0500 | [diff] [blame] | 389 | error = gfs2_find_jhead(jd, &lh, false); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 390 | 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 Peterson | 49787b1 | 2020-12-22 14:43:27 -0600 | [diff] [blame] | 399 | gfs2_freeze_unlock(&sdp->sd_freeze_gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 400 | |
Steven Whitehouse | a91ea69 | 2006-09-04 12:04:26 -0400 | [diff] [blame] | 401 | out: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 402 | while (!list_empty(&list)) { |
Andreas Gruenbacher | 969183b | 2020-02-03 19:22:45 +0100 | [diff] [blame] | 403 | lfcc = list_first_entry(&list, struct lfcc, list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 404 | list_del(&lfcc->list); |
| 405 | gfs2_glock_dq_uninit(&lfcc->gh); |
| 406 | kfree(lfcc); |
| 407 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 408 | return error; |
| 409 | } |
| 410 | |
Steven Whitehouse | 9eed04c | 2011-05-09 14:11:40 +0100 | [diff] [blame] | 411 | void 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. Biederman | d054642 | 2013-01-31 22:08:10 -0800 | [diff] [blame] | 421 | 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 Whitehouse | 9eed04c | 2011-05-09 14:11:40 +0100 | [diff] [blame] | 423 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 447 | |
| 448 | /** |
| 449 | * gfs2_write_inode - Make sure the inode is stable on the disk |
| 450 | * @inode: The inode |
Steven Whitehouse | 1027efa | 2011-03-30 16:13:25 +0100 | [diff] [blame] | 451 | * @wbc: The writeback control structure |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 452 | * |
| 453 | * Returns: errno |
| 454 | */ |
| 455 | |
Christoph Hellwig | a9185b4 | 2010-03-05 09:21:37 +0100 | [diff] [blame] | 456 | static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 457 | { |
| 458 | struct gfs2_inode *ip = GFS2_I(inode); |
| 459 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 1027efa | 2011-03-30 16:13:25 +0100 | [diff] [blame] | 460 | struct address_space *metamapping = gfs2_glock2aspace(ip->i_gl); |
Christoph Hellwig | de1414a | 2015-01-14 10:42:36 +0100 | [diff] [blame] | 461 | struct backing_dev_info *bdi = inode_to_bdi(metamapping->host); |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 462 | int ret = 0; |
Bob Peterson | adbc3dd | 2017-10-11 16:22:07 +0200 | [diff] [blame] | 463 | bool flush_all = (wbc->sync_mode == WB_SYNC_ALL || gfs2_is_jdata(ip)); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 464 | |
Bob Peterson | adbc3dd | 2017-10-11 16:22:07 +0200 | [diff] [blame] | 465 | if (flush_all) |
Bob Peterson | c1696fb | 2018-01-17 00:01:33 +0100 | [diff] [blame] | 466 | gfs2_log_flush(GFS2_SB(inode), ip->i_gl, |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 467 | GFS2_LOG_HEAD_FLUSH_NORMAL | |
| 468 | GFS2_LFC_WRITE_INODE); |
Tejun Heo | a88a341 | 2015-05-22 17:13:28 -0400 | [diff] [blame] | 469 | if (bdi->wb.dirty_exceeded) |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 470 | gfs2_ail1_flush(sdp, wbc); |
Steven Whitehouse | 1d4ec64 | 2011-08-02 13:13:20 +0100 | [diff] [blame] | 471 | else |
| 472 | filemap_fdatawrite(metamapping); |
Bob Peterson | adbc3dd | 2017-10-11 16:22:07 +0200 | [diff] [blame] | 473 | if (flush_all) |
Steven Whitehouse | 1027efa | 2011-03-30 16:13:25 +0100 | [diff] [blame] | 474 | ret = filemap_fdatawait(metamapping); |
| 475 | if (ret) |
| 476 | mark_inode_dirty_sync(inode); |
Abhi Das | 957a7ac | 2018-01-30 10:00:09 -0700 | [diff] [blame] | 477 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 483 | return ret; |
| 484 | } |
| 485 | |
| 486 | /** |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 487 | * 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 | |
| 499 | static 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 Hellwig | 0e11f64 | 2018-02-21 07:54:49 -0800 | [diff] [blame] | 509 | if (!(flags & I_DIRTY_INODE)) |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 510 | return; |
Bob Peterson | eb43e66 | 2019-11-14 09:52:15 -0500 | [diff] [blame] | 511 | if (unlikely(gfs2_withdrawn(sdp))) |
Bob Peterson | 0d1c7ae | 2017-03-03 12:37:14 -0500 | [diff] [blame] | 512 | return; |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 513 | 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 Peterson | e28c02b | 2020-07-30 12:31:38 -0500 | [diff] [blame] | 517 | gfs2_dump_glock(NULL, ip->i_gl, true); |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 518 | return; |
| 519 | } |
| 520 | need_unlock = 1; |
Benjamin Marzinski | 3d16268 | 2012-11-06 00:49:28 -0600 | [diff] [blame] | 521 | } else if (WARN_ON_ONCE(ip->i_gl->gl_state != LM_ST_EXCLUSIVE)) |
| 522 | return; |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 523 | |
| 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 Whitehouse | 350a9b0 | 2012-12-14 12:36:02 +0000 | [diff] [blame] | 535 | gfs2_trans_add_meta(ip->i_gl, bh); |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 536 | gfs2_dinode_out(ip, bh->b_data); |
| 537 | brelse(bh); |
| 538 | } |
| 539 | |
| 540 | if (need_endtrans) |
| 541 | gfs2_trans_end(sdp); |
| 542 | out: |
| 543 | if (need_unlock) |
| 544 | gfs2_glock_dq_uninit(&gh); |
| 545 | } |
| 546 | |
| 547 | /** |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 548 | * 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 Price | 1f52aa0 | 2019-03-27 14:46:00 +0000 | [diff] [blame] | 554 | int gfs2_make_fs_ro(struct gfs2_sbd *sdp) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 555 | { |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 556 | int error = 0; |
| 557 | int log_write_allowed = test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 558 | |
Andreas Gruenbacher | a0e3cc6 | 2020-01-16 20:12:26 +0100 | [diff] [blame] | 559 | gfs2_flush_delete_work(sdp); |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 560 | 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 Peterson | 5b3a9f3 | 2019-04-26 08:11:27 -0600 | [diff] [blame] | 563 | kthread_stop(sdp->sd_quotad_process); |
| 564 | sdp->sd_quotad_process = NULL; |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 565 | |
| 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 Peterson | 5b3a9f3 | 2019-04-26 08:11:27 -0600 | [diff] [blame] | 569 | kthread_stop(sdp->sd_logd_process); |
| 570 | sdp->sd_logd_process = NULL; |
Steven Whitehouse | 8ad151c | 2013-12-12 11:34:09 +0000 | [diff] [blame] | 571 | |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 572 | if (log_write_allowed) { |
| 573 | gfs2_quota_sync(sdp->sd_vfs, 0); |
| 574 | gfs2_statfs_sync(sdp->sd_vfs, 0); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 575 | |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 576 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 587 | gfs2_quota_cleanup(sdp); |
| 588 | |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 589 | if (!log_write_allowed) |
| 590 | sdp->sd_vfs->s_flags |= SB_RDONLY; |
| 591 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 592 | return error; |
| 593 | } |
| 594 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 595 | /** |
| 596 | * gfs2_put_super - Unmount the filesystem |
| 597 | * @sb: The VFS superblock |
| 598 | * |
| 599 | */ |
| 600 | |
| 601 | static 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 607 | /* No more recovery requests */ |
| 608 | set_bit(SDF_NORECOVERY, &sdp->sd_flags); |
| 609 | smp_mb(); |
| 610 | |
| 611 | /* Wait on outstanding recovery */ |
| 612 | restart: |
| 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, |
NeilBrown | 7431620 | 2014-07-07 15:16:04 +1000 | [diff] [blame] | 619 | TASK_UNINTERRUPTIBLE); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 620 | goto restart; |
| 621 | } |
| 622 | spin_unlock(&sdp->sd_jindex_spin); |
| 623 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 624 | if (!sb_rdonly(sb)) { |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 625 | error = gfs2_make_fs_ro(sdp); |
| 626 | if (error) |
| 627 | gfs2_io_error(sdp); |
| 628 | } |
Andreas Gruenbacher | 5a61ae1 | 2020-08-28 23:44:36 +0200 | [diff] [blame] | 629 | WARN_ON(gfs2_withdrawing(sdp)); |
| 630 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 631 | /* At this point, we're through modifying the disk */ |
| 632 | |
| 633 | /* Release stuff */ |
| 634 | |
| 635 | iput(sdp->sd_jindex); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 636 | 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 Marzinski | 2497255 | 2014-05-01 22:26:55 -0500 | [diff] [blame] | 641 | gfs2_glock_put(sdp->sd_freeze_gl); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 642 | |
| 643 | if (!sdp->sd_args.ar_spectator) { |
Bob Peterson | 601ef0d | 2020-01-28 20:23:45 +0100 | [diff] [blame] | 644 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 648 | gfs2_glock_dq_uninit(&sdp->sd_sc_gh); |
| 649 | gfs2_glock_dq_uninit(&sdp->sd_qc_gh); |
Abhi Das | 97fd734 | 2020-10-20 15:58:04 -0500 | [diff] [blame] | 650 | free_local_statfs_inodes(sdp); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 651 | 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 Peterson | a9dd945 | 2020-10-27 10:10:02 -0500 | [diff] [blame] | 659 | truncate_inode_pages_final(&sdp->sd_aspace); |
Bob Peterson | b2fb7da | 2017-07-28 07:22:55 -0500 | [diff] [blame] | 660 | gfs2_delete_debugfs_file(sdp); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 661 | /* 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 Iles | c2a04b0 | 2020-10-12 14:13:09 +0100 | [diff] [blame] | 666 | free_sbd(sdp); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 667 | } |
| 668 | |
| 669 | /** |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 670 | * gfs2_sync_fs - sync the filesystem |
| 671 | * @sb: the superblock |
| 672 | * |
| 673 | * Flushes the log to disk. |
| 674 | */ |
| 675 | |
| 676 | static int gfs2_sync_fs(struct super_block *sb, int wait) |
| 677 | { |
Steven Whitehouse | 1027efa | 2011-03-30 16:13:25 +0100 | [diff] [blame] | 678 | struct gfs2_sbd *sdp = sb->s_fs_info; |
Jan Kara | a117782 | 2012-07-03 16:45:29 +0200 | [diff] [blame] | 679 | |
| 680 | gfs2_quota_sync(sb, -1); |
Bob Peterson | 942b0cd | 2017-08-16 11:30:06 -0500 | [diff] [blame] | 681 | if (wait) |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 682 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | |
| 683 | GFS2_LFC_SYNC_FS); |
Bob Peterson | 942b0cd | 2017-08-16 11:30:06 -0500 | [diff] [blame] | 684 | return sdp->sd_log_error; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 687 | void 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 Peterson | 49787b1 | 2020-12-22 14:43:27 -0600 | [diff] [blame] | 695 | error = gfs2_freeze_lock(sdp, &freeze_gh, 0); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 696 | if (error) { |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 697 | gfs2_assert_withdraw(sdp, 0); |
Abhi Das | 8f918219 | 2019-04-30 16:53:47 -0500 | [diff] [blame] | 698 | } else { |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 699 | atomic_set(&sdp->sd_freeze_state, SFS_UNFROZEN); |
| 700 | error = thaw_super(sb); |
| 701 | if (error) { |
Bob Peterson | f29e62e | 2019-05-13 09:42:18 -0500 | [diff] [blame] | 702 | fs_info(sdp, "GFS2: couldn't thaw filesystem: %d\n", |
| 703 | error); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 704 | gfs2_assert_withdraw(sdp, 0); |
| 705 | } |
Bob Peterson | 49787b1 | 2020-12-22 14:43:27 -0600 | [diff] [blame] | 706 | gfs2_freeze_unlock(&freeze_gh); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 707 | } |
| 708 | deactivate_super(sb); |
Abhi Das | 8f918219 | 2019-04-30 16:53:47 -0500 | [diff] [blame] | 709 | clear_bit_unlock(SDF_FS_FROZEN, &sdp->sd_flags); |
| 710 | wake_up_bit(&sdp->sd_flags, SDF_FS_FROZEN); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 711 | return; |
| 712 | } |
| 713 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 714 | /** |
| 715 | * gfs2_freeze - prevent further writes to the filesystem |
| 716 | * @sb: the VFS structure for the filesystem |
| 717 | * |
| 718 | */ |
| 719 | |
| 720 | static int gfs2_freeze(struct super_block *sb) |
| 721 | { |
| 722 | struct gfs2_sbd *sdp = sb->s_fs_info; |
Bob Peterson | 6c6d583 | 2021-03-25 08:51:13 -0400 | [diff] [blame] | 723 | int error; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 724 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 725 | mutex_lock(&sdp->sd_freeze_mutex); |
Bob Peterson | 6c6d583 | 2021-03-25 08:51:13 -0400 | [diff] [blame] | 726 | if (atomic_read(&sdp->sd_freeze_state) != SFS_UNFROZEN) { |
| 727 | error = -EBUSY; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 728 | goto out; |
Bob Peterson | 6c6d583 | 2021-03-25 08:51:13 -0400 | [diff] [blame] | 729 | } |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 730 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 731 | for (;;) { |
Bob Peterson | 60528af | 2019-11-14 09:53:36 -0500 | [diff] [blame] | 732 | if (gfs2_withdrawn(sdp)) { |
| 733 | error = -EINVAL; |
| 734 | goto out; |
| 735 | } |
| 736 | |
Bob Peterson | 52b1cdc | 2019-11-15 09:42:46 -0500 | [diff] [blame] | 737 | error = gfs2_lock_fs_check_clean(sdp); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 738 | if (!error) |
| 739 | break; |
| 740 | |
Bob Peterson | 55317f5 | 2019-04-29 09:36:23 -0600 | [diff] [blame] | 741 | if (error == -EBUSY) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 742 | fs_err(sdp, "waiting for recovery before freeze\n"); |
Bob Peterson | 52b1cdc | 2019-11-15 09:42:46 -0500 | [diff] [blame] | 743 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 748 | fs_err(sdp, "error freezing FS: %d\n", error); |
Bob Peterson | 52b1cdc | 2019-11-15 09:42:46 -0500 | [diff] [blame] | 749 | } |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 750 | fs_err(sdp, "retrying...\n"); |
| 751 | msleep(1000); |
| 752 | } |
Abhi Das | 8f918219 | 2019-04-30 16:53:47 -0500 | [diff] [blame] | 753 | set_bit(SDF_FS_FROZEN, &sdp->sd_flags); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 754 | out: |
| 755 | mutex_unlock(&sdp->sd_freeze_mutex); |
| 756 | return error; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 757 | } |
| 758 | |
| 759 | /** |
| 760 | * gfs2_unfreeze - reallow writes to the filesystem |
| 761 | * @sb: the VFS structure for the filesystem |
| 762 | * |
| 763 | */ |
| 764 | |
| 765 | static int gfs2_unfreeze(struct super_block *sb) |
| 766 | { |
Steven Whitehouse | d564053f | 2013-01-11 10:49:34 +0000 | [diff] [blame] | 767 | struct gfs2_sbd *sdp = sb->s_fs_info; |
| 768 | |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 769 | mutex_lock(&sdp->sd_freeze_mutex); |
Bob Peterson | 6c6d583 | 2021-03-25 08:51:13 -0400 | [diff] [blame] | 770 | if (atomic_read(&sdp->sd_freeze_state) != SFS_FROZEN || |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 771 | !gfs2_holder_initialized(&sdp->sd_freeze_gh)) { |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 772 | mutex_unlock(&sdp->sd_freeze_mutex); |
Bob Peterson | 6c6d583 | 2021-03-25 08:51:13 -0400 | [diff] [blame] | 773 | return -EINVAL; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 774 | } |
| 775 | |
Bob Peterson | 49787b1 | 2020-12-22 14:43:27 -0600 | [diff] [blame] | 776 | gfs2_freeze_unlock(&sdp->sd_freeze_gh); |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 777 | mutex_unlock(&sdp->sd_freeze_mutex); |
Abhi Das | 8f918219 | 2019-04-30 16:53:47 -0500 | [diff] [blame] | 778 | return wait_on_bit(&sdp->sd_flags, SDF_FS_FROZEN, TASK_INTERRUPTIBLE); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 779 | } |
| 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 | |
| 789 | static 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 | |
| 812 | static int gfs2_statfs_slow(struct gfs2_sbd *sdp, struct gfs2_statfs_change_host *sc) |
| 813 | { |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 814 | 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 Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 822 | gha = kmalloc_array(slots, sizeof(struct gfs2_holder), GFP_KERNEL); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 823 | if (!gha) |
| 824 | return -ENOMEM; |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 825 | for (x = 0; x < slots; x++) |
| 826 | gfs2_holder_mark_uninitialized(gha + x); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 827 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 828 | 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 Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 836 | if (gfs2_holder_initialized(gh) && gfs2_glock_poll(gh)) { |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 837 | err = gfs2_glock_wait(gh); |
| 838 | if (err) { |
| 839 | gfs2_holder_uninit(gh); |
| 840 | error = err; |
| 841 | } else { |
Andreas Gruenbacher | 6f6597ba | 2017-06-30 07:55:08 -0500 | [diff] [blame] | 842 | if (!error) { |
| 843 | struct gfs2_rgrpd *rgd = |
| 844 | gfs2_glock2rgrp(gh->gh_gl); |
| 845 | |
| 846 | error = statfs_slow_fill(rgd, sc); |
| 847 | } |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 848 | gfs2_glock_dq_uninit(gh); |
| 849 | } |
| 850 | } |
| 851 | |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 852 | if (gfs2_holder_initialized(gh)) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 853 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 873 | 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 | |
| 885 | static 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 | |
| 917 | static int gfs2_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 918 | { |
Al Viro | fc64005 | 2016-04-10 01:33:30 -0400 | [diff] [blame] | 919 | struct super_block *sb = dentry->d_sb; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 920 | struct gfs2_sbd *sdp = sb->s_fs_info; |
| 921 | struct gfs2_statfs_change_host sc; |
| 922 | int error; |
| 923 | |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 924 | error = gfs2_rindex_update(sdp); |
| 925 | if (error) |
| 926 | return error; |
| 927 | |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 928 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 949 | * gfs2_drop_inode - Drop an inode (test for remote unlink) |
| 950 | * @inode: The inode to drop |
| 951 | * |
Andreas Gruenbacher | 61b91cf | 2017-08-01 09:54:33 -0500 | [diff] [blame] | 952 | * If we've received a callback on an iopen lock then it's because a |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 953 | * 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 Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 963 | static int gfs2_drop_inode(struct inode *inode) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 964 | { |
| 965 | struct gfs2_inode *ip = GFS2_I(inode); |
| 966 | |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 967 | if (!test_bit(GIF_FREE_VFS_INODE, &ip->i_flags) && |
| 968 | inode->i_nlink && |
| 969 | gfs2_holder_initialized(&ip->i_iopen_gh)) { |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 970 | struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl; |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 971 | if (test_bit(GLF_DEMOTE, &gl->gl_flags)) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 972 | clear_nlink(inode); |
| 973 | } |
Andreas Gruenbacher | 6a1c8f6 | 2017-08-01 11:49:42 -0500 | [diff] [blame] | 974 | |
| 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 Gruenbacher | a0e3cc6 | 2020-01-16 20:12:26 +0100 | [diff] [blame] | 986 | if (!gfs2_queue_delete_work(gl, 0)) |
Andreas Gruenbacher | 6a1c8f6 | 2017-08-01 11:49:42 -0500 | [diff] [blame] | 987 | gfs2_glock_queue_put(gl); |
| 988 | return false; |
| 989 | } |
| 990 | |
Al Viro | 45321ac | 2010-06-07 13:43:19 -0400 | [diff] [blame] | 991 | return generic_drop_inode(inode); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 992 | } |
| 993 | |
| 994 | static 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 Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1007 | * @root: root of this (sub)tree |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1008 | * |
| 1009 | * Returns: 0 on success or error code |
| 1010 | */ |
| 1011 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1012 | static int gfs2_show_options(struct seq_file *s, struct dentry *root) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1013 | { |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1014 | struct gfs2_sbd *sdp = root->d_sb->s_fs_info; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1015 | struct gfs2_args *args = &sdp->sd_args; |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 1016 | int val; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1017 | |
Al Viro | 34c80b1 | 2011-12-08 21:32:45 -0500 | [diff] [blame] | 1018 | if (is_ancestor(root, sdp->sd_master_dir)) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1019 | seq_puts(s, ",meta"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1020 | if (args->ar_lockproto[0]) |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 1021 | seq_show_option(s, "lockproto", args->ar_lockproto); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1022 | if (args->ar_locktable[0]) |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 1023 | seq_show_option(s, "locktable", args->ar_locktable); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1024 | if (args->ar_hostdata[0]) |
Kees Cook | a068acf | 2015-09-04 15:44:57 -0700 | [diff] [blame] | 1025 | seq_show_option(s, "hostdata", args->ar_hostdata); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1026 | if (args->ar_spectator) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1027 | seq_puts(s, ",spectator"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1028 | if (args->ar_localflocks) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1029 | seq_puts(s, ",localflocks"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1030 | if (args->ar_debug) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1031 | seq_puts(s, ",debug"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1032 | if (args->ar_posix_acl) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1033 | seq_puts(s, ",acl"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1034 | 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 Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1053 | seq_puts(s, ",suiddir"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1054 | 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 Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1070 | seq_puts(s, ",discard"); |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 1071 | val = sdp->sd_tune.gt_logd_secs; |
| 1072 | if (val != 30) |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 1073 | 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 Whitehouse | 2b9731e | 2012-08-20 17:07:49 +0100 | [diff] [blame] | 1077 | else if (sdp->sd_tune.gt_statfs_slow) |
| 1078 | seq_puts(s, ",statfs_quantum=0"); |
Benjamin Marzinski | 3d3c10f | 2009-10-20 02:39:44 -0500 | [diff] [blame] | 1079 | 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 Peterson | d34843d | 2009-08-24 10:44:18 +0100 | [diff] [blame] | 1084 | 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 Whitehouse | cdcfde6 | 2009-10-30 10:48:53 +0000 | [diff] [blame] | 1100 | if (test_bit(SDF_NOBARRIERS, &sdp->sd_flags)) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1101 | seq_puts(s, ",nobarrier"); |
Steven Whitehouse | 913a71d | 2010-05-06 11:03:29 +0100 | [diff] [blame] | 1102 | if (test_bit(SDF_DEMOTE, &sdp->sd_flags)) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1103 | seq_puts(s, ",demote_interface_used"); |
Benjamin Marzinski | 90306c4 | 2012-05-29 23:01:09 -0500 | [diff] [blame] | 1104 | if (args->ar_rgrplvb) |
Fabian Frederick | eaebded | 2014-07-02 22:08:46 +0200 | [diff] [blame] | 1105 | seq_puts(s, ",rgrplvb"); |
Benjamin Marzinski | 471f3db | 2015-12-01 08:46:55 -0600 | [diff] [blame] | 1106 | if (args->ar_loccookie) |
| 1107 | seq_puts(s, ",loccookie"); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1111 | static 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 Peterson | 638803d | 2019-06-06 07:33:38 -0500 | [diff] [blame] | 1119 | if (atomic_read(&gl->gl_revokes) == 0) { |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1120 | clear_bit(GLF_LFLUSH, &gl->gl_flags); |
| 1121 | clear_bit(GLF_DIRTY, &gl->gl_flags); |
| 1122 | } |
| 1123 | } |
| 1124 | |
| 1125 | static int gfs2_dinode_dealloc(struct gfs2_inode *ip) |
| 1126 | { |
| 1127 | struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1128 | struct gfs2_rgrpd *rgd; |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1129 | struct gfs2_holder gh; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1130 | int error; |
| 1131 | |
| 1132 | if (gfs2_get_inode_blocks(&ip->i_inode) != 1) { |
Steven Whitehouse | 94fb763 | 2011-05-09 13:36:10 +0100 | [diff] [blame] | 1133 | gfs2_consist_inode(ip); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1134 | return -EIO; |
| 1135 | } |
| 1136 | |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1137 | error = gfs2_rindex_update(sdp); |
| 1138 | if (error) |
| 1139 | return error; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1140 | |
Eric W. Biederman | f4108a6 | 2013-01-31 17:49:26 -0800 | [diff] [blame] | 1141 | error = gfs2_quota_hold(ip, NO_UID_QUOTA_CHANGE, NO_GID_QUOTA_CHANGE); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1142 | if (error) |
Bob Peterson | 5407e24 | 2012-05-18 09:28:23 -0400 | [diff] [blame] | 1143 | return error; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1144 | |
Steven Whitehouse | 66fc061 | 2012-02-08 12:58:32 +0000 | [diff] [blame] | 1145 | rgd = gfs2_blk2rgrpd(sdp, ip->i_no_addr, 1); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1146 | if (!rgd) { |
| 1147 | gfs2_consist_inode(ip); |
| 1148 | error = -EIO; |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 1149 | goto out_qs; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1152 | error = gfs2_glock_nq_init(rgd->rd_gl, LM_ST_EXCLUSIVE, 0, &gh); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1153 | if (error) |
Steven Whitehouse | 8339ee5 | 2011-08-31 16:38:29 +0100 | [diff] [blame] | 1154 | goto out_qs; |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1155 | |
Steven Whitehouse | 4667a0e | 2011-04-18 14:18:09 +0100 | [diff] [blame] | 1156 | error = gfs2_trans_begin(sdp, RES_RG_BIT + RES_STATFS + RES_QUOTA, |
| 1157 | sdp->sd_jdesc->jd_blocks); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1158 | 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 | |
| 1167 | out_rg_gunlock: |
Bob Peterson | 564e12b | 2011-11-21 13:36:17 -0500 | [diff] [blame] | 1168 | gfs2_glock_dq_uninit(&gh); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1169 | out_qs: |
| 1170 | gfs2_quota_unhold(ip); |
Steven Whitehouse | f42ab08 | 2011-04-14 16:50:31 +0100 | [diff] [blame] | 1171 | return error; |
| 1172 | } |
| 1173 | |
Steven Whitehouse | 380f7c6 | 2011-07-14 08:59:44 +0100 | [diff] [blame] | 1174 | /** |
Andreas Gruenbacher | 71c1b213 | 2017-08-01 11:45:23 -0500 | [diff] [blame] | 1175 | * 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 | |
| 1182 | static 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 Gruenbacher | 9e73330 | 2020-01-14 14:59:08 +0100 | [diff] [blame] | 1190 | static 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 Gruenbacher | 9e8990d | 2020-01-17 10:53:23 +0100 | [diff] [blame] | 1205 | * 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 Gruenbacher | 9e73330 | 2020-01-14 14:59:08 +0100 | [diff] [blame] | 1211 | * |
| 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 Gruenbacher | 9e8990d | 2020-01-17 10:53:23 +0100 | [diff] [blame] | 1229 | !test_bit(HIF_WAIT, &gh->gh_iflags) || |
| 1230 | test_bit(GLF_DEMOTE, &ip->i_gl->gl_flags), |
Andreas Gruenbacher | 9e73330 | 2020-01-14 14:59:08 +0100 | [diff] [blame] | 1231 | 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 Gruenbacher | 71c1b213 | 2017-08-01 11:45:23 -0500 | [diff] [blame] | 1239 | /** |
Bob Peterson | 53dbc27e | 2020-09-11 10:30:26 -0500 | [diff] [blame] | 1240 | * 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 | */ |
| 1248 | static 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 | |
| 1295 | should_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 Peterson | 6e7e9a5 | 2020-09-11 09:29:25 -0500 | [diff] [blame] | 1307 | * evict_unlinked_inode - delete the pieces of an unlinked evicted inode |
| 1308 | * @inode: The inode to evict |
| 1309 | */ |
| 1310 | static 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); |
| 1341 | out: |
| 1342 | return ret; |
| 1343 | } |
| 1344 | |
Bob Peterson | d90be6a | 2020-09-11 14:53:52 -0500 | [diff] [blame] | 1345 | /* |
| 1346 | * evict_linked_inode - evict an inode whose dinode has not been unlinked |
| 1347 | * @inode: The inode to evict |
| 1348 | */ |
| 1349 | static 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 Peterson | 6e7e9a5 | 2020-09-11 09:29:25 -0500 | [diff] [blame] | 1378 | /** |
Steven Whitehouse | 380f7c6 | 2011-07-14 08:59:44 +0100 | [diff] [blame] | 1379 | * 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1392 | * 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 Viro | d5c1515 | 2010-06-07 11:05:19 -0400 | [diff] [blame] | 1399 | static void gfs2_evict_inode(struct inode *inode) |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1400 | { |
Steven Whitehouse | 001e8e8 | 2011-03-30 14:17:51 +0100 | [diff] [blame] | 1401 | struct super_block *sb = inode->i_sb; |
| 1402 | struct gfs2_sbd *sdp = sb->s_fs_info; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1403 | struct gfs2_inode *ip = GFS2_I(inode); |
| 1404 | struct gfs2_holder gh; |
Bob Peterson | 23d828f | 2020-09-11 10:56:31 -0500 | [diff] [blame] | 1405 | int ret; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1406 | |
Abhi Das | 0597880 | 2014-03-31 10:33:17 -0500 | [diff] [blame] | 1407 | if (test_bit(GIF_FREE_VFS_INODE, &ip->i_flags)) { |
| 1408 | clear_inode(inode); |
| 1409 | return; |
| 1410 | } |
| 1411 | |
David Howells | bc98a42 | 2017-07-17 08:45:34 +0100 | [diff] [blame] | 1412 | if (inode->i_nlink || sb_rdonly(sb)) |
Al Viro | d5c1515 | 2010-06-07 11:05:19 -0400 | [diff] [blame] | 1413 | goto out; |
| 1414 | |
Bob Peterson | 53dbc27e | 2020-09-11 10:30:26 -0500 | [diff] [blame] | 1415 | gfs2_holder_mark_uninitialized(&gh); |
| 1416 | ret = evict_should_delete(inode, &gh); |
| 1417 | if (ret == SHOULD_DEFER_EVICTION) |
Andreas Gruenbacher | 8c7b926 | 2020-01-13 22:16:17 +0100 | [diff] [blame] | 1418 | goto out; |
Bob Peterson | 0a0d9f5 | 2020-09-16 08:50:44 -0500 | [diff] [blame] | 1419 | if (ret == SHOULD_DELETE_DINODE) |
| 1420 | ret = evict_unlinked_inode(inode); |
| 1421 | else |
| 1422 | ret = evict_linked_inode(inode); |
Steven Whitehouse | acf7e24 | 2009-09-08 18:00:30 +0100 | [diff] [blame] | 1423 | |
Bob Peterson | a097dc7e | 2015-07-16 08:28:04 -0500 | [diff] [blame] | 1424 | if (gfs2_rs_active(&ip->i_res)) |
| 1425 | gfs2_rs_deltree(&ip->i_res); |
Bob Peterson | 8e2e004 | 2012-07-19 08:12:40 -0400 | [diff] [blame] | 1426 | |
Bob Peterson | 240c623 | 2017-07-18 12:36:01 -0500 | [diff] [blame] | 1427 | if (gfs2_holder_initialized(&gh)) { |
| 1428 | glock_clear_object(ip->i_gl, ip); |
Andreas Gruenbacher | e0b62e2 | 2017-06-30 08:16:46 -0500 | [diff] [blame] | 1429 | gfs2_glock_dq_uninit(&gh); |
Bob Peterson | 240c623 | 2017-07-18 12:36:01 -0500 | [diff] [blame] | 1430 | } |
Bob Peterson | 23d828f | 2020-09-11 10:56:31 -0500 | [diff] [blame] | 1431 | if (ret && ret != GLR_TRYFAILED && ret != -EROFS) |
| 1432 | fs_warn(sdp, "gfs2_evict_inode: %d\n", ret); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1433 | out: |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 1434 | truncate_inode_pages_final(&inode->i_data); |
Bob Peterson | 2fba46a | 2020-02-27 12:47:53 -0600 | [diff] [blame] | 1435 | if (ip->i_qadata) |
| 1436 | gfs2_assert_warn(sdp, ip->i_qadata->qa_ref == 0); |
Andreas Gruenbacher | 1595548 | 2020-03-06 10:32:35 -0600 | [diff] [blame] | 1437 | gfs2_rs_delete(ip, NULL); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 1438 | gfs2_ordered_del_inode(ip); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 1439 | clear_inode(inode); |
Steven Whitehouse | 17d539f | 2011-06-15 10:29:37 +0100 | [diff] [blame] | 1440 | gfs2_dir_hash_inval(ip); |
Andreas Gruenbacher | 6df9f9a | 2016-06-17 07:31:27 -0500 | [diff] [blame] | 1441 | if (gfs2_holder_initialized(&ip->i_iopen_gh)) { |
Andreas Gruenbacher | 71c1b213 | 2017-08-01 11:45:23 -0500 | [diff] [blame] | 1442 | struct gfs2_glock *gl = ip->i_iopen_gh.gh_gl; |
| 1443 | |
| 1444 | glock_clear_object(gl, ip); |
Andreas Gruenbacher | 40e7e86 | 2020-01-24 14:14:46 +0100 | [diff] [blame] | 1445 | 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 Gruenbacher | 71c1b213 | 2017-08-01 11:45:23 -0500 | [diff] [blame] | 1449 | gfs2_glock_hold(gl); |
Andreas Gruenbacher | 40e7e86 | 2020-01-24 14:14:46 +0100 | [diff] [blame] | 1450 | gfs2_holder_uninit(&ip->i_iopen_gh); |
Andreas Gruenbacher | 71c1b213 | 2017-08-01 11:45:23 -0500 | [diff] [blame] | 1451 | gfs2_glock_put_eventually(gl); |
Al Viro | d5c1515 | 2010-06-07 11:05:19 -0400 | [diff] [blame] | 1452 | } |
Bob Peterson | 664ccea | 2021-10-28 11:53:10 -0500 | [diff] [blame] | 1453 | 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1460 | } |
| 1461 | |
| 1462 | static 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 Gruenbacher | d403125 | 2019-07-24 13:05:38 +0200 | [diff] [blame] | 1467 | if (!ip) |
| 1468 | return NULL; |
| 1469 | ip->i_flags = 0; |
| 1470 | ip->i_gl = NULL; |
Andreas Gruenbacher | 40e7e86 | 2020-01-24 14:14:46 +0100 | [diff] [blame] | 1471 | gfs2_holder_mark_uninitialized(&ip->i_iopen_gh); |
Andreas Gruenbacher | d403125 | 2019-07-24 13:05:38 +0200 | [diff] [blame] | 1472 | memset(&ip->i_res, 0, sizeof(ip->i_res)); |
| 1473 | RB_CLEAR_NODE(&ip->i_res.rs_node); |
| 1474 | ip->i_rahead = 0; |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1475 | return &ip->i_inode; |
| 1476 | } |
| 1477 | |
Al Viro | 784494e | 2019-04-15 19:45:26 -0400 | [diff] [blame] | 1478 | static void gfs2_free_inode(struct inode *inode) |
Nick Piggin | fa0d7e3d | 2011-01-07 17:49:49 +1100 | [diff] [blame] | 1479 | { |
Al Viro | 784494e | 2019-04-15 19:45:26 -0400 | [diff] [blame] | 1480 | kmem_cache_free(gfs2_inode_cachep, GFS2_I(inode)); |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1481 | } |
| 1482 | |
Abhi Das | 97fd734 | 2020-10-20 15:58:04 -0500 | [diff] [blame] | 1483 | extern 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 | |
| 1498 | extern 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 Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1512 | const struct super_operations gfs2_super_ops = { |
| 1513 | .alloc_inode = gfs2_alloc_inode, |
Al Viro | 784494e | 2019-04-15 19:45:26 -0400 | [diff] [blame] | 1514 | .free_inode = gfs2_free_inode, |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1515 | .write_inode = gfs2_write_inode, |
Steven Whitehouse | ab9bbda | 2011-08-15 14:20:36 +0100 | [diff] [blame] | 1516 | .dirty_inode = gfs2_dirty_inode, |
Al Viro | d5c1515 | 2010-06-07 11:05:19 -0400 | [diff] [blame] | 1517 | .evict_inode = gfs2_evict_inode, |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1518 | .put_super = gfs2_put_super, |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1519 | .sync_fs = gfs2_sync_fs, |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 1520 | .freeze_super = gfs2_freeze, |
| 1521 | .thaw_super = gfs2_unfreeze, |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1522 | .statfs = gfs2_statfs, |
Steven Whitehouse | 9e6e0a1 | 2009-05-22 10:36:01 +0100 | [diff] [blame] | 1523 | .drop_inode = gfs2_drop_inode, |
| 1524 | .show_options = gfs2_show_options, |
| 1525 | }; |
| 1526 | |