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. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 4 | * Copyright (C) 2004-2006 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 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 9 | #include <linux/sched.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/spinlock.h> |
| 12 | #include <linux/completion.h> |
| 13 | #include <linux/buffer_head.h> |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 14 | #include <linux/kallsyms.h> |
Steven Whitehouse | f057f6c | 2009-01-12 10:43:39 +0000 | [diff] [blame] | 15 | #include <linux/gfs2_ondisk.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 16 | |
| 17 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 18 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 19 | #include "glock.h" |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 20 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 21 | #include "log.h" |
| 22 | #include "lops.h" |
| 23 | #include "meta_io.h" |
| 24 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 25 | #include "util.h" |
Benjamin Marzinski | 5e687ea | 2010-05-04 14:29:16 -0500 | [diff] [blame] | 26 | #include "trace_gfs2.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 27 | |
Bob Peterson | b0be23b | 2020-07-23 13:14:07 -0500 | [diff] [blame] | 28 | static void gfs2_print_trans(struct gfs2_sbd *sdp, const struct gfs2_trans *tr) |
| 29 | { |
| 30 | fs_warn(sdp, "Transaction created at: %pSR\n", (void *)tr->tr_ip); |
| 31 | fs_warn(sdp, "blocks=%u revokes=%u reserved=%u touched=%u\n", |
| 32 | tr->tr_blocks, tr->tr_revokes, tr->tr_reserved, |
| 33 | test_bit(TR_TOUCHED, &tr->tr_flags)); |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 34 | fs_warn(sdp, "Buf %u/%u Databuf %u/%u Revoke %u\n", |
Bob Peterson | b0be23b | 2020-07-23 13:14:07 -0500 | [diff] [blame] | 35 | tr->tr_num_buf_new, tr->tr_num_buf_rm, |
| 36 | tr->tr_num_databuf_new, tr->tr_num_databuf_rm, |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 37 | tr->tr_num_revoke); |
Bob Peterson | b0be23b | 2020-07-23 13:14:07 -0500 | [diff] [blame] | 38 | } |
| 39 | |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 40 | int __gfs2_trans_begin(struct gfs2_trans *tr, struct gfs2_sbd *sdp, |
| 41 | unsigned int blocks, unsigned int revokes, |
| 42 | unsigned long ip) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 | { |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 44 | unsigned int extra_revokes; |
| 45 | |
Bob Peterson | b0be23b | 2020-07-23 13:14:07 -0500 | [diff] [blame] | 46 | if (current->journal_info) { |
| 47 | gfs2_print_trans(sdp, current->journal_info); |
| 48 | BUG(); |
| 49 | } |
Steven Whitehouse | d0dc80d | 2006-03-29 14:36:49 -0500 | [diff] [blame] | 50 | BUG_ON(blocks == 0 && revokes == 0); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 51 | |
Steven Whitehouse | a1c0643 | 2009-05-13 10:56:52 +0100 | [diff] [blame] | 52 | if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) |
| 53 | return -EROFS; |
| 54 | |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 55 | tr->tr_ip = ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 56 | tr->tr_blocks = blocks; |
| 57 | tr->tr_revokes = revokes; |
Andreas Gruenbacher | fe3e397 | 2020-12-10 12:49:56 +0100 | [diff] [blame] | 58 | tr->tr_reserved = GFS2_LOG_FLUSH_MIN_BLOCKS; |
Andreas Gruenbacher | 297de31 | 2020-12-06 14:04:36 +0100 | [diff] [blame] | 59 | if (blocks) { |
| 60 | /* |
| 61 | * The reserved blocks are either used for data or metadata. |
| 62 | * We can have mixed data and metadata, each with its own log |
| 63 | * descriptor block; see calc_reserved(). |
| 64 | */ |
| 65 | tr->tr_reserved += blocks + 1 + DIV_ROUND_UP(blocks - 1, databuf_limit(sdp)); |
| 66 | } |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 67 | INIT_LIST_HEAD(&tr->tr_databuf); |
| 68 | INIT_LIST_HEAD(&tr->tr_buf); |
Bob Peterson | 462582b | 2020-08-21 08:50:34 -0500 | [diff] [blame] | 69 | INIT_LIST_HEAD(&tr->tr_list); |
Bob Peterson | cbcc89b | 2020-06-05 14:12:34 -0500 | [diff] [blame] | 70 | INIT_LIST_HEAD(&tr->tr_ail1_list); |
| 71 | INIT_LIST_HEAD(&tr->tr_ail2_list); |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 72 | |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 73 | if (gfs2_assert_warn(sdp, tr->tr_reserved <= sdp->sd_jdesc->jd_blocks)) |
| 74 | return -EINVAL; |
| 75 | |
Jan Kara | 39263d5e | 2012-06-12 16:20:41 +0200 | [diff] [blame] | 76 | sb_start_intwrite(sdp->sd_vfs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 77 | |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 78 | /* |
| 79 | * Try the reservations under sd_log_flush_lock to prevent log flushes |
| 80 | * from creating inconsistencies between the number of allocated and |
| 81 | * reserved revokes. If that fails, do a full-block allocation outside |
| 82 | * of the lock to avoid stalling log flushes. Then, allot the |
| 83 | * appropriate number of blocks to revokes, use as many revokes locally |
| 84 | * as needed, and "release" the surplus into the revokes pool. |
| 85 | */ |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 86 | |
| 87 | down_read(&sdp->sd_log_flush_lock); |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 88 | if (gfs2_log_try_reserve(sdp, tr, &extra_revokes)) |
| 89 | goto reserved; |
| 90 | up_read(&sdp->sd_log_flush_lock); |
| 91 | gfs2_log_reserve(sdp, tr, &extra_revokes); |
| 92 | down_read(&sdp->sd_log_flush_lock); |
| 93 | |
| 94 | reserved: |
| 95 | gfs2_log_release_revokes(sdp, extra_revokes); |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 96 | if (unlikely(!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags))) { |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 97 | gfs2_log_release_revokes(sdp, tr->tr_revokes); |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 98 | up_read(&sdp->sd_log_flush_lock); |
Andreas Gruenbacher | 5ae8fff | 2020-12-13 11:37:17 +0100 | [diff] [blame] | 99 | gfs2_log_release(sdp, tr->tr_reserved); |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 100 | sb_end_intwrite(sdp->sd_vfs); |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 101 | return -EROFS; |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 102 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 104 | current->journal_info = tr; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 105 | |
| 106 | return 0; |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 107 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 108 | |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 109 | int gfs2_trans_begin(struct gfs2_sbd *sdp, unsigned int blocks, |
| 110 | unsigned int revokes) |
| 111 | { |
| 112 | struct gfs2_trans *tr; |
| 113 | int error; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 114 | |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 115 | tr = kmem_cache_zalloc(gfs2_trans_cachep, GFP_NOFS); |
| 116 | if (!tr) |
| 117 | return -ENOMEM; |
| 118 | error = __gfs2_trans_begin(tr, sdp, blocks, revokes, _RET_IP_); |
| 119 | if (error) |
| 120 | kmem_cache_free(gfs2_trans_cachep, tr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 | return error; |
| 122 | } |
| 123 | |
| 124 | void gfs2_trans_end(struct gfs2_sbd *sdp) |
| 125 | { |
Steven Whitehouse | f4154ea | 2006-04-11 14:49:06 -0400 | [diff] [blame] | 126 | struct gfs2_trans *tr = current->journal_info; |
Steven Whitehouse | c50b91c | 2012-04-16 16:40:56 +0100 | [diff] [blame] | 127 | s64 nbuf; |
Benjamin Marzinski | 2e60d76 | 2014-11-13 20:42:04 -0600 | [diff] [blame] | 128 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 129 | current->journal_info = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 130 | |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 131 | if (!test_bit(TR_TOUCHED, &tr->tr_flags)) { |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 132 | gfs2_log_release_revokes(sdp, tr->tr_revokes); |
Andreas Gruenbacher | c1eba1b | 2020-12-12 23:30:22 +0100 | [diff] [blame] | 133 | up_read(&sdp->sd_log_flush_lock); |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 134 | gfs2_log_release(sdp, tr->tr_reserved); |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 135 | if (!test_bit(TR_ONSTACK, &tr->tr_flags)) |
Bob Peterson | b839dad | 2019-04-17 12:04:27 -0600 | [diff] [blame] | 136 | gfs2_trans_free(sdp, tr); |
Andreas Gruenbacher | 15e20a3 | 2021-02-03 16:15:27 +0100 | [diff] [blame] | 137 | sb_end_intwrite(sdp->sd_vfs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 141 | gfs2_log_release_revokes(sdp, tr->tr_revokes - tr->tr_num_revoke); |
| 142 | |
Steven Whitehouse | c50b91c | 2012-04-16 16:40:56 +0100 | [diff] [blame] | 143 | nbuf = tr->tr_num_buf_new + tr->tr_num_databuf_new; |
| 144 | nbuf -= tr->tr_num_buf_rm; |
| 145 | nbuf -= tr->tr_num_databuf_rm; |
| 146 | |
Andreas Gruenbacher | 625a8ed | 2020-12-06 20:10:51 +0100 | [diff] [blame] | 147 | if (gfs2_assert_withdraw(sdp, nbuf <= tr->tr_blocks) || |
| 148 | gfs2_assert_withdraw(sdp, tr->tr_num_revoke <= tr->tr_revokes)) |
Bob Peterson | e54c78a | 2018-10-03 08:47:36 -0500 | [diff] [blame] | 149 | gfs2_print_trans(sdp, tr); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 150 | |
| 151 | gfs2_log_commit(sdp, tr); |
Andreas Gruenbacher | c968f57 | 2021-01-29 16:45:33 +0100 | [diff] [blame] | 152 | if (!test_bit(TR_ONSTACK, &tr->tr_flags) && |
| 153 | !test_bit(TR_ATTACHED, &tr->tr_flags)) |
Bob Peterson | b839dad | 2019-04-17 12:04:27 -0600 | [diff] [blame] | 154 | gfs2_trans_free(sdp, tr); |
Benjamin Marzinski | 16ca941 | 2013-04-05 20:31:46 -0500 | [diff] [blame] | 155 | up_read(&sdp->sd_log_flush_lock); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 156 | |
Linus Torvalds | 1751e8a | 2017-11-27 13:05:09 -0800 | [diff] [blame] | 157 | if (sdp->sd_vfs->s_flags & SB_SYNCHRONOUS) |
Bob Peterson | 805c0907 | 2018-01-08 10:34:17 -0500 | [diff] [blame] | 158 | gfs2_log_flush(sdp, NULL, GFS2_LOG_HEAD_FLUSH_NORMAL | |
| 159 | GFS2_LFC_TRANS_END); |
Andreas Gruenbacher | 15e20a3 | 2021-02-03 16:15:27 +0100 | [diff] [blame] | 160 | sb_end_intwrite(sdp->sd_vfs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 163 | static struct gfs2_bufdata *gfs2_alloc_bufdata(struct gfs2_glock *gl, |
Bob Peterson | cbbe76c | 2018-11-16 14:18:32 -0600 | [diff] [blame] | 164 | struct buffer_head *bh) |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 165 | { |
| 166 | struct gfs2_bufdata *bd; |
| 167 | |
| 168 | bd = kmem_cache_zalloc(gfs2_bufdata_cachep, GFP_NOFS | __GFP_NOFAIL); |
| 169 | bd->bd_bh = bh; |
| 170 | bd->bd_gl = gl; |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 171 | INIT_LIST_HEAD(&bd->bd_list); |
Bob Peterson | 1a5a2cf | 2021-02-25 11:11:09 -0500 | [diff] [blame] | 172 | INIT_LIST_HEAD(&bd->bd_ail_st_list); |
| 173 | INIT_LIST_HEAD(&bd->bd_ail_gl_list); |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 174 | bh->b_private = bd; |
| 175 | return bd; |
| 176 | } |
| 177 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 178 | /** |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 179 | * gfs2_trans_add_data - Add a databuf to the transaction. |
| 180 | * @gl: The inode glock associated with the buffer |
| 181 | * @bh: The buffer to add |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 182 | * |
Andreas Gruenbacher | 845802b | 2018-06-04 07:50:16 -0500 | [diff] [blame] | 183 | * This is used in journaled data mode. |
| 184 | * We need to journal the data block in the same way as metadata in |
| 185 | * the functions above. The difference is that here we have a tag |
| 186 | * which is two __be64's being the block number (as per meta data) |
| 187 | * and a flag which says whether the data block needs escaping or |
| 188 | * not. This means we need a new log entry for each 251 or so data |
| 189 | * blocks, which isn't an enormous overhead but twice as much as |
| 190 | * for normal metadata blocks. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 191 | */ |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 192 | void gfs2_trans_add_data(struct gfs2_glock *gl, struct buffer_head *bh) |
| 193 | { |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 194 | struct gfs2_trans *tr = current->journal_info; |
Bob Peterson | 15562c4 | 2015-03-16 11:52:05 -0500 | [diff] [blame] | 195 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 196 | struct gfs2_bufdata *bd; |
| 197 | |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 198 | lock_buffer(bh); |
Bob Peterson | aacee72 | 2017-01-30 11:51:21 -0500 | [diff] [blame] | 199 | if (buffer_pinned(bh)) { |
| 200 | set_bit(TR_TOUCHED, &tr->tr_flags); |
| 201 | goto out; |
| 202 | } |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 203 | gfs2_log_lock(sdp); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 204 | bd = bh->b_private; |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 205 | if (bd == NULL) { |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 206 | gfs2_log_unlock(sdp); |
| 207 | unlock_buffer(bh); |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 208 | if (bh->b_private == NULL) |
Bob Peterson | cbbe76c | 2018-11-16 14:18:32 -0600 | [diff] [blame] | 209 | bd = gfs2_alloc_bufdata(gl, bh); |
Bob Peterson | 491e94f | 2015-10-01 11:47:31 -0500 | [diff] [blame] | 210 | else |
| 211 | bd = bh->b_private; |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 212 | lock_buffer(bh); |
| 213 | gfs2_log_lock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 214 | } |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 215 | gfs2_assert(sdp, bd->bd_gl == gl); |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 216 | set_bit(TR_TOUCHED, &tr->tr_flags); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 217 | if (list_empty(&bd->bd_list)) { |
| 218 | set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
| 219 | set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); |
| 220 | gfs2_pin(sdp, bd->bd_bh); |
| 221 | tr->tr_num_databuf_new++; |
Steven Whitehouse | d69a3c6 | 2014-02-21 15:22:35 +0000 | [diff] [blame] | 222 | list_add_tail(&bd->bd_list, &tr->tr_databuf); |
Steven Whitehouse | 4513899 | 2013-01-28 09:30:07 +0000 | [diff] [blame] | 223 | } |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 224 | gfs2_log_unlock(sdp); |
Bob Peterson | aacee72 | 2017-01-30 11:51:21 -0500 | [diff] [blame] | 225 | out: |
Benjamin Marzinski | 96e5d1d | 2012-11-07 00:38:06 -0600 | [diff] [blame] | 226 | unlock_buffer(bh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Steven Whitehouse | 350a9b0 | 2012-12-14 12:36:02 +0000 | [diff] [blame] | 229 | void gfs2_trans_add_meta(struct gfs2_glock *gl, struct buffer_head *bh) |
| 230 | { |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 231 | |
Bob Peterson | 15562c4 | 2015-03-16 11:52:05 -0500 | [diff] [blame] | 232 | struct gfs2_sbd *sdp = gl->gl_name.ln_sbd; |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 233 | struct gfs2_bufdata *bd; |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 234 | struct gfs2_meta_header *mh; |
Bob Peterson | aacee72 | 2017-01-30 11:51:21 -0500 | [diff] [blame] | 235 | struct gfs2_trans *tr = current->journal_info; |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 236 | enum gfs2_freeze_state state = atomic_read(&sdp->sd_freeze_state); |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 237 | |
| 238 | lock_buffer(bh); |
Bob Peterson | aacee72 | 2017-01-30 11:51:21 -0500 | [diff] [blame] | 239 | if (buffer_pinned(bh)) { |
| 240 | set_bit(TR_TOUCHED, &tr->tr_flags); |
| 241 | goto out; |
| 242 | } |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 243 | gfs2_log_lock(sdp); |
| 244 | bd = bh->b_private; |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 245 | if (bd == NULL) { |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 246 | gfs2_log_unlock(sdp); |
| 247 | unlock_buffer(bh); |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 248 | lock_page(bh->b_page); |
| 249 | if (bh->b_private == NULL) |
Bob Peterson | cbbe76c | 2018-11-16 14:18:32 -0600 | [diff] [blame] | 250 | bd = gfs2_alloc_bufdata(gl, bh); |
Bob Peterson | 491e94f | 2015-10-01 11:47:31 -0500 | [diff] [blame] | 251 | else |
| 252 | bd = bh->b_private; |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 253 | unlock_page(bh->b_page); |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 254 | lock_buffer(bh); |
| 255 | gfs2_log_lock(sdp); |
| 256 | } |
Steven Whitehouse | c76c4d9 | 2012-12-14 17:54:21 +0000 | [diff] [blame] | 257 | gfs2_assert(sdp, bd->bd_gl == gl); |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 258 | set_bit(TR_TOUCHED, &tr->tr_flags); |
| 259 | if (!list_empty(&bd->bd_list)) |
| 260 | goto out_unlock; |
| 261 | set_bit(GLF_LFLUSH, &bd->bd_gl->gl_flags); |
| 262 | set_bit(GLF_DIRTY, &bd->bd_gl->gl_flags); |
| 263 | mh = (struct gfs2_meta_header *)bd->bd_bh->b_data; |
| 264 | if (unlikely(mh->mh_magic != cpu_to_be32(GFS2_MAGIC))) { |
Bob Peterson | e54c78a | 2018-10-03 08:47:36 -0500 | [diff] [blame] | 265 | fs_err(sdp, "Attempting to add uninitialised block to " |
| 266 | "journal (inplace block=%lld)\n", |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 267 | (unsigned long long)bd->bd_bh->b_blocknr); |
| 268 | BUG(); |
| 269 | } |
| 270 | if (unlikely(state == SFS_FROZEN)) { |
Bob Peterson | e54c78a | 2018-10-03 08:47:36 -0500 | [diff] [blame] | 271 | fs_info(sdp, "GFS2:adding buf while frozen\n"); |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 272 | gfs2_assert_withdraw(sdp, 0); |
| 273 | } |
Bob Peterson | 2ca0c2f | 2019-11-13 13:58:30 -0600 | [diff] [blame] | 274 | if (unlikely(gfs2_withdrawn(sdp))) { |
| 275 | fs_info(sdp, "GFS2:adding buf while withdrawn! 0x%llx\n", |
| 276 | (unsigned long long)bd->bd_bh->b_blocknr); |
| 277 | } |
Bob Peterson | 192738b | 2017-01-25 12:57:42 -0500 | [diff] [blame] | 278 | gfs2_pin(sdp, bd->bd_bh); |
| 279 | mh->__pad0 = cpu_to_be64(0); |
| 280 | mh->mh_jid = cpu_to_be32(sdp->sd_jdesc->jd_jid); |
| 281 | list_add(&bd->bd_list, &tr->tr_buf); |
| 282 | tr->tr_num_buf_new++; |
| 283 | out_unlock: |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 284 | gfs2_log_unlock(sdp); |
Bob Peterson | aacee72 | 2017-01-30 11:51:21 -0500 | [diff] [blame] | 285 | out: |
Steven Whitehouse | 767f433 | 2012-12-14 12:52:14 +0000 | [diff] [blame] | 286 | unlock_buffer(bh); |
Steven Whitehouse | 350a9b0 | 2012-12-14 12:36:02 +0000 | [diff] [blame] | 287 | } |
| 288 | |
Steven Whitehouse | 1ad38c4 | 2007-09-03 11:01:33 +0100 | [diff] [blame] | 289 | void gfs2_trans_add_revoke(struct gfs2_sbd *sdp, struct gfs2_bufdata *bd) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 290 | { |
Steven Whitehouse | 75f2b87 | 2012-12-14 12:29:56 +0000 | [diff] [blame] | 291 | struct gfs2_trans *tr = current->journal_info; |
| 292 | |
Bob Peterson | c0752aa | 2012-05-01 12:00:34 -0400 | [diff] [blame] | 293 | BUG_ON(!list_empty(&bd->bd_list)); |
Benjamin Marzinski | 5d05496 | 2013-06-14 11:38:29 -0500 | [diff] [blame] | 294 | gfs2_add_revoke(sdp, bd); |
Bob Peterson | 9862ca0 | 2017-01-25 12:50:47 -0500 | [diff] [blame] | 295 | set_bit(TR_TOUCHED, &tr->tr_flags); |
Steven Whitehouse | 75f2b87 | 2012-12-14 12:29:56 +0000 | [diff] [blame] | 296 | tr->tr_num_revoke++; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Andreas Gruenbacher | fbb2787 | 2019-04-05 12:18:23 +0100 | [diff] [blame] | 299 | void gfs2_trans_remove_revoke(struct gfs2_sbd *sdp, u64 blkno, unsigned int len) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 300 | { |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 301 | struct gfs2_bufdata *bd, *tmp; |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 302 | unsigned int n = len; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 303 | |
| 304 | gfs2_log_lock(sdp); |
Andreas Gruenbacher | a5b1d3f | 2019-04-05 12:16:14 +0100 | [diff] [blame] | 305 | list_for_each_entry_safe(bd, tmp, &sdp->sd_log_revokes, bd_list) { |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 306 | if ((bd->bd_blkno >= blkno) && (bd->bd_blkno < (blkno + len))) { |
Bob Peterson | c0752aa | 2012-05-01 12:00:34 -0400 | [diff] [blame] | 307 | list_del_init(&bd->bd_list); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | gfs2_assert_withdraw(sdp, sdp->sd_log_num_revoke); |
| 309 | sdp->sd_log_num_revoke--; |
Bob Peterson | fe5e7ba | 2019-11-14 09:49:11 -0500 | [diff] [blame] | 310 | if (bd->bd_gl) |
| 311 | gfs2_glock_remove_revoke(bd->bd_gl); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 312 | kmem_cache_free(gfs2_bufdata_cachep, bd); |
Andreas Gruenbacher | 2129b42 | 2020-12-17 16:14:30 +0100 | [diff] [blame] | 313 | gfs2_log_release_revokes(sdp, 1); |
Steven Whitehouse | 5731be5 | 2008-02-01 13:16:55 +0000 | [diff] [blame] | 314 | if (--n == 0) |
| 315 | break; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 318 | gfs2_log_unlock(sdp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 319 | } |
| 320 | |
Bob Peterson | b839dad | 2019-04-17 12:04:27 -0600 | [diff] [blame] | 321 | void gfs2_trans_free(struct gfs2_sbd *sdp, struct gfs2_trans *tr) |
| 322 | { |
| 323 | if (tr == NULL) |
| 324 | return; |
| 325 | |
| 326 | gfs2_assert_warn(sdp, list_empty(&tr->tr_ail1_list)); |
| 327 | gfs2_assert_warn(sdp, list_empty(&tr->tr_ail2_list)); |
| 328 | gfs2_assert_warn(sdp, list_empty(&tr->tr_databuf)); |
| 329 | gfs2_assert_warn(sdp, list_empty(&tr->tr_buf)); |
| 330 | kmem_cache_free(gfs2_trans_cachep, tr); |
| 331 | } |